(ZJU-2006复试)-ZOJ-2529-A+B in Hogwarts

本文介绍了一种基于哈利波特魔法世界背景的特殊加法问题,该加法使用不同的素数作为每一位的基数,实现非固定基数的大数加法运算。文章提供了详细的解析过程及C++实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

A+B in Hogwarts
Time Limit: 1 Second      Memory Limit: 32768 KB

If you are a fan of Harry Potter, you would know the school he is attending to, the Hogwarts School for Witchcraft and Wizardry. What you might not know is that Harry is never good at math, yet he doesn't want to bother Hermione for trivial A+B problems. Now your job is to write a simple calculator for him -- the calculator is so simple that it handles non-negative integer additions only.

What is not so simple is that the world of Witchcraft and Wizardry dosen't use a fixed radix numeration system. That is, an integer is represented with different radices for different digits -- the radix for the i-th digit is the i-th prime number. For example, the decimal number 2 is 10 in Hogwarts (let's denote it by 210 = 1,0H) since the radix for the 1st digit is the 1st prime number 2. Similarly we have 610 = 1,0,0Hsince the radix for the 2nd digit is 3.

Input

The input consists of multiple test cases, each occupies a line with two integers in Hogwarts' system. Digits of a Hogwarts' integer are seperated by ',' and the two numbers are seperated by a space. Each number has at most 25 digits.

Output

For each test case, print in one line the sum of the two given numbers in Hogwarts' system.

Sample Input

1,0 2,1
4,2,0 1,2,0
1 10,6,4,2,1

Sample Output

1,0,1
1,1,1,0
1,0,0,0,0,0

就是一个不同进制的大数加法。。可惜本人基本功不过关,这道题费了很多时间,这里是浙大上的题目,杭电上的输入输出稍有不同
代码用了一些不必要的变量
测试的时候考虑如下数据
0,0,0 0,0,0
0

0,1,0,1 0,0,1,1

1,2,0

 

96,88,82,78,72,70,66,60,58,52,46,42,40,36,30,28,22,18,16,12,10,6,4,2,1 96,88,82,78,72,70,66,60,58,52,46,42,40,36,30,28,22,18,16,12,10,6,4,2,1

1,96,88,82,78,72,70,66,60,58,52,46,42,40,36,30,28,22,18,16,12,10,6,4,2,0

 

 

#include <iostream>
#include <string>
#include <string.h>
#include <stdio.h>
#include <math.h>
using namespace std;

int prime[28]={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,101,103, 107};

int a[30],b[30];
int c[30];
int main()
{
    string ta,tb;
    while(cin>>ta>>tb)
    {    
        int temp=0,j=0,k=0;
        for(int i=ta.length()-1;i>=0;--i)
        {
            if(ta[i]==',')
            {
                a[k++]=temp;
                temp=0;
                j=0;
            }
            else
            {
                temp+=(ta[i]-48)*pow(10.0, j*1.0);
                ++j;
            }
        }
        a[k]=temp;
        int lena=k+1;
        temp=0;j=0;k=0;
        for(int i=tb.length()-1;i>=0;--i)
        {
            if(tb[i]==',')
            {
                b[k++]=temp;
                temp=0;
                j=0;
            }
            else
            {
                temp+=(tb[i]-48)*pow(10.0, j*1.0);
                ++j;
            }
        }
        b[k]=temp;
        int lenb=k+1;   
        int carry=0;
        int len=lena<lenb?lena:lenb;
        
        memset(c,0,sizeof(c));
        
        for(int i=0;i<len;++i)
        {
            c[i]=(carry+a[i]+b[i])%prime[i];
            carry=(carry+a[i]+b[i])/prime[i];
        }

        if(lena<lenb)
        {
            for(int i=lena;i<lenb;++i)
                if(carry)
                { 
                    c[i]=(carry+b[i])%prime[i];
                    carry=(carry+b[i])/prime[i];
                }
                else c[i]=b[i];
                
        }
        else
        {    
            for(int i=lenb;i<lena;++i)
                if(carry)
                { 
                    c[i]=(carry+a[i])%prime[i];
                    carry=(carry+a[i])/prime[i];
                }
                else c[i]=a[i];
        }
        len=lena>lenb?lena:lenb;
        while(carry)
        {
            c[len]=carry%prime[len];
            carry=carry/prime[len];
            ++len;
        }

        len--;
        while(c[len]==0&&len>0)len--;
        cout<<c[len];
        for(int i=len-1;i>=0;--i)
            cout<<","<<c[i];
        cout<<endl;
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值