火星 A+B问题(研究生复试题)

  1. /*题目描述:  
  2.     读入两个不超过25位的火星正整数A和B,计算A+B。需要注意的是:在火星上,整数不是单一进制的,第n位的进制就是第n个素数。 
  3.     例如:地球上的10进制数2,在火星上记为“1,0”,因为火星个位数是2进制的;地球上的10进制数38,在火星上记为“1,1,1,0”, 
  4.     因为火星个位数是2进制的,十位数是3进制的,百位数是5进制的,千位数是7进制的…… 
  5. 输入:  
  6.     测试输入包含若干测试用例,每个测试用例占一行,包含两个火星正整数A和B,火星整数的相邻两位数用逗号分隔, 
  7.     A和B之间有一个空格间隔。当A或B为0时输入结束,相应的结果不要输出。 
  8. 输出:  
  9.     对每个测试用例输出1行,即火星表示法的A+B的值。 
  10. 样例输入:  
  11. 1,0 2,1 
  12. 4,2,0 1,2,0 
  13. 1 10,6,4,2,1 
  14. 0 0 
  15. 样例输出:  
  16. 1,0,1 
  17. 1,1,1,0 
  18. 1,0,0,0,0,0 
  19. */  
  20. #include <cstdio>   
  21. #include <iostream>   
  22. #include <string.h>   
  23. #include <cmath>   
  24. using namespace std;  
  25. bool check(int a)      //  Is it a prime number?   
  26. {  
  27.     int i;  
  28.     for(i=2;i<=sqrt(a);i++)  
  29.     {  
  30.         if(a%i==0)  
  31.             break;  
  32.     }  
  33.     if(i>sqrt(a))  
  34.         return true;  
  35.     else   
  36.         return false;  
  37. }  
  38. int main()  
  39. {  
  40.     int su[26];           //for storing the prime numbers   
  41.     int a[30],b[30],c[30];      //a+b   
  42.     char str[200];            
  43.     int i=0,j,s,t;  
  44.     int num=2;  
  45.     int len_a,len_b,len;  
  46.   
  47. //  freopen("f:/in.txt","r",stdin);   
  48.     while(i<25)  
  49.     {  
  50.         if(check(num))  
  51.         {  
  52.             su[i]=num;  
  53.             i++;  
  54.         }  
  55.         num++;  
  56.     }//save the prime number to array    
  57.   
  58.     while(gets(str))  
  59.     {  
  60.         memset(a,0,sizeof(a));  
  61.         memset(b,0,sizeof(b));  
  62.         memset(c,0,sizeof(c));  
  63.         len_a=len_b=0;  
  64.         len=strlen(str);  
  65.         for(i=0,s=0;i<len;i++)  
  66.         {  
  67.             if(str[i]==',')  
  68.             {  
  69.                 s++;  
  70.                 continue;  
  71.             }  
  72.             if(str[i]==' ')  
  73.             {  
  74.                 j=++i;  
  75.                 break;  
  76.             }  
  77.             a[s]=10*a[s]+str[i]-'0';  
  78.         }  //  读入A   
  79.   
  80.         for(t=0;j<len;j++)  
  81.         {  
  82.             if(str[j]==',')  
  83.             {  
  84.     //          printf("%d",b[t]);   
  85.                 t++;  
  86.                 continue;  
  87.             }  
  88.             if(str[j]=='\n')  
  89.                 break;  
  90.             b[t]=10*b[t]+str[j]-'0';  
  91.         }  // 读入B   
  92.   
  93.         if(a[0]==0 && b[0]==0)  
  94.             break;  
  95.   
  96.         for(i=0;s>=0 && t>=0 ;s--,t--,i++)  //A+B   
  97.         {  
  98.                 c[i+1]=(a[s]+b[t]+c[i])/su[i];  
  99.                 c[i]=(a[s]+b[t]+c[i])%su[i];  
  100.         }  
  101.   
  102.         while(s>=0)  
  103.         {  
  104.             c[i+1]=(a[s]+c[i])/su[i];  
  105.             c[i]=(a[s]+c[i])%su[i];  
  106.             i++;  
  107.             s--;  
  108.         }  
  109.         while(t>=0)  
  110.         {  
  111.             c[i+1]=(b[t]+c[i])/su[i];  
  112.             c[i]=(b[t]+c[i])%su[i];  
  113.             i++;  
  114.             t--;  
  115.         } //end of A+B   
  116.   
  117.         if(c[i]!=0)  //  output       the first number   
  118.             printf("%d",c[i--]);  
  119.         else  
  120.         {  
  121.             printf("%d",c[--i]);  
  122.             i--;  
  123.         }  
  124.   
  125.         while(i>=0)  
  126.             printf(",%d",c[i--]);  
  127.         printf("\n");  
  128.     }//end of output   
  129. //  fclose(stdin);   
  130.     return 0;  
  131. }  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值