#include<iostream>#include<algorithm>usingnamespace std;typedeflonglong ll;intget(char c){if(c <='9')return c -'0';return c -'a'+10;}ll calc(string n, ll r){ll res =0;for(auto c : n){if((double)res * r +get(c)>1e16)return1e18;res = res * r +get(c);}return res;}intmain(){string n1, n2;cin >> n1 >> n2;int tag, radix;cin >> tag >> radix;if(tag ==2)swap(n1, n2);ll target =calc(n1, radix);ll l =0, r = target +1;// ll l = 0, r = max(target, 36ll);for(auto c : n2) l =max(l,(ll)get(c)+1);while(l < r){ll mid = l + r >>1;if(calc(n2, mid)>= target) r = mid;// == problem!else l = mid +1;}if(calc(n2, r)== target) cout << r;else cout <<"Impossible";return0;}
#include<iostream>usingnamespace std;typedeflonglong LL;boolis_prime(int n){if(n ==1)returnfalse;for(int i =2; i * i <= n; i ++)if(n % i ==0)returnfalse;returntrue;}boolcheck(int n,int d){if(!is_prime(n))returnfalse;LL r =0;while(n){r = r * d + n % d;n /= d;}returnis_prime(r);}intmain(){int n, d;while(cin >> n >> d, n >=1){if(check(n, d))puts("Yes");elseputs("No");}return0;}
#include<iostream>#include<vector>usingnamespace std;vector<int> nums;boolcheck(){for(int i =0, j = nums.size()-1; i < j; i ++, j --)if(nums[i]!= nums[j])returnfalse;returntrue;}intmain(){int n, b;cin >> n >> b;if(!n) nums.push_back(0);while(n) nums.push_back(n % b), n /= b;if(check())puts("Yes");elseputs("No");cout << nums.back();for(int i = nums.size()-2; i >=0; i --) cout <<' '<< nums[i];return0;}
#include<iostream>usingnamespace std;int a[3];charget(int x){if(x >9)return'A'+ x -10;return'0'+ x;}intmain(){for(int i =0; i <3; i ++) cin >> a[i];cout <<'#';for(int i =0; i <3; i ++) cout <<get(a[i]/13)<<get(a[i]%13);return0;}
1100 Mars Numbers (20 分)
思路 :重要的一點是高進制和低進制的英文單詞沒有重復,所以可以直接連接在同一個數組中
語法 :stringstream頭文件為sstream
#include<iostream>#include<sstream>usingnamespace std;char names[][5]={"tret","jan","feb","mar","apr","may","jun","jly","aug","sep","oct","nov","dec","tam","hel","maa","huh","tou","kes","hei","elo","syy","lok","mer","jou"};intget(string word){for(int i =0; i <25; i ++)if(names[i]== word){if(i <13)return i;return13*(i -12);}return-1;// 一定不會執行}intmain(){int T;cin >> T;getchar();while(T --){string line;getline(cin, line);stringstream ssin(line);if(line[0]<='9'){int v;ssin >> v;if(v <13) cout << names[v]<< endl;else{cout << names[12+ v /13];if(v %13!=0) cout <<" "<< names[v %13];cout << endl;}}else{string word;int res =0;while(ssin >> word) res +=get(word);cout << res << endl;}}}