来源: http://acm.hdu.edu.cn/showproblem.php?pid=1230
1,0 2,1 4,2,0 1,2,0 1 10,6,4,2,1 0 0
1,0,1 1,1,1,0 1,0,0,0,0,0
题意: 略
题解: 大数问题。用一个数组ans 存放各个数位的进制,两个数组N1 N2分别保存两个火星数。在编程实现方面,主要是开头的输入需要一点技巧。
AC代码:
#include<iostream>
#include<cstring>
#include<cmath>
#include<cstdio>
using namespace std;
bool prime(int k){
if(k==2||k==3)
return true;
for(int i=2;i<=(int)sqrt(float(k));i++)
if(k%i==0) return false;
return true;
}
const int Max=55;
int num1[Max],num2[Max],N1[Max],N2[Max],posx=0,posy=0,temp;
int ans[60+5];
char ch;
int main(){
int tpos=0;
for(int i=2;tpos<56;i++)
if(prime(i)) ans[tpos++]=i;
while(1){
posx=0,posy=0;
memset(N1,0,sizeof(N1)); memset(N2,0,sizeof(N2));
while(1){
scanf("%d",&temp);
ch=getchar();num1[posx++]=temp;
if(ch==' ')break;
}
while(1){
scanf("%d",&temp); ch=getchar();
num2[posy++]=temp;
if(ch=='\n')break;
}
//posx++; posy++;
if((posx==1&&!num1[0])||(posy==1&&!num2[0]))
return 0;
for(int i=posx-1;i>=0;i--)
N1[posx-i-1]=num1[i];
for(int i=posy-1;i>=0;i--)
N2[posy-i-1]=num2[i];
for(int i=0;i<Max-2;i++){
N1[i]+=N2[i];
if(N1[i]>=ans[i]){
N1[i+1]++;
N1[i]%=ans[i];
}
}
int k;
for(k=Max-3;k>0&&!N1[k];)k--;
for(;k>0;k--) printf("%d,",N1[k]);
printf("%d\n",N1[0]);
}
}
原文地址:http://blog.csdn.net/mummyding/article/details/38486547