|
发表于 2021-3-12 10:47:37
|
显示全部楼层
- #include <stdio.h>
- #include <string.h>
- main()
- {
- char num1[1100] = "1", num2[1100] = "1", temp[1100];
-
- int i, j;
- int a, b, c, d, e = 0, f, count = 0;
- while (strlen(num2) < 1000)
- {
- strcpy(temp, num2);
- j = strlen(num1);
- for (i = 0; i < j; i++)//让num1和num2相加并存到num2中
- {
- a = num1[i] - '0';
- b = num2[i] - '0';
- c = a + b;
- d = (c + e) % 10;
- num2[i] = d + '0';
- e = (c + e)/ 10;
- }
- while (e)//此时还有进位
- {
- if (num2[i] != NULL)
- {
- f = num2[i] - '0';
- f += e;
- num2[i] = f + '0';
-
- }
- else
- {
- num2[i] = e + '0';
- }
- e /= 10;
- i++;
- }
- if (num2[i] == NULL)//添加结束符
- {
- num2[i] = '\0';
- }
- else
- {
- num2[i + 1] = '\0';
- }
- strcpy(num1, temp);//将上一个num2的值给num1
-
- count++;//记录项数
- }
-
- printf("\n%d\n", count + 2);
- }
复制代码
大佬帮我看看还有没有可以优化的地方,谢谢! |
|