转载的别人的文章
火星A+B
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 12162 Accepted Submission(s): 4103
Problem Description
读入两个不超过25位的火星正整数A和B,计算A+B。需要注意的是:在火星上,整数不是单一进制的,第n位的进制就是第n个素数。例如:地球上的10进制数2,在火星上记为“1,0”,因为火星个位数是2进制的;地球上的10进制数38,在火星上记为“1,1,1,0”,因为火星个位数是2进制的,十位数是3进制的,百位数是5进制的,千位数是7进制的……
Input
测试输入包含若干测试用例,每个测试用例占一行,包含两个火星正整数A和B,火星整数的相邻两位数用逗号分隔,A和B之间有一个空格间隔。当A或B为0时输入结束,相应的结果不要输出。
Output
对每个测试用例输出1行,即火星表示法的A+B的值。
Sample Input
1,0 2,1
4,2,0 1,2,0
1 10,6,4,2,1
0 0
Sample Output
1,0,1
1,1,1,0
1,0,0,0,0,0
Source
Recommend
JGShining | We have carefully selected several similar problems for you:
1235
1228
1205
1720
1215
Statistic |
Submit |
Discuss |
Note
细心点 把,分割的数字读取出来就行了
- #include <stdio.h>
- #include <string.h>
- #include <stack>
- #include <math.h>
- using namespace std;
- int main()
- {
- char num1[1000];
- char num2[1000];
- int prim[26]={0,2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97};
- while(~scanf("%s %s",num1,num2))
- {
- if(strcmp(num1,"0")==0&&strcmp(num2,"0")==0)
- break;
- int len1=strlen(num1);
- int len2=strlen(num2);
- int t1=len1-1,t2=len2-1;
- stack<int>s;
- int r=0;
- int cnt=1;
- while(t1>=0||t2>=0)
- {
- int mul=0;
- int a=0,b=0;
- for(;t1>=0;t1--)
- {
- if(num1[t1]>='0'&&num1[t1]<='9')
- a=a+(num1[t1]-'0')*pow(10,mul++);
- else
- break;
- }
- mul=0;
- for(;t2>=0;t2--)
- {
- if(num2[t2]>='0'&&num2[t2]<='9')
- b=b+(num2[t2]-'0')*pow(10,mul++);
- else
- break;
- }
-
-
-
- s.push((a+b+r)%prim[cnt]);
- r=(a+b+r)/prim[cnt];
- t1--,t2--,cnt++;
- }
- if(r)
- s.push(r);
- printf("%d",s.top());s.pop();
- while(!s.empty())
- printf(",%d",s.top()),s.pop();
- printf("\n");
- }
- }