火星A+B

题目:http://ac.jobdu.com/problem.php?pid=1016
有0,0,1   0,0,1 这种情况,开始没注意,结果Runtimeerror,在杭电上直接就WA,晕——

#include<algorithm>
#include <stdio.h>
#include <math.h>
#include<string.h>
using namespace std;
char a[600],b[600];
int as[600],bs[600],cs[600];
int prime[300];
int i,j;
void calprime(){
    int flag;
    int cont = 0;
    for(i =2;i < 200;i++){
       flag = 1;
       for(j =2; j < i;j++)
          if(i%j==0){
              flag = 0;
              break;
           }
       if(flag)
          prime[cont++] = i;
    }
}
int main(){
    while(~scanf("%s%s",a,b)){
       if(!strcmp(a,"0")||!strcmp(b,"0"))//有0,0,1 0,0,1这种情况
          break;
       calprime();
       memset(as,0,sizeof(as));
       memset(bs,0,sizeof(bs));
       memset(cs,0,sizeof(cs));
       int t,t1 =0,t2 =0;
       for(i =0;i < strlen(a); i++){
          if(a[i]>= '0'&& a[i]<= '9')
              as[t1] = 10*as[t1] + a[i] -'0';
          else if(a[i] ==','){
              t1++;
              continue;
           }
       }
       for(i =0;i < strlen(b); i++){
          if(b[i]>= '0'&& b[i]<= '9')
              bs[t2] = 10*bs[t2] + b[i] -'0';
          else if(b[i] ==','){
              t2++;
              continue;
           }
       }
       for(i =t1;i >=0; i--)
          cs[t1-i] += as[i];
       for(i =t2;i >=0; i--)
          cs[t2-i] += bs[i];
       t = max(t1,t2);
       int s =0;
       for(i =0; i <= t; i++){
           s =cs[i]/prime[i];
          cs[i] = cs[i]%prime[i];
          cs[i+1] +=s;
       }
       if(cs[t+1])
           t++;
       for(i =t; i >=0; i--){
          if(i)
              printf("%d,",cs[i]);
          else
              printf("%d\n",cs[i]);
       }
    }
    return 0;
}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值