#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;}
思路 :
n
n
n%
d
d
d是n在d进制下最后一位数,也就是在d进制下的个位,在翻转后,这位应该是第一位,将翻转后的结果变成十进制时是要从最高位开始做。这样就将最后三步化为了一步。
语法 :逗号表达式的值是最后一个的值;
N
N
N最大是
1
0
5
10^5
105,那么如果是二进制的话就会有十几位,十五六位,所以得用
l
o
n
g
l
o
n
g
long long
longlong存转换之后的结果。
#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;}}}