hdu1230火星A+B (大数题)

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

用大数的方法做 一位一位分开求 最后倒叙输出

#include<iostream>
#include<cstdio>
#include<cstring>

using namespace std;

int hash[9999];
int sir[500];
char ch1[1000],ch2[1000];
int num1[100],num2[100],n1[100],n2[100],sum[100];

int main()
{
    int k=1;
    for(int i=2;i<=9999;i++)
    {
        if(!hash[i])
        {
            sir[k++]=i;
            for(int j=i+i;j<=9999;j+=i)
            hash[j]=1;
        }
    }

    while(scanf("%s%s",ch1,ch2)!=EOF)
    {

        if(strcmp(ch1,"0")==0&&strcmp(ch2,"0")==0)
        break;
        int k1=1,k2=1;
        num1[1]=num2[1]=0;
        for(int i=0;i<strlen(ch1);i++)
        {
            if(ch1[i]==‘,‘)
            {
                k1++;
                num1[k1]=0;
                continue;
            }
            num1[k1]=num1[k1]*10+ch1[i]-‘0‘;
            //cout<<num1[k1];
        }
        //cout<<endl;
        for(int i=0;i<strlen(ch2);i++)
        {
            if(ch2[i]==‘,‘)
            {
                k2++;
                num2[k2]=0;
                continue;
            }
            num2[k2]=num2[k2]*10+ch2[i]-‘0‘;
            //cout<<num2[k2];
        }
        //cout<<endl;

        memset(n1,0,sizeof(n1));
        memset(n2,0,sizeof(n2));
        for(int i=k1;i>0;i--)
        n1[k1-i+1]=num1[i];
        for(int i=k2;i>0;i--)
        n2[k2-i+1]=num2[i];

        //for(int i=1;i<=k1;i++)
        //printf("%d,",n1[i]);

        int K=k1>k2?k1:k2;
        for(int i=1;i<=K;i++)
        {
            if(i==1)
            sum[i]=n1[i]+n2[i];
            else
            {
                if(sum[i-1]>=sir[i-1])
                sum[i]=n1[i]+n2[i]+1;
                else
                sum[i]=n1[i]+n2[i];
            }
            //cout<<sum[i]<<endl;
        }
        if(sum[K]>=sir[K])
        sum[++K]=1;
        for(int i=K;i>0;i--)
        {
            if(i==K)
            {
                printf("%d",sum[i]%sir[i]);
            }
            else
            printf(",%d",sum[i]%sir[i]);
        }
        printf("\n");
    }
    return 0;
}
时间: 2024-10-30 07:15:06

hdu1230火星A+B (大数题)的相关文章

hdu1230 火星A+B

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1230 题目类型: 模拟 题意概括: 第i位数的进制为第i个素数,求两个这样的数之和 解题思路: 根据给的范围,先将前29位素数打表出来,然后通过大数加法的方法即可 题目: 火星A+B Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 1289

HDU-1041-Computer Transformation(规律题 &amp;&amp; 大数题)

Computer Transformation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6543    Accepted Submission(s): 2378 Problem Description A sequence consisting of one digit, the number 1 is initially wr

hdu1002 A + B Problem II(大数题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1002 A + B Problem II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 230247    Accepted Submission(s): 44185 Problem Description I have a very sim

oj 1002题 (大数题)

#include <stdio.h> #include <string.h> int main(void) { int q,j,h,k,l; int d; char s1[1001],s2[1001];//题目要求位数不大于1000 scanf("%d",&h); for(l=1;l<=h;l++){ int c[1001]={0}, a[1001]={0},b[1001]={0};//这样可以令数组内全部元素为0 scanf("%s %

HDU2054_A == B ?【模拟题】【大数】【水的问题】

A == B ? Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 64243    Accepted Submission(s): 10061 Problem Description Give you two numbers A and B, if A is equal to B, you should print "YES"

HDU2054_A == B ?【模拟题】【大数】【水题】

A == B ? Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 64243    Accepted Submission(s): 10061 Problem Description Give you two numbers A and B, if A is equal to B, you should print "YES"

大菲波数(Fibonacci)java大数(hdu1715)

大菲波数 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 11520 Accepted Submission(s): 3911 Problem Description Fibonacci数列,定义如下:f(1)=f(2)=1f(n)=f(n-1)+f(n-2) n>=3.计算第n项Fibonacci数值. Input 输入第一行为一个整数N,接

大数运算(python2)

偶然又遇到了一道大数题,据说python大数运算好屌,试了一发,果然方便- 1 a = int( raw_input() ); //注意这里是按行读入的,即每行只读一个数 2 b = int( raw_input() ); 3 print a+b; 4 print a*b; 5 print a/b; 6 print a%b;

poj 大数 - 2602 Superlong sums

这应该算是最简单的大数题了...目的就是为了让你知道char的输入输出比int快很多,还学会了getchar(),putchar(). #include<stdio.h> #define MAX 1000002 char num[MAX],num1[MAX]; int main(){ int n,i; scanf("%d",&n); getchar(); for(i=1;i<=n;i++){ num[i] = getchar(); getchar(); num