zoj1205

Martian Addition
Time Limit: 1 Second      Memory Limit: 32768 KB

  In the 22nd Century, scientists have discovered intelligent residents live on the Mars. Martians are very fond of mathematics. Every year, they would hold an Arithmetic Contest on Mars (ACM). The task of the contest is to calculate the sum of two 100-digit numbers, and the winner is the one who uses least time. This year they also invite people on Earth to join the contest.
  As the only delegate of Earth, you're sent to Mars to demonstrate the power of mankind. Fortunately you have taken your laptop computer with you which can help you do the job quickly. Now the remaining problem is only to write a short program to calculate the sum of 2 given numbers. However, before you begin to program, you remember that the Martians use a 20-based number system as they usually have 20 fingers.

Input:
You're given several pairs of Martian numbers, each number on a line.
Martian number consists of digits from 0 to 9, and lower case letters from a to j (lower case letters starting from a to present 10, 11, ..., 19).
The length of the given number is never greater than 100.

Output:
For each pair of numbers, write the sum of the 2 numbers in a single line.

Sample Input:

1234567890
abcdefghij
99999jjjjj
9999900001


Sample Output:

bdfi02467j
iiiij00000

#include
#include
#include
using namespace std;

 

void swap(char &s1,char &s2)
{
 char temp;
 temp=s1;
 s1=s2;
 s2=temp;
}
void convert(string& s)
{
 int len=s.size();
 for(int i=0,j=len-1;i  swap(s[i],s[j]);
}
int change(char c)
{
 if(c>='0'&&c<='9')return c-'0';
 else return c-'a'+10;
}
char rechange(int n)
{
 if(n>=0&&n<=9)return (char)n+'0';
 else return (char)n+'a'-10;
}
void add(string s1,string s2,char* s3)
{
 int i;
 convert(s1);
 convert(s2);
 int len1=s1.size();
 int len2=s2.size();
 int sum;
 int tag=0;
 for(i=0;i {
  sum=change(s1[i])+change(s2[i])+tag;
  if(sum>=20)s3[i]=rechange(sum-20),tag=1;
  else s3[i]=rechange(sum),tag=0;
 }
 if(i {
  for(;i  {
   sum=change(s1[i])+tag;
   if(sum>=20)s3[i]=rechange(sum-20),tag=1;
   else s3[i]=rechange(sum),tag=0;
  }
 }
 if(i {
  for(;i  {
   sum=change(s2[i])+tag;
   if(sum>=20)s3[i]=rechange(sum-20),tag=1;
   else s3[i]=rechange(sum),tag=0;
  }
 }
 if(tag)s3[i]=rechange(1),s3[i+1]='/0';
 else s3[i]='/0';
 string answer(s3);
 convert(answer);
 cout< //cout< //for(int j=i-1;j>=0;j--)cout< //cout<}

 

int main()
{
 //freopen("in.txt","r",stdin);
 string s1,s2;
 char ans[105];
 while(cin>>s1>>s2)add(s1,s2,ans);
 
 return 0;
}

 

很郁闷,觉得没出错,可还是WA  后来网上找了个版本,输出都一样,在提交什么都没改 又AC了 搞不懂...

你可能感兴趣的:(numbers,string,input,each,output,pair)