Skip to content

Instantly share code, notes, and snippets.

@bchetty
Created May 23, 2013 22:38
Show Gist options
  • Save bchetty/5640023 to your computer and use it in GitHub Desktop.
Save bchetty/5640023 to your computer and use it in GitHub Desktop.
GCJ 2008 - Alien Numbers
public class AlienNumbers {
public String convertFromOneNumberSystemToOther(String alienNum, String srcLang, String targetLang) {
int iSrcLangBaseNum = srcLang.trim().length();
int iTgtLangBaseNum = targetLang.trim().length();
int iAlienNumInDecSystem = 0;
int iAlienNumLen = alienNum.length();
for(int i=0;i<iAlienNumLen;i++) {
iAlienNumInDecSystem += srcLang.indexOf(alienNum.charAt(i)) * Math.pow(iSrcLangBaseNum,(iAlienNumLen-1-i));
}
StringBuffer sbTgtLangNum = new StringBuffer();
int tempNum = iAlienNumInDecSystem;
while(true) {
sbTgtLangNum.append(strTgtLang.charAt(tempNum % iTgtLangBaseNum));
if((tempNum/iTgtLangBaseNum) == 0) {
break;
} else {
tempNum /= iTgtLangBaseNum;
}
}
return sbTgtLangNum.reverse().toString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment