亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費(fèi)注冊(cè) 查看新帖 |

Chinaunix

  平臺(tái) 論壇 博客 文庫
最近訪問板塊 發(fā)新帖
樓主: loveguohuasai
打印 上一主題 下一主題

[算法] 母牛數(shù)量算法 [復(fù)制鏈接]

論壇徽章:
1
2015年辭舊歲徽章
日期:2015-03-03 16:54:15
101 [報(bào)告]
發(fā)表于 2003-08-21 12:18 |只看該作者

母牛數(shù)量算法

呵呵。我的答案不一定是效率最高的,但是理解起來絕對(duì)是最簡(jiǎn)單的,而且結(jié)果也絕對(duì)是正確的。凡是和我的結(jié)果不同的,當(dāng)然就是錯(cuò)的。

論壇徽章:
0
102 [報(bào)告]
發(fā)表于 2003-08-21 15:39 |只看該作者

母牛數(shù)量算法

[quote]原帖由 "db_info"]不會(huì)吧,第100年時(shí),第1頭母?偣采97,第2頭母牛總共生了94,第3頭母牛總共生了91,依此,所以100年絕對(duì)不會(huì)超過1到100的和5050=(100+1)*100/2哦,最多也只是97+94+91+...+1=(97+1)*32/2=736頭,不知是否認(rèn)同

論壇徽章:
0
103 [報(bào)告]
發(fā)表于 2003-08-21 16:55 |只看該作者

母牛數(shù)量算法

[quote]原帖由 "flw"]呵呵。我的答案不一定是效率最高的,但是理解起來絕對(duì)是最簡(jiǎn)單的,而且結(jié)果也絕對(duì)是正確的。凡是和我的結(jié)果不同的,當(dāng)然就是錯(cuò)的。[/quote 發(fā)表:
     
剛才編譯了一下老大用 c++ 鏈表寫的,里面有一處小問題,我用的是 g++ ,改正過來了,同時(shí)改動(dòng)了一下 main () 好讓它接受參數(shù),不要介意,以下是改好的代碼:

  1. // 修改自 cow_flw.cpp
  2. # include <stdio.h>;
  3. # include <iostream>;
  4. # define YEAR 100

  5. class cow
  6. {
  7. public:
  8.    static cow *head;
  9.    static cow *tail;
  10.    static unsigned long cowCount;

  11.    int age;
  12.    cow *next;

  13.    cow()
  14.    {
  15.       age = 0;
  16.       next = ( cow * ) NULL;
  17.    }

  18.    void grow(void)
  19.    {
  20.       age++;
  21.       if ( age >;= 4 )
  22.       {
  23.          cow *child = new cow;
  24.          tail->;next = child;
  25.          tail = child;
  26.          cowCount++;
  27.       }
  28.    }
  29. };

  30. unsigned long cow::cowCount = 0;
  31. cow *cow::head = NULL;
  32. cow *cow::tail = NULL;


  33. int main( char argc, char **argv)
  34. {
  35.         int years;
  36.         if ( argc == 1)
  37.                 years = 10;      // 默認(rèn)計(jì)算 10 年
  38.         else {
  39.                 if ( argc >;= 2 && atoi(argv [1]) >;= 0 && atoi(argv [1]) <= YEAR)
  40.                 {
  41.                 years = atoi(argv [1]);
  42.                 printf ("year: %d\n", years);
  43.                 }

  44.                 else {
  45.                         printf ("年數(shù)錯(cuò)誤,請(qǐng)保持在 0 ~ 100 范圍內(nèi)的整數(shù)\n");
  46.                         return 0;
  47.                 }
  48.         }


  49.    cow *cows = new cow;
  50.    cow::head = cows;
  51.    cow::tail = cows;
  52.    cow::cowCount = 1;


  53.    cow *ptr = cow::head;  // 指針 ptr 在此處定義
  54.    for( int i=0; i<years; i++ )
  55.       for( ptr = cow::head; ptr != NULL; ptr = ptr->;next )  // 這里改動(dòng)了一下,指針定義我拿到了嵌套外部
  56.          ptr->;grow();

  57.    printf( "result: [%ld]\n", cow::cowCount );

  58.    ptr = cow::head;
  59.    while( ptr != NULL )
  60.    {
  61.       cow *temp = ptr;
  62.       ptr = ptr->;next;
  63.       delete temp;
  64.    }

  65.    return 1;
  66. }
復(fù)制代碼


flw 兄的約定是整整 3 周歲,就是 365 * 3 天,而我的是 4 周歲,這樣的話,我的代碼調(diào)整如下(修改兩個(gè)數(shù)字):

  1. #include <stdio.h>;
  2. #define YEAR 1000       // 計(jì)算到 1000 年,自行修改

  3. int main( char argc, char **argv)
  4. {
  5.         int year;
  6.         if ( argc == 1)
  7.                 year = 10;      // 默認(rèn)計(jì)算 10 年
  8.         else {
  9.                 if ( argc >;= 2 && atoi(argv [1]) >;= 0 && atoi(argv [1]) <= YEAR)
  10.                 {
  11.                 year = atoi(argv [1]);
  12.                 printf ("year: %d\n", year);
  13.                 }
  14.                 else {
  15.                         printf ("年數(shù)錯(cuò)誤,請(qǐng)保持在 0 ~ 1000 范圍內(nèi)的整數(shù)\n");
  16.                         return 0;
  17.                 }
  18.         }

  19.         int born [YEAR] = {0};          // 存儲(chǔ)該年出生奶牛數(shù)目
  20.         unsigned long count = 0;        // 到 n 年為止全部母牛總數(shù)

  21.         int i;
  22.         for ( i = 0; i <= year; i++) {  // 計(jì)算該年出生奶牛數(shù)目
  23.                 if ( i <= 2 )  // 這里由 3 改成 2
  24.                         born [i] = (i == 0) ? 1 : 0;
  25.                 else
  26.                         born [i] = born [i - 1] + born [ i -3 ];  // 這里由 4 改成 3
  27.         }
  28.         for ( i = 0; i <= year; i++) {
  29.                 printf ("%3d ", i);
  30.                 int j;
  31.                 for ( j = 0; j < born[i]; j++)
  32.                         printf ("*");
  33.                 printf ("\n");
  34.                 count += born [i];
  35.         }
  36.         printf ("total cow number after %d year: %lu\n", year, count);

  37.         return 0;
  38. }      
復(fù)制代碼


計(jì)算結(jié)果和 flw 的結(jié)果剛好相差一年,也就是說:
我的 10 年是 28 頭;
flw 11 剛好 28 頭;
以此類推,除此之外完全一樣。
我思考了一下,主要是對(duì)第一年的理解可能有所不同,我是算每年的年初產(chǎn)牛,具體誰對(duì)誰錯(cuò),還沒細(xì)想。

這里提到效率問題:
用鏈表操作,越到下面,效率低的可憐,要是計(jì)算 100 年,怕是等不了,內(nèi)存不夠大,估計(jì)可能還會(huì)死機(jī),不過這里用到 c++ 面象對(duì)象的思想還是很不錯(cuò)的。


算法其實(shí)很簡(jiǎn)單,就兩個(gè)循環(huán),一個(gè)計(jì)算每年出生的牛數(shù),另外一個(gè)用來統(tǒng)計(jì)總的牛數(shù)。理解上,只要你照我寫的耐心看一下,只會(huì)比鏈表來的簡(jiǎn)單。

論壇徽章:
0
104 [報(bào)告]
發(fā)表于 2003-08-22 00:02 |只看該作者

母牛數(shù)量算法

如果約定 3 周歲開始生育:
每年農(nóng)場(chǎng)出生的牛數(shù)為 f(n):

  1.         1 , n = 0
  2. f(n) =  0 , n = 1 || 2
  3.         f(n - 1) + f(n - 3) n >;= 3
復(fù)制代碼

  1. f(0) = 1
  2. f(1) = 0
  3. f(2) = 0
  4. f(3) = f(2) + f(0)
  5. f(4) = f(3) + f(1)
  6. f(5) = f(4) + f(2)
  7. ......
  8. f(n-1) = f(n-2) + f(n-4)
  9. f(n) = f(n-1) + f(n-3)

  10. 把左邊一列相加得到母牛的總數(shù)目:
  11. Sum(n) = f(0) + f(1) +...+ f(n)

  12. 等號(hào)右邊第一列相加,把最后一列也加起來,剛好得到以下式子:
  13. Sum(n) = Sum(n - 1) + Sum(n - 3)

  14. 加上初始條件:
  15. Sum(0) = Sum(1) = Sum(2) = 1
復(fù)制代碼


我想這個(gè)式子應(yīng)該不陌生了,跟樓上幾位朋友推出來的一樣吧!
可以這樣理解:
n-1 年的母牛總數(shù)到了 n 年,一頭沒少,全活得好好的,而且它們中處于生育年齡母牛們還生了不少小寶寶,數(shù)目剛好等于相隔三年前母牛的總數(shù)目。

論壇徽章:
1
榮譽(yù)版主
日期:2011-11-23 16:44:17
105 [報(bào)告]
發(fā)表于 2003-08-22 08:25 |只看該作者

母牛數(shù)量算法

我暈,現(xiàn)在這帖子還被頂了上來,也沒有什么新的算法,還是原先討論過的算法在不停的貼代碼。

論壇徽章:
0
106 [報(bào)告]
發(fā)表于 2003-08-22 08:29 |只看該作者

母牛數(shù)量算法

[quote]原帖由 "aero"]我暈,現(xiàn)在這帖子還被頂了上來,也沒有什么新的算法,還是原先討論過的算法在不停的貼代碼。[/quote 發(fā)表:
     

呵呵,就這帖子,比清茶齋還熱鬧。

論壇徽章:
0
107 [報(bào)告]
發(fā)表于 2003-08-26 10:01 |只看該作者

母牛數(shù)量算法

這是遞歸調(diào)用的好題啊。
是精華!

論壇徽章:
0
108 [報(bào)告]
發(fā)表于 2003-08-27 05:01 |只看該作者

母牛數(shù)量算法

大家好,我是新手(C&linux)近來看此題,工作閑暇與幾位同事閑聊得出一結(jié)果:16637075746565964頭牛!!!有點(diǎn)夸張了吧???
對(duì)題目首先進(jìn)行假設(shè)處理:每頭牛壽命無限
解題思路:首先,同樣受前帖子解題思想的影響考慮用遞歸,但發(fā)現(xiàn)就單純從牛繁殖且用年份度量的話,很短時(shí)間后,牛的數(shù)目的增長(zhǎng)已經(jīng)不單單是線性指數(shù)關(guān)系了所以先否定了遞歸調(diào)用+牛繁殖且用年份度量的解題思路。
          其次,對(duì)核裂變公式的推敲更加認(rèn)識(shí)到用線性時(shí)間變量去度量以高階指數(shù)形式改變數(shù)量的解題思路關(guān)鍵在公式的形成,因?yàn)樵俸谩⒃俪暗挠布步?jīng)不住N(線性時(shí)間變量)的考驗(yàn)。!
          所以認(rèn)識(shí)到就此題所設(shè),及我自身數(shù)學(xué)水平有限而采用以下方法與步驟:
         1、不采用線性時(shí)間變量,而采用牛的生產(chǎn)周期時(shí)間做變量(關(guān)鍵);
         2、用電子表格,劃分4個(gè)階段(列,注:牛的生產(chǎn)周期)羅列100年(行),觀察數(shù)據(jù)變化推出基本可行公式;
         3、用for循環(huán)實(shí)現(xiàn)。

附程序(草)
  

     main()
{
               double a,a1,a2,a3;/*分別為:生產(chǎn)牛,1年牛,2年牛,3年牛*/
                double temp,sum;
               int i;
               a=1;a1=1;a2=1;a3=1;/*第6年(共4頭牛)各周期的牛*/
                for(i=7;i<=100;i++)
                {
                        a=(a+a3);
                        a1=a;
                        temp=a2;
                        a2=(a-a3);
                        a3=temp;
                 }
                   sum=a+a1+a2+a3;
                  printf("sum=%f\n",sum);
}

論壇徽章:
0
109 [報(bào)告]
發(fā)表于 2003-08-27 21:57 |只看該作者

母牛數(shù)量算法

剛剛來到這兒,就看到這么一篇帖子,受益匪淺!
以后會(huì)常來,還請(qǐng)各位老大關(guān)照!

論壇徽章:
0
110 [報(bào)告]
發(fā)表于 2003-08-31 15:45 |只看該作者

母牛數(shù)量算法

性能最好,最容易理解的解決方法上場(chǎng)了。
#define AGE_1 0  //一歲的牛
#define AGE_2 1 //二歲的牛
#define AGE_3 2 //三歲的牛
#define AGE_4 3 //大于或者等于四歲的牛,可以生牛的牛;
int main(void)
{
     int cow[4];
    int years = 0;

   //初始化,只有一頭一歲的母牛;
   cow[AGE_1] = 1;
    cow[AGE_2] = 1;
    cow[AGE_3] = 1;
    cow[AGE_4] = 1;

    printf("input Years? ";
    scanf("%d", years);
    for (int i=1; i<=years; i++)
    {
     //三歲的豬都長(zhǎng)了一歲,所以放到四歲的數(shù)組中來;
         cow[AGE_4] += cow[AGE_3];
     //2歲的豬也長(zhǎng)了一歲,放到三歲的數(shù)組中來;
        cow[AGE_3] = cow[AGE_2];
     //1歲的豬也長(zhǎng)了一歲,放到二歲的數(shù)組中來;
     cow[AGE_2] = cow[AGE_3];
     //1歲的豬是由今年四歲的和大于四歲的豬生的;
     cow[AGE_1] = cow[AGE_4];
    }
    printf("all cow is : %d", cow[0] + cow[1] + cow[2] + cow[3]);
}

不好意思,一直以為是豬,應(yīng)該是牛,呵呵。。。


其他的幾種不大讓我滿意的解決方法:

solution1:
#include <stdio.h>;
#include <stdlib.h>;

int total_n(int year)
{
    if (year <= 3) return 1;
    if (year == 4) return 2;
    return total_n(year-1)+total_n(year-3);
}

int main(void)
{
   printf("50 years go by, total pigs: %d\n", total_n(50));
   return 1;
}

solution2:
#include <iostream.h>;

struct pig
{
    int age;
//    int mother;
    int children;
};
int count_pigs(int year)
{
    int i, j, k, m;
    int count, count_tmp;
    pig pigs[102400000];
    //init;
    i=j=k=m=0;
    count=count_tmp=1;
    for (i=0; i<10240; i++)
    {
        pigs.age=0;
//        pigs.mother=0;
        pigs.children=0;
    }
    //years go by...;
    for(i=1; i<=year; i++)
    {
        cout << "現(xiàn)在是第" << i << "年:" << endl;
        for (j=0; j<count_tmp; j++)
        {
            pigs[j].age++;
//            cout << "    第" << j << "頭豬的年齡是:" << pigs[j].age << endl;
//            cout << "        她共生了" << pigs[j].children << "頭小豬" << endl;
            if (pigs[j].age >;= 4)
            {
                pigs[j].children++;
                pigs[count++].age=1;
//                pigs[count-1].mother=j;
                pigs[count-1].children=0;
//                cout <<"            她今年生了一頭小豬" << count -1 << endl;
            }
        }
        count_tmp=count;
        cout << "\t" << "第" << i << "年豬的總數(shù): " << count << endl;
    }
    cout << "total pigs: " << count << endl;
    return count;
}
int main(void)
{
    int years=0;
    //get year;
    cout << "input years: ";
    cin >;>; years;
    count_pigs(years);
}

solution3:
#include <stdio.h>;
#define n 70
struct year{
int yearnum;
long pignum;
};
struct year yearslot[n];
int main(){
yearslot[0].yearnum=1;
yearslot[0].pignum=1;
for(int i=1;i<n;i++)
{
long num=0;
for(int j=0;j<=i-3;j++)
{
        num+=yearslot[j].pignum;
}
printf("year %d's pig num = %ul\n",i+1,num);
yearslot.pignum=num;
yearslot.yearnum=1;

for(int j=0;j<i;j++)
yearslot[j].yearnum++;
}
long num=0;
for(int i=0;i<n;i++)
num+=yearslot.pignum;
printf("all pig is:%ul\n",num);
}
您需要登錄后才可以回帖 登錄 | 注冊(cè)

本版積分規(guī)則 發(fā)表回復(fù)

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號(hào)-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號(hào):11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報(bào)專區(qū)
中國互聯(lián)網(wǎng)協(xié)會(huì)會(huì)員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請(qǐng)注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP