网站建议:179001057@qq.com

09GCJ r1-3rd-A all your base

技术2022-05-11  1

// 2009 GCJ round1-3rd-A: all your base #include <stdio.h> #include <iostream> #include <math.h> using namespace std; int main(){ int T; scanf("%d", &T); char ch; scanf("%c", &ch); int i; for(i = 0; i < T; i++){ char s[65]; gets(s); int base = 0; bool hash[40]; int len = strlen(s); int k, val; memset(hash, 0, 40); for(k = 0; k < len; k++){ if( s[k] >= '0' && s[k] <='9'){ val = s[k] - '0'; if(hash[val] == 0){ hash[val] = 1; base++; } }else{ val = s[k] - 'a' + 10; if(hash[val] == 0){ hash[val] = 1; base++; } } } if(1 == base) base++; int code[65]; long long num[65]; for(k = 0; k < 65; k++){ code[k] = -1; } num[0] = 1; k = 0; if( s[k] >= '0' && s[k] <='9'){ val = s[k] - '0'; }else{ val = s[k] - 'a' + 10; } code[val] = 1; int next = 0; for(k = 1; k < len; k++){ if( s[k] >= '0' && s[k] <='9'){ val = s[k] - '0'; }else{ val = s[k] - 'a' + 10; } if(code[val] != -1){ num[k] = code[val]; }else{ code[val] = next; num[k] = code[val]; if(0 == next) next=2; else next ++; } } long long sum = 0; for(k = 0; k < len; k++){ sum *= (long long)base; sum += (long long)num[k]; //long long exp = (long long)ceil( ( long double)pow( (long double)base, (long double)(len - k -1)) ); //sum += num[k] * (long long)exp; // 如果用这2行,就会导致计算错误。 } printf("Case #%d: %lld/n", i+1, sum); } // system("pause"); return 1; }


最新回复(0)