Created
May 23, 2013 22:38
-
-
Save bchetty/5640023 to your computer and use it in GitHub Desktop.
GCJ 2008 - Alien Numbers
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
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