`
amozon
  • 浏览: 134395 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Alien Numbers

阅读更多
Google Code Jam 2008 Practice one

http://code.google.com/codejam/contest/dashboard?c=agdjb2RlamFtcg4LEghjb250ZXN0cxh5DA

Arbitrary size numberal system conversion

1. numInputSystem[inputSystem[i]], inputSystem[i] is a char, actually converts to ASC2 in this array.
These 94 signals or numbers are consistent ASC2 codes.
2. Converts to decimal at first.

Java code:

public class CodeJam1 {

	public static void convertSystem(char[] alienNum, char[] inputSystem, char[] outputSystem) {
			int inputSize = inputSystem.length;
			int outputSize = outputSystem.length;
			
			int[] numInputSystem = new int[300];
			for(int i = 0;i < inputSize;i ++) {
				numInputSystem[inputSystem[i]] = i;
			}
			
			int x = 0;
			
			for(int i = 0;i < alienNum.length;i ++) {
				x = (x * inputSize) + numInputSystem[alienNum[i]];
			}
			
			//Decimal Number
			System.out.println(x);
			
			StringBuilder output = new StringBuilder();			
			while(x != 0) {
				StringBuilder temp = new StringBuilder();
				output = temp.append(outputSystem[x % outputSize]).append(output);
				x /= outputSize;
			}
			
			System.out.println("Case #1: " + output.toString());
	}
	
	public static void main(String[] args) {
		char[] alienNum = "CODE".toCharArray();
		char[] inputSystem = "O!CDE?".toCharArray();
		char[] outputSystem = "A?JM!.".toCharArray();
		
//		char[] alienNum = "2".toCharArray();
//		char[] inputSystem = "0123456789".toCharArray();
//		char[] outputSystem = "01".toCharArray();
		
//		char[] alienNum = "10".toCharArray();
//		char[] inputSystem = "01".toCharArray();
//		char[] outputSystem = "0123456789".toCharArray();
		
		CodeJam1.convertSystem(alienNum, inputSystem, outputSystem);
	}
}


C code
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdlib>
#include <vector>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <bitset>
#include <cctype>

#define int64 long long
#define ii pair<int,int>
#define vi vector<int>
#define mp make_pair
#define pb push_back
#define all(v) (v).begin(),(v).end()
#define sz(v) ((int)(v.size()))

using namespace std;

template<class T> T abs(T x){return x>0 ? x:(-x);}
template<class T> T sqr(T x){return x*x;}

int num1[300],num2[300];
int c1,c2;

int main()
{
	int n;
	cin >> n;
	for(int ic=0;ic<n;ic++){
		memset(num1,-1,sizeof(num1));
		string s,s1,s2;
		cin >> s >> s1 >> s2;
		for(int i=0;i<sz(s1);i++)
			num1[s1[i]]=i;
		for(int i=0;i<sz(s2);i++)
			num2[s2[i]]=i;
		c1=sz(s1);
		c2=sz(s2);
		int64 x=0;
		for(int i=0;i<sz(s);i++){
			if(num1[s[i]]==-1) while(1);
			x=(x*(int64)c1)+(int64)(num1[s[i]]);
		}
		string ss;
		if(!x) ss+=s2[0];
		while(x){
			ss=s2[x%c2]+ss;
			x/=c2;
		}
		cout << "Case #" << ic+1 << ": " << ss << "\n";
	}
	return 0;
}


2
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics