1010 Radix

题目链接:https://pintia.cn/problem-sets/994805342720868352/problems/994805507225665536

进制转换,但是没有拿到满分,还有点小问题,下面是代码

 1 #include<iostream>
 2 #include<string.h>
 3 using namespace std;
 4 
 5 long trans(string num,int radix){
 6     int in[10] = {1};
 7     for(int i = 1; i < 10; i++){
 8         in[i] = in[i-1]*radix;
 9     }
10     int x = num.size();
11     long stem = 0;
12     for(int i = x-1; i >= 0; i--){
13         if(num[i] >= '0' && num[i] <= '9'){
14             stem += (int)(num[i]-'0')*in[x-i-1];
15         }
16         else{
17             stem += ((int)(num[i]-'a')+10)*in[x-i-1];
18         }
19     }
20     return stem;
21 }
22 int main(){
23     string str[2];
24     int n,radix;
25     std::ios::sync_with_stdio(false);
26     std::cin.tie(0);
27     int n_1 = 0;
28     while(cin >> str[0]){
29         cin >> str[1]>>n>>radix;
30         long stem = 0, stem_x = 0;
31         int max = 0,ste,radix_x = 0;
32         stem = trans(str[n-1],radix);
33         n_1 = str[2-n].size();
34         for(int i = 0; i < n_1; i++){
35             if(str[2-n][i] >= '0' && str[2-n][i] <= '9'){
36                 ste = (int)(str[2-n][i]-'0');
37             }
38             else{
39                 ste = (int)(str[2-n][i]-'a')+10;
40             }
41             if(ste > max) max = ste;
42         }
43         radix_x = max+1;
44         while(stem_x < stem){
45             stem_x = trans(str[2-n],radix_x);
46             radix_x++;
47         }
48         if(stem_x == stem) cout << radix_x-1 << endl;
49         else cout << "Impossible" << endl;
50     }
51     return 0;
52 }

 

posted @ 2018-10-08 16:21  琥琥笙威  阅读(115)  评论(0编辑  收藏  举报