您的位置:首页 > 其它

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

2010-02-02 23:11 736 查看
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: