Skip to content

Instantly share code, notes, and snippets.

@swimmi
Created April 21, 2014 16:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save swimmi/11147114 to your computer and use it in GitHub Desktop.
Save swimmi/11147114 to your computer and use it in GitHub Desktop.
Calculate the sum of 2 numbers in Mars
#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