题目1016:火星A+B(进制新问题)

题目链接:http://ac.jobdu.com/problem.php?pid=1016

详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus

参考代码:

//
//  1016 火星A+B.cpp
//  Jobdu
//
//  Created by PengFei_Zheng on 28/04/2017.
//  Copyright © 2017 PengFei_Zheng. All rights reserved.
//
 
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <cstring>
#include <cmath>
#define MAX_PRIME 30
#define MAX_SIZE 26
//#define debug
 
using namespace std;
 
int prime[MAX_PRIME];
 
bool isPrime(int x){
    if(x<=1) return false;
    if(x==2) return true;
    int tmp = sqrt(x)+1;
    for(int i = 2 ; i <= tmp ; i++){
        if(x%i==0)
            return false;
    }
    return true;
}
 
void calPrime(){
    int num = 0;
    for(int i = 2 ; num<25 ; i++){
        if(isPrime(i)){
            num++;
            prime[num] = i;
        }
    }
}
 
int a[MAX_SIZE];
int b[MAX_SIZE];
int sum[MAX_SIZE];
char c;
int lena, lenb, i, j;
 
void init(){
    memset(a, 0, sizeof(a));
    memset(b, 0, sizeof(b));
    memset(sum, 0, sizeof(sum));
    c=' ';
    lena=lenb=i=j=0;
}
int main(){
    calPrime();
#ifdef debug
    for(int i = 1 ; i < MAX_PRIME ; i++){
        cout<<i<<" is : "<<prime[i]<<endl;
    }
#endif
//    freopen("/Users/pengfei_zheng/Desktop/input.txt", "r", stdin);
    while(1){
        init();
        char c;
         
        for(i = 1 ; i < MAX_SIZE ; i++){
            scanf("%d",&a[i]);
            scanf("%c",&c);
            if(c==' ')break;
        }
        lena = i;
        for(j = 1 ; j < MAX_SIZE ; j++){
            scanf("%d",&b[j]);
            scanf("%c",&c);
            if(c=='\n')break;
        }
        lenb = j;
        if(a[1]==0 && b[1]==0)
            break;
         
        int maxLen = lena>=lenb ? lena : lenb;
         
        int carry=0;
        int len = maxLen;
        int x,y;
        for(i = 1 ; i <= maxLen; i++){
            if(lena>=1) x=a[lena--];
            else x=0;
            if(lenb>=1) y=b[lenb--];
            else y=0;
            sum[len--]=(x+y+carry)%prime[i];
            carry=(x+y+carry)/prime[i];
        }
        if(carry!=0){
            printf("%d,",carry);
        }
        for(i = 1 ; i < maxLen ; i++){
            printf("%d,",sum[i]);
        }
        printf("%d\n",sum[maxLen]);
    }
    return 0;
}
/**************************************************************
    Problem: 1016
    User: zpfbuaa
    Language: C++
    Result: Accepted
    Time:10 ms
    Memory:1532 kb
****************************************************************/

 

转载于:https://www.cnblogs.com/zpfbuaa/p/6783445.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值