免费注册 查看新帖 |

Chinaunix

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

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

论坛徽章:
0
1 [报告]
发表于 2003-08-07 16:50 |显示全部楼层

母牛数量算法

我也做了一个:

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

  3. #define MAX_STRING_LEN                128

  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.                 switch(i)
  19.                 {
  20.                         case 1:
  21.                         case 2:
  22.                         case 3:
  23.                                 strcpy(szCowCount, "1");
  24.                         break;
  25.                         case 4:
  26.                                 strcpy(szCowCount, "2");
  27.                         break;
  28.                         case 5:
  29.                                 strcpy(szCowCount, "3");
  30.                                 strcpy(szCount3, "1");
  31.                                 strcpy(szCount2, "1");
  32.                                 strcpy(szCount1, "1");
  33.                         break;
  34.                         default:
  35.                                 if (strlen(szCowCount) >;= MAX_STRING_LEN)
  36.                                         goto OUT;
  37.                                 strcpy(szLastCount3, szCount3);
  38.                                 Add(szCowCount, szCount3, szCowCount);
  39.                                 //szCowCount = szLastCowCount + szCount3;
  40.                                 Add(szCount3, szCount2, szCount3);
  41.                                 //szCount3 += szCount2;
  42.                                 strcpy(szCount2, szCount1);
  43.                                 strcpy(szCount1, szLastCount3);
  44.                         break;
  45.                 }
  46.         }
  47. OUT:       
  48.         printf("Yead:[%d]; Cow number:[%s].\n", iYears, szCowCount);
  49. }



  50. //
  51. //  字符串数据相加.
  52. //
  53. static void Add(const char *pcFirst, const char *pcSecond, char *pcAdd)
  54. {
  55.         char        szFirstAdd[MAX_STRING_LEN];
  56.         char        szAdd[MAX_STRING_LEN];
  57.         int                i;
  58.         int                iFirst;
  59.         int                iSecond;
  60.         int                iAdd;
  61.         int                iFirstLen;
  62.         int                iSecondLen;
  63.         int                iMinLen;
  64.         int                iOver = 0;
  65.         int                n;
  66.        
  67.         iFirstLen = strlen(pcFirst);
  68.         iSecondLen = strlen(pcSecond);
  69.         if (iFirstLen < iSecondLen)
  70.                 iMinLen = iFirstLen;
  71.         else
  72.                 iMinLen = iSecondLen;
  73.         for (i = 0; i < iMinLen; ++i)
  74.         {
  75.                 szFirstAdd[i] = pcFirst[iFirstLen - i - 1] - 48 + pcSecond[iSecondLen - i - 1] + iOver;

  76.                 if (szFirstAdd[i] - 48 >;= 10)
  77.                 {
  78.                         szFirstAdd[i] = (szFirstAdd[i] - 48) % 10 + 48;
  79.                         iOver = 1;
  80.                 }
  81.                 else
  82.                         iOver = 0;
  83.         }
  84.         if (iFirstLen >; iMinLen)
  85.         {
  86.                 for (; i < iFirstLen; ++i)
  87.                 {
  88.                         szFirstAdd[i] = pcFirst[iFirstLen - i - 1] + iOver;
  89.                 if (szFirstAdd[i] - 48 >;= 10)
  90.                 {
  91.                         szFirstAdd[i] = (szFirstAdd[i] - 48) % 10 + 48;
  92.                         iOver = 1;
  93.                 }
  94.                 else
  95.                         iOver = 0;
  96.                 }
  97.         }
  98.         else
  99.         {
  100.                 for (; i < iSecondLen; ++i)
  101.                 {
  102.                         szFirstAdd[i] = pcSecond[iSecondLen - i - 1] + iOver;
  103.                 if (szFirstAdd[i] - 48 >;= 10)
  104.                 {
  105.                         szFirstAdd[i] = (szFirstAdd[i] - 48) % 10 + 48;
  106.                         iOver = 1;
  107.                 }
  108.                 else
  109.                         iOver = 0;
  110.                 }
  111.         }
  112.         if (iOver == 1)
  113.                 szFirstAdd[i++] = '1';
  114.        
  115.         for (n = 0; n < i; ++n)
  116.                 szAdd[i - n - 1] = szFirstAdd[n];
  117.         szAdd[i] = '\0';
  118.         strcpy(pcAdd, szAdd);
  119.        
  120.         return;
  121. }
复制代码

测试了几个跟flw兄的结果一致,不知道数字大了会不会错,请各位测试一下!
要是没有问题的话下次来讨论算法!!!

论坛徽章:
0
2 [报告]
发表于 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
3 [报告]
发表于 2003-08-08 09:01 |显示全部楼层

母牛数量算法

原来算法前面已经有了!!!
白忙了这么久!
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP