百练2981 http://poj.grids.cn/practice/2981/
View Code
1 #include<stdio.h>
2 #include<string.h>
3 int num[301];
4 int main()
5 {
6 int i,j,f = 0,f1;
7 char c1[201],c2[201];
8 gets(c1);
9 gets(c2);
10 i = strlen(c1)-1;
11 j = strlen(c2)-1;
12 int g = 0;
13 while(i>=0&&j>=0)
14 {
15 g++;
16 if(c1[i]-'0'+c2[j]-'0'+num[g]>9)
17 {
18 num[g+1] += (c1[i]-'0'+c2[j]-'0'+num[g])/10;
19 num[g] = (c1[i]-'0'+c2[j]-'0'+num[g])%10;
20 f1 = 0;
21 }
22 else
23 num[g] += c1[i]-'0'+c2[j]-'0';
24 i--;j--;
25 }
26 while(i>=0)
27 {
28 g++;
29 if(num[g]+c1[i]-'0'>9)
30 {
31 num[g+1] += (num[g]+c1[i]-'0')/10;
32 num[g] = (num[g]+c1[i]-'0')%10;
33 }
34 else
35 num[g] += c1[i]-'0';
36 i--;
37 }
38 while(j>=0)
39 {
40 g++;
41 if(num[g]+c1[j]-'0'>9)
42 {
43 num[g+1] += (num[g]+c1[j]-'0')/10;
44 num[g] = (num[g]+c1[j]-'0')%10;
45 }
46 else
47 num[g] = num[g]+c1[j]-'0';
48 j--;
49 }
50 g++;
51 while(num[g]==0&&g)
52 g--;
53 for(i = g ; i>= 1; i--)
54 {
55 f = 1;
56 printf("%d",num[i]);
57 }
58 if(!f)
59 printf("0");
60 printf("\n");
61 return 0;
62 }
hdu 1002 http://acm.hdu.edu.cn/showproblem.php?pid=1002
加法 先字符串化整 直接相加 再进位
View Code
1 #include<stdio.h>
2 #include<string.h>
3 int main()
4 {
5 int i,j,k,t,a1[1001],a2[1001],g,f;
6 char c1[1001],c2[1001];
7 scanf("%d%*c",&t);
8 for(j = 1; j <= t ; j++)
9 {
10 scanf("%s %s",c1,c2);
11 memset(a1,0,sizeof(a1));
12 memset(a2,0,sizeof(a2));
13 f = 0;
14 for(i = 0 ; i < strlen(c1) ; i++)
15 a1[strlen(c1)-i-1] = c1[i]-'0';
16 for(i = 0 ; i < strlen(c2) ; i++)
17 a2[strlen(c2)-i-1] = c2[i]-'0';
18 if(strlen(c1)>strlen(c2))
19 k = strlen(c1);
20 else
21 k = strlen(c2);
22 for(i = 0 ; i < k ; i++)
23 a2[i] += a1[i];
24 for(i = 0 ; i < k ; i++)
25 if(a2[i]>9)
26 {
27 a2[i+1] += a2[i]/10;
28 a2[i] = a2[i]%10;
29 }
30 printf("Case %d:\n",j);
31 printf("%s + %s = ",c1,c2);
32 while(a2[i]==0&&i>=0)
33 i--;
34 for(g = i ; g >= 0 ; g--)
35 {
36 f = 1;
37 printf("%d",a2[g]);
38 }
39 if(!f)
40 printf("0");
41 printf("\n");
42 if(j!=t)
43 printf("\n");
44 }
45 return 0;
46 }
hdu1047 http://acm.hdu.edu.cn/showproblem.php?pid=1047
View Code
1 #include<stdio.h>
2 #include<string.h>
3 int main()
4 {
5 int i,j,t,num[101][201],toal[201],g;
6 char c[101][101];
7 scanf("%d%*c", &t);
8 while(t--)
9 {
10 memset(num,0,sizeof(num));
11 memset(toal,0,sizeof(toal));
12 i = 0;
13 int f = 0;
14 while(gets(c[i])!=NULL)
15 {
16 if(strcmp(c[i],"0")==0)
17 break;
18 i++;
19 }
20 int max = 0;
21 for(j = 0 ; j < i ; j++)
22 {
23 for(g = 0 ; g < strlen(c[j]) ; g++)
24 num[j][strlen(c[j])-g-1] = c[j][g]-'0';
25 if(strlen(c[j])>max)
26 max = strlen(c[j]);
27 }
28 for(g = 0 ; g < max ; g++)
29 for(j = 0 ; j < i ; j++)
30 toal[g]+=num[j][g];
31 for(g = 0 ; g < max+3 ; g++)
32 if(toal[g]>9)
33 {
34 toal[g+1] += toal[g]/10;
35 toal[g] = toal[g]%10;
36 }
37 while(toal[g]==0&&g>=0)
38 g--;
39 for(i = g ; i>=0 ; i--)
40 {
41 f = 1;
42 printf("%d",toal[i]);
43 }
44 if(!f)
45 printf("0");
46 printf("\n");
47 if(t!=0)
48 puts("");
49 }
50 return 0;
51 }
hdu 1753 http://acm.hdu.edu.cn/showproblem.php?pid=1753
小数的加法 化为整数的加法 注意小数位不一样时 少的补0 前导0和小数后面多余的0要去掉
View Code
1 #include<stdio.h>
2 #include<string.h>
3 int main()
4 {
5 int i,j,k,t,a1[1001],a2[1001],g,f,k1,k2,x,y,w;
6 char c1[1001],c2[1001];
7 while(scanf("%s %s",c1,c2)!=EOF)
8 {
9 memset(a1,0,sizeof(a1));
10 memset(a2,0,sizeof(a2));
11 k1 = strlen(c1);
12 k2 = strlen(c2);
13 w =0;f = 0;x=y=0;
14 for(i = 0 ; i < k1 ; i++)
15 if(c1[i]=='.')
16 {
17 w = 1;
18 x = k1-1-i;
19 for(j = i ; j < k1-1 ; j++)
20 c1[j] = c1[j+1];
21 break;
22 }
23 if(w)
24 k1--;
25 w = 0;
26 for(i = 0 ; i < k2 ; i++)
27 if(c2[i]=='.')
28 {
29 w = 1;
30 y = k2-1-i;
31 for(j = i ; j < k2-1 ; j++)
32 c2[j] = c2[j+1];
33 break;
34 }
35 if(w)
36 k2--;
37 int g1=0,g2=0;
38 if(x<y)
39 {
40 for(i = 0 ; i < y-x ; i++)
41 a1[g1++] = 0;
42 x = y;
43 }
44 else
45 for(i = 0 ; i < x-y ; i++)
46 a2[g2++] = 0;
47 for(i = k1-1 ; i >= 0 ; i--)
48 a1[g1++] = c1[i]-'0';
49 for(i = k2-1 ; i >= 0 ; i--)
50 a2[g2++] = c2[i]-'0';
51 if(g1>g2)
52 g2 = g1;
53 for(i = 0 ; i < g2 ; i++)
54 a2[i] += a1[i];
55 for(i = 0 ; i < g2 ; i++)
56 if(a2[i]>9)
57 {
58 a2[i+1] += a2[i]/10;
59 a2[i] = a2[i]%10;
60 }
61 while(a2[i]==0&&i>=0)
62 i--;
63 j = 0;
64 while(a2[j]==0&&j<x)
65 j++;
66 for(g = i ; g >= j ; g--)
67 {
68 f = 1;
69 if(g+1==x)
70 {
71 if(g==i)
72 printf("0");
73 printf(".");
74 }
75 printf("%d",a2[g]);
76 }
77 if(!f)
78 printf("0");
79 printf("\n");
80 }
81 return 0;
82 }
百练2736 http://poj.grids.cn/practice/2736/
减法 直接把第二个数都变成负数相加
View Code
1 #include<stdio.h>
2 #include<string.h>
3 int main()
4 {
5 int i,j,k,t,a1[1001],a2[1001],g,f;
6 char c1[1001],c2[1001];
7 scanf("%d%*c",&t);
8 for(j = 1; j <= t ; j++)
9 {
10 scanf("%s %s",c1,c2);
11 memset(a1,0,sizeof(a1));
12 memset(a2,0,sizeof(a2));
13 f = 0;
14 for(i = 0 ; i < strlen(c1) ; i++)
15 a1[strlen(c1)-i-1] = c1[i]-'0';
16 for(i = 0 ; i < strlen(c2) ; i++)
17 a2[strlen(c2)-i-1] = (c2[i]-'0')*-1;
18 if(strlen(c1)>strlen(c2))
19 k = strlen(c1);
20 else
21 k = strlen(c2);
22 for(i = 0 ; i < k ; i++)
23 a1[i] += a2[i];
24 for(i = 0 ; i < k ; i++)
25 if(a1[i]<0)
26 {
27 a1[i+1]--;
28 a1[i] = a1[i]+10;
29 }
30 while(a1[i]==0&&i>=0)
31 i--;
32 for(g = i ; g >= 0 ; g--)
33 {
34 f = 1;
35 printf("%d",a1[g]);
36 }
37 if(!f)
38 printf("0");
39 printf("\n");
40 }
41 return 0;
42 }
http://poj.org/problem?id=1001
化为整数相乘 再算出小数点在哪里
View Code
1 #include<stdio.h>
2 #include<string.h>
3 int num[1001];
4 int main()
5 {
6 int i,j,k,n,x,y,w,o,g,a;
7 char c[15],c1[1001],c2[1001];
8 while(scanf("%s %d", c,&n)!=EOF)
9 {
10 memset(num,0,sizeof(num));
11 memset(c2,'0',sizeof(c2));
12 k = strlen(c);
13 x = 0;
14 int f = 0;
15 for(i = 0 ; i < k ;i++)
16 if(c[i] == '.')
17 {
18 x = k-1-i;
19 for(j = i ; j < k-1 ; j++)
20 c[j] = c[j+1];
21 k--;
22 break;
23 }
24 c[k] = '\0';
25 o = 0;
26 x = n*x;
27 for(i = k-1 ; i >= 0 ; i--)
28 {
29
30 if(n==1)
31 c2[o++] = c[i];
32 else
33 c1[o++] = c[i];
34 }
35 for(i = 1; i < n ; i++)
36 {
37 w = 0;
38 memset(c2,'0',sizeof(c2));
39 for(j = k-1 ; j>=0 ; j--)
40 {
41 g = w;
42 for(y = 0 ; y< o ; y++)
43 {
44 if(c2[g]-'0'+(c[j]-'0')*(c1[y]-'0')>9)
45 {
46 c2[g+1]+=(c2[g]-'0'+(c[j]-'0')*(c1[y]-'0'))/10;
47 c2[g] = (c2[g]-'0'+(c[j]-'0')*(c1[y]-'0'))%10+'0';
48 }
49 else
50 c2[g]+= (c[j]-'0')*(c1[y]-'0');
51 g++;
52 }
53 w++;
54 }
55 if(c2[g]=='0')
56 o = g;
57 else
58 o = g+1;
59 for(a = 0 ; a < o ; a++)
60 c1[a] = c2[a];
61 }
62 o++;
63 while(c2[o]=='0'&&o>=x)
64 o--;
65 j = 0;
66 while(c2[j]=='0'&&j<x)
67 j++;
68 for(i = o ; i >= j ; i--)
69 {
70 f = 1;
71 if(i+1==x)
72 printf(".");
73 printf("%c",c2[i]);
74 }
75 if(!f)
76 printf("0");
77 printf("\n");
78 }
79 return 0;
80 }
来源:https://www.cnblogs.com/shangyu/archive/2012/07/25/2608565.html
