1/**************************************
2Problem: HEU 5003 Total Amount
3Time: 0.0070 s
4Memory: 244 k
5Accepted Time: 2009-03-29 17:59:04
6Tips: 高精度计算
7**************************************/
8#include <stdio.h>
9#include <string.h>
10void add(char *a,char *b,char *c)
11{
12 int lena=strlen(a);
13 int lenb=strlen(b);
14 int lmax,lmin;
15 char *pmax,*pmin;
16 if(lena<lenb)
17 {
18 pmax=b;pmin=a;lmax=lenb;lmin=lena;
19 }
20 else
21 {
22 pmax=a;pmin=b;lmax=lena;lmin=lenb;
23 }
24 char *s=new char[lmax+2];
25 int i,j,k;
26 s[2]='0';
27 for(i=lmax-1,j=lmin-1,k=lmax+1;pmin[j]!='$';i--,j--,k--)
28 {
29 if(pmax[i]=='.'||pmax[i]==',')s[k]=pmax[i];
30 else s[k]=pmax[i]+pmin[j]-'0';
31 }
32 for(;pmax[i]!='$';i--,k--)s[k]=pmax[i];
33 for(k=lmax+1;k>2;)
34 {
35 if(s[k]!='.'&&s[k]!=','&&s[k]>'9')
36 {
37 s[k]-=10;
38 k--;
39 while(s[k]=='.'||s[k]==',')k--;
40 s[k]++;
41 }
42 else k--;
43 }
44 if(s[2]=='0')
45 {
46 s[2]='$';
47 strncpy(c,&s[2],lmax);
48 c[lmax]=0;
49 }
50 else
51 {
52 for(k=2;s[k]!=','&&s[k]!='.';k++);
53 if(k==6)
54 {
55 s[0]='$';
56 s[1]=s[2];
57 s[2]=',';
58 strncpy(c,s,lmax+2);
59 c[lmax+2]=0;
60 }
61 else
62 {
63 s[1]='$';
64 strncpy(c,&s[1],lmax+1);
65 c[lmax+1]=0;
66 }
67 }
68 delete []s;
69}
70int main()
71{
72 int n;
73 while(1)
74 {
75 scanf("%d",&n);
76 if(n==0)break;
77 char a[100],b[100];
78 if(n==1)
79 {
80 scanf("%s",b);
81 printf("%s\n",b);
82 continue;
83 }
84 for(int i=1;i<n;i++)
85 {
86 if(i==1)scanf("%s%s",a,b);
87 else scanf("%s",b);
88 add(a,b,a);
89 }
90 printf("%s\n",a);
91 }
92 return 0;
93}
94
2Problem: HEU 5003 Total Amount
3Time: 0.0070 s
4Memory: 244 k
5Accepted Time: 2009-03-29 17:59:04
6Tips: 高精度计算
7**************************************/
8#include <stdio.h>
9#include <string.h>
10void add(char *a,char *b,char *c)
11{
12 int lena=strlen(a);
13 int lenb=strlen(b);
14 int lmax,lmin;
15 char *pmax,*pmin;
16 if(lena<lenb)
17 {
18 pmax=b;pmin=a;lmax=lenb;lmin=lena;
19 }
20 else
21 {
22 pmax=a;pmin=b;lmax=lena;lmin=lenb;
23 }
24 char *s=new char[lmax+2];
25 int i,j,k;
26 s[2]='0';
27 for(i=lmax-1,j=lmin-1,k=lmax+1;pmin[j]!='$';i--,j--,k--)
28 {
29 if(pmax[i]=='.'||pmax[i]==',')s[k]=pmax[i];
30 else s[k]=pmax[i]+pmin[j]-'0';
31 }
32 for(;pmax[i]!='$';i--,k--)s[k]=pmax[i];
33 for(k=lmax+1;k>2;)
34 {
35 if(s[k]!='.'&&s[k]!=','&&s[k]>'9')
36 {
37 s[k]-=10;
38 k--;
39 while(s[k]=='.'||s[k]==',')k--;
40 s[k]++;
41 }
42 else k--;
43 }
44 if(s[2]=='0')
45 {
46 s[2]='$';
47 strncpy(c,&s[2],lmax);
48 c[lmax]=0;
49 }
50 else
51 {
52 for(k=2;s[k]!=','&&s[k]!='.';k++);
53 if(k==6)
54 {
55 s[0]='$';
56 s[1]=s[2];
57 s[2]=',';
58 strncpy(c,s,lmax+2);
59 c[lmax+2]=0;
60 }
61 else
62 {
63 s[1]='$';
64 strncpy(c,&s[1],lmax+1);
65 c[lmax+1]=0;
66 }
67 }
68 delete []s;
69}
70int main()
71{
72 int n;
73 while(1)
74 {
75 scanf("%d",&n);
76 if(n==0)break;
77 char a[100],b[100];
78 if(n==1)
79 {
80 scanf("%s",b);
81 printf("%s\n",b);
82 continue;
83 }
84 for(int i=1;i<n;i++)
85 {
86 if(i==1)scanf("%s%s",a,b);
87 else scanf("%s",b);
88 add(a,b,a);
89 }
90 printf("%s\n",a);
91 }
92 return 0;
93}
94
来源:https://www.cnblogs.com/main/archive/2009/04/02/1427988.html