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 <iostream>
#include <iomanip>
#include <string>
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<j;i++,j--)
  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<len1&&i<len2;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<len1)
 {
  for(;i<len1;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<len2)
 {
  for(;i<len2;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<<answer<<endl;
 //cout<<s3[i]<<endl;
 //for(int j=i-1;j>=0;j--)cout<<s3[j];
 //cout<<endl;
}

 

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了 搞不懂...

1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合;、下 4载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合;、下载 4使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合;、下载 4使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值