Description
Normally, you use base 10 to do arithmetic. In computer science, you also deal with binary (base 2), octal (base 8), and hex (base 16). In this problem, we'll worry about base 9, which uses the digits 0..8.
Input
each line of input will contain two numbers separated by a space. Each number is in base 9. If converted to base 10, the numbers would be greater than 0, and smaller than 65000.
Output
For each input, your program should add the two numbers together and print the result (in base 9).
Sample Input
148 765 111 888 8734 8345
Sample Output
1024 1110 18180
KEY:这题就是求和,只是他是9进制的就是了
Source:
#include<iostream>
using namespace std;
void Add(char str1[],char str2[])
...{
int la=strlen(str1),lb=strlen(str2);
int a[15],b[15];
int i,j;
for(i=0;i<=10;i++)
a[i]=b[i]=0;
for(i=0;i<la;i++)
a[i+1]=str1[i]-'0';
for(i=0;i<lb;i++)
b[i+1]=str2[i]-'0';
if(la>=lb)
...{
i=la;
j=lb;
while(j>0)
...{
a[i--]+=b[j--];
}
for(i=la;i>0;i--)
...{
int t;
t=a[i];
a[i-1]+=t/9;
a[i]=t%9;
}
if(a[0]==0) i=1;
else i=0;
for( ;i<=la;i++)
cout<<a[i];
cout<<endl;
}
else
...{
i=la;
j=lb;
while(i>0)
...{
b[j--]+=a[i--];
}
for(j=lb;j>0;j--)
...{
int t;
t=b[j];
b[j-1]+=t/9;
b[j]=t%9;
}
if(b[0]==0) j=1;
else j=0;
for( ;j<=lb;j++)
cout<<b[j];
cout<<endl;
}
}
int main()
...{
// freopen("fjnu_1178.in","r",stdin);
char str1[10],str2[10];
while(cin>>str1>>str2)
...{
Add(str1,str2);
}
return 0;
}
![]()