Created
April 21, 2014 16:02
-
-
Save swimmi/11147114 to your computer and use it in GitHub Desktop.
Calculate the sum of 2 numbers in Mars
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <fstream> | |
#include <iostream> | |
#include <string> | |
using namespace std; | |
//Calculate the sum of 2 numbers in Mars. | |
int main() | |
{ | |
//ifstream cin("martian_addition.txt"); | |
string a,b; | |
string s="0123456789abcdefghijk"; //more than 20 ! The key is 'k'. | |
while(cin>>a>>b) | |
{ | |
bool m=false; //more digits | |
while(a.size()!=b.size()) | |
{ | |
a.size()<b.size()?(a="0"+a):(b="0"+b); //equals the length. | |
} | |
a="0"+a;b="0"+b; //add '0' before | |
for(int i=a.size()-1;i>0;i--) | |
{ | |
int n=s.find(a[i])+s.find(b[i]); | |
if(n>=20&&i==1) | |
m=true; | |
a[i]=s[n%20]; | |
a[i-1]=s[(s.find(a[i-1])+n/20)]; | |
} | |
if(m) a[0]='1'; //add digit | |
if(!m) a=a.replace(0,1,""); //remove '0' | |
cout<<a<<endl; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment