hdu1715
http://acm.hdu.edu.cn/showproblem.php?pid=1715
大数模拟,原以为大数已经很熟了,但是今天一打又出现问题,具体看代码
1 #include<stdio.h>
2 #include<memory.h>
3 int add(int num1[],int len,int num2[],int answer[])
4 {
5 int k=0,c=0;
6 for(int i=0;i<len;i++)
7 answer[k++]=(num1[i]+num2[i]+c)%10000,c=(num1[i]+num2[i]+c)/10000;
8 while(c)
9 answer[k++]=c%10000,c/=10000; //这里采用每个数组元素存放4位数,具体问题可以具体选取合适的存放位数
10 return k;
11 }
12 void cpy(int num[],int len,int answer[])
13 {
14 for(int i=0;i<len;i++) num[i]=answer[i];
15 }
16 int main()
17 {
18 int sum;
19 scanf("%d",&sum);
20 while(sum--)
21 {
22 int num;
23 scanf("%d",&num);
24 if(num<3) printf("1\n");
25 else
26 {
27 int num1[100],num2[100],answer[100],len=1;
28 memset(num1,0,sizeof(num1));
29 memset(num2,0,sizeof(num2));
30 memset(answer,0,sizeof(answer));
31 num1[0]=num2[0]=1;
32 num-=2;
33 while(num)
34 {
35 len=add(num1,len,num2,answer);
36 cpy(num2,len,num1);
37 cpy(num1,len,answer);
38 num--;
39 }
40 printf("%d",num1[len-1]); //先输出第一个数以不置0的方式
41 for(int i=len-2;i>=0;i--)
42 printf("%04d",num1[i]); //以后输出时不足4为以0补之,此处wa了好几次
43 printf("\n");
44 }
45 }
46 return 0;
47 }