免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
楼主: loveguohuasai
打印 上一主题 下一主题

[算法] 母牛数量算法 [复制链接]

论坛徽章:
0
81 [报告]
发表于 2003-08-08 05:18 |只看该作者

母牛数量算法

有这等事
很严重呀,要是改成“美女”真不知道会是什么样的呢~!

论坛徽章:
0
82 [报告]
发表于 2003-08-08 08:54 |只看该作者

母牛数量算法

修改了一下:

  1. #include <stdlib.h>;
  2. #include <stdio.h>;

  3. #define MAX_STRING_LEN                1024

  4. static void Add(const char *, const char *, char *);

  5. int main(int argc, char **argv)
  6. {
  7.         char        szCowCount[MAX_STRING_LEN];
  8.         char        szLastCount3[MAX_STRING_LEN];
  9.         char        szCount3[MAX_STRING_LEN];
  10.         char        szCount2[MAX_STRING_LEN];
  11.         char        szCount1[MAX_STRING_LEN];
  12.         int                iYears;
  13.         int                i;
  14.        
  15.         iYears = atoi(argv[1]);
  16.         for (i = 1; i <= iYears; ++i)
  17.         {
  18.                 if (i == 1)
  19.                 {
  20.                         strcpy(szCowCount, "1");
  21.                         strcpy(szCount3, "0");
  22.                         strcpy(szCount2, "0");
  23.                         strcpy(szCount1, "1");
  24.                 }
  25.                 else
  26.                 {
  27.                         if (strlen(szCowCount) >;= MAX_STRING_LEN)
  28.                                 goto OUT;
  29.                         strcpy(szLastCount3, szCount3);
  30.                         Add(szCowCount, szCount3, szCowCount);
  31.                         Add(szCount3, szCount2, szCount3);
  32.                         strcpy(szCount2, szCount1);
  33.                         strcpy(szCount1, szLastCount3);
  34.                 }
  35.         }
  36. OUT:       
  37.         printf("Yead:[%d]; Cow number:[%s].\n", i - 1, szCowCount);
  38. }



  39. //
  40. //  字符串数据相加.
  41. //
  42. static void Add(const char *pcFirst, const char *pcSecond, char *pcAdd)
  43. {
  44.         char        szFirstAdd[MAX_STRING_LEN];
  45.         char        szAdd[MAX_STRING_LEN];
  46.         int                i;
  47.         int                iFirst;
  48.         int                iSecond;
  49.         int                iAdd;
  50.         int                iFirstLen;
  51.         int                iSecondLen;
  52.         int                iMinLen;
  53.         int                iOver = 0;
  54.         int                n;
  55.        
  56.         iFirstLen = strlen(pcFirst);
  57.         iSecondLen = strlen(pcSecond);
  58.         if (iFirstLen < iSecondLen)
  59.                 iMinLen = iFirstLen;
  60.         else
  61.                 iMinLen = iSecondLen;
  62.         for (i = 0; i < iMinLen; ++i)
  63.         {
  64.                 szFirstAdd[i] = pcFirst[iFirstLen - i - 1] - 48 + pcSecond[iSecondLen - i - 1] + iOver;

  65.                 if (szFirstAdd[i] - 48 >;= 10)
  66.                 {
  67.                         szFirstAdd[i] = (szFirstAdd[i] - 48) % 10 + 48;
  68.                         iOver = 1;
  69.                 }
  70.                 else
  71.                         iOver = 0;
  72.         }
  73.         if (iFirstLen >; iMinLen)
  74.         {
  75.                 for (; i < iFirstLen; ++i)
  76.                 {
  77.                         szFirstAdd[i] = pcFirst[iFirstLen - i - 1] + iOver;
  78.                 if (szFirstAdd[i] - 48 >;= 10)
  79.                 {
  80.                         szFirstAdd[i] = (szFirstAdd[i] - 48) % 10 + 48;
  81.                         iOver = 1;
  82.                 }
  83.                 else
  84.                         iOver = 0;
  85.                 }
  86.         }
  87.         else
  88.         {
  89.                 for (; i < iSecondLen; ++i)
  90.                 {
  91.                         szFirstAdd[i] = pcSecond[iSecondLen - i - 1] + iOver;
  92.                 if (szFirstAdd[i] - 48 >;= 10)
  93.                 {
  94.                         szFirstAdd[i] = (szFirstAdd[i] - 48) % 10 + 48;
  95.                         iOver = 1;
  96.                 }
  97.                 else
  98.                         iOver = 0;
  99.                 }
  100.         }
  101.         if (iOver == 1)
  102.                 szFirstAdd[i++] = '1';
  103.        
  104.         for (n = 0; n < i; ++n)
  105.                 szAdd[i - n - 1] = szFirstAdd[n];
  106.         szAdd[i] = '\0';
  107.         strcpy(pcAdd, szAdd);
  108.        
  109.         return;
  110. }
复制代码


看来大家都没有仔细看我的程序。

论坛徽章:
0
83 [报告]
发表于 2003-08-08 09:01 |只看该作者

母牛数量算法

原来算法前面已经有了!!!
白忙了这么久!

论坛徽章:
0
84 [报告]
发表于 2003-08-08 15:39 |只看该作者

母牛数量算法

#include<math.h>;
#include<iostream>;
main()
{
int i,j,m;
int years=50;               //50 年
static unsigned long cow[41];  //牛只能活40年,分别为一岁的牛,二岁的牛...
static unsigned long cowCount;
for(i=1;i<41;i++)
{
cow=0;
}

cow[1]=5;

for (m=1;m<years+1;m++)
{
cowCount=0;
for(i=4;i<21;i++)           //牛到20岁就不生了
{
cowCount=cowCount+cow;
}

for(i=40;i>;1;i--)
{
cow=cow[i-1];
}
cow[1]=cowCount;
}
cowCount=0;
for(i=1;i<41;i++)
{
cowCount=cowCount+cow;
}
cout<<cowCount<<endl;

论坛徽章:
0
85 [报告]
发表于 2003-08-08 17:01 |只看该作者

母牛数量算法

  1. #include <stdlib.h>;
  2. #include <stdio.h>;
  3. #include <string.h>;
  4. #include <iostream.h>;
  5. #include <string>;
  6. #include <vector>;
  7. using namespace std;

  8. int cowgo(int num)
  9. {
  10.         int i;
  11.         int cow=0,baby1,baby2,baby3;
  12.         int temp;
  13.         cow=baby2=baby3=0;
  14.         baby1=1;
  15.         for (i=num;i>;0;i--)
  16.         {
  17.         temp=baby3;
  18.         baby3=baby2;
  19.         baby2=baby1;
  20.         baby1=cow;
  21.         cow=cow+temp;
  22.         }
  23. return cow+baby1+baby2+baby3;
  24. }


  25. main()
  26. {
  27. int i;
  28. scanf("%d",&i);
  29. cout<<cowgo(i)<<endl;
  30. }
复制代码

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
86 [报告]
发表于 2003-08-08 19:21 |只看该作者

母牛数量算法

呵呵,看来也没有什么新的算法了,继续关注。

论坛徽章:
0
87 [报告]
发表于 2003-08-08 19:49 |只看该作者

母牛数量算法

#include<math.h>;
#include<iostream>;
main()
{
int i,j,m;
int years=50; //50 年
float cow[41]; //牛只能活40年,分别为一岁的牛,二岁的牛...
float cowCount; //cow[],cowcolunt用float才能算到100,否则到70几就有益出了
for(i=1;i<41;i++)
{
cow=0;
}

cow[1]=5;

for (m=1;m<years+1;m++)
{
cowCount=0;
for(i=4;i<21;i++) //牛到20岁就不生了
{
cowCount=cowCount+cow;
}

for(i=40;i>;1;i--)
{
cow=cow[i-1];
}
cow[1]=cowCount;
}
cowCount=0;
for(i=1;i<41;i++)
{
cowCount=cowCount+cow;
}
cout<<cowCount<<endl;

论坛徽章:
0
88 [报告]
发表于 2003-08-08 23:51 |只看该作者

母牛数量算法


  1. #include<math.h>;
  2. #include<iostream.h>;

  3. #define ARRAY_LONG        50
  4. #define CHILD_AGE        4
  5. #define        MOTHER_AGE        20
  6. #define DIE_AGE                40


  7. //考虑到将来处理更大的规模,所以用模板
  8. template<class NUM>;
  9. class cow
  10. {
  11.         public:
  12.                 cow(int y)        {year=y;}
  13.                 int        work();
  14.                 int        show_population()
  15.                 {
  16.                         cout<<result;
  17.                         return 0;
  18.                 }

  19.         private:
  20.                 int        year;
  21.                 NUM        result;
  22.                 int        dec_pa(int &pa)
  23.                 {
  24.                         //循环数组退格
  25.                         pa += (ARRAY_LONG -1);
  26.                         pa %= ARRAY_LONG;
  27.                         return pa;
  28.                 }
  29. };

  30. template <class NUM>;
  31. int cow<NUM>;::work()
  32. {
  33.         int        pa_zero,
  34.                 pa_child,
  35.                 pa_mother,
  36.                 pa_die;
  37.         NUM        count_child,
  38.                 count_mother,
  39.                 count_grannie;
  40.         NUM        *cyc_queue = new NUM(ARRAY_LONG);
  41.        
  42.         for (int j =0 ;j<ARRAY_LONG ;j++)
  43.         {
  44.                 cyc_queue[j] = 0;
  45.         }

  46.         //c++ 自动对数据初始化为零值
  47.         cyc_queue[1] = 1;
  48.         pa_zero = 1;        //为了语义上的统一性,数组从1开始计数
  49.         pa_child = CHILD_AGE;
  50.         pa_mother = MOTHER_AGE;
  51.         pa_die = DIE_AGE;
  52.         count_child = 1;
  53.         count_mother = 0;
  54.         count_grannie =0;
  55.         //开始计数
  56.         for (int i=0 ;i<year ;i++)
  57.         {   
  58.                 //新的一年长一岁
  59.                 //循环数组向后退
  60.                 dec_pa(pa_zero);
  61.                 dec_pa(pa_child);
  62.                 dec_pa(pa_mother);
  63.                 dec_pa(pa_die);

  64.                 //年老的母牛死了:(
  65.                 count_grannie -= cyc_queue[pa_die];
  66.                 //到年龄的母牛不能生了,变奶奶了
  67.                 count_grannie += cyc_queue[pa_mother];
  68.                 count_mother -= cyc_queue[pa_mother];
  69.                 //小牛变成大母牛
  70.                 count_mother += cyc_queue[pa_child];
  71.                 count_child -= cyc_queue[pa_child];
  72.                 //小牛
  73.                 cyc_queue[pa_zero] = count_mother;//一只母牛生一只小牛
  74.                 count_child += cyc_queue[pa_zero];//新生的小牛加进去

  75.         }
  76.         //最后一共有几头?
  77.         //小牛+母牛+老牛
  78.         result = count_child + count_mother + count_grannie;
  79.         return 0;
  80. }

  81. main()
  82. {
  83.         int year;
  84.         cout<<"please input year:";
  85.         cin>;>;year;
  86.         cow<unsigned long>; foo(year);
  87.         foo.work();
  88.         foo.show_population();
  89.         return 0;
  90. }

复制代码

大家帮我排排错吧,反正结果和大家的程序不一样

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
89 [报告]
发表于 2003-08-09 12:05 |只看该作者

母牛数量算法

呵呵,好,还考虑到了牛的养老问题。

论坛徽章:
0
90 [报告]
发表于 2003-08-09 12:17 |只看该作者

母牛数量算法

faint, 怎么题目都变了,牛还有节育的,升天的,再下去要考虑10岁那年母牛改嫁,一年生了两。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP