HDU 1230 火星A+B

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1230

水题模拟一道,主要考验代码能力,刷完题就感觉自己还是太弱了。

  1 #include<cmath>
  2 #include<cstdio>
  3 #include<cstring>
  4 #include<iostream>
  5 #include<algorithm>
  6 using namespace std;
  7 char a[100],b[100];
  8 int L,now,prime[27],A[26],B[26];
  9 bool f[100];
 10 void init()
 11 {
 12     int k,N=1,j=4;
 13     memset(f,false,sizeof(f));
 14     for(int i=2;N<=26&&i<=101;i++)
 15     {
 16         // cout<<i<<' '<<f[i]<<endl;
 17         if(f[i]==false)
 18         {
 19             prime[N]=i;
 20             N=N+1;
 21             for(j=i+i;j<=100;j=j+i){                f[j]=true;            } 
 22         }
 23         else
 24             continue;
 25     }
 26     /* for(int i=1;i<=25;i++)
 27         cout<<prime[i]<<' ';
 28     cout<<endl; */
 29     return;
 30 }
 31 void atoA(int x)
 32 {
 33     L++;
 34     int num=0;
 35     for(;a[now]!=','&&a[now]!='';now++)
 36     {
 37         num=num*10+a[now]-'0';
 38     }
 39     if(a[now++]!='')
 40         atoA(x+1);
 41     A[L-x]=num;
 42     return;
 43 }
 44 void btoB(int x)
 45 {
 46     L++;
 47     int num=0;
 48     for(;b[now]!=','&&b[now]!='';now++)
 49     {
 50         num=num*10+b[now]-'0';
 51     }
 52     if(b[now++]!='')
 53         btoB(x+1);
 54     B[L-x]=num;
 55     return;
 56 }
 57 int main()
 58 {
 59     init();
 60     int i,l,ans[100];
 61     while(scanf("%s %s",a,b))
 62     {
 63         if(strcmp(a,"0")==0||strcmp(b,"0")==0)
 64             break;
 65         memset(A,0,sizeof(A));
 66         memset(B,0,sizeof(B));
 67         now=0;L=0;
 68         atoA(0);
 69         l=L;
 70         now=0;L=0;
 71         btoB(0);
 72         if(l<L)
 73             l=L;
 74         int J=0;
 75         for(i=1;i<=l;i++)
 76         {
 77             ans[i]=A[i]+B[i]+J;
 78             J=ans[i]/prime[i];
 79             ans[i]%=prime[i];
 80         }
 81         while(J)
 82         {
 83             l++;
 84             ans[l]=J%prime[l];
 85             J=J/prime[l];
 86         }
 87         bool first=true;
 88         for(i=l;i>0;i--)
 89         {
 90             if(first)
 91             {
 92                 first=false;
 93                 printf("%d",ans[i]);
 94             }
 95             else
 96                 printf(",%d",ans[i]);
 97         }
 98         cout<<endl;
 99     }
100     return 0;
101 }
View Code
原文地址:https://www.cnblogs.com/wuwing/p/3465741.html