uva 343 What Base Is This?

来源:互联网 发布:联合程序员开发网 编辑:程序博客网 时间:2024/06/01 09:48

题目:给了两个数,分别可以转成2-36的任意进制,输出相等时的进制。如果都不可以,输出都不行

注意:1  刚开始把两个数存到了两个char数组。刚开始我设置两个数组长度都是10。多次RA后才发现一些很长的就读不进去了。这让我想到看很多大佬的代码。把数组长度define成M,来控制,也可能是为了避免这种问题


2. 题干中有一句话很考验英语“ The bases associatedwith X and Y will be between 1 and 36 (inclusive), and as noted above, need not be the same for X andY . In representing these numbers the digits 0 through 9 have their usual decimal interpretations. ”  注意那个inclusive是放在36后面的。不对!根本就没有1进制。唉。智商呀。所以如果输入两个0,应该是 0 (base 2) = 0 (base 2) ,而不是 0 (base 1) = 0 (base 1)



3 写代码时,一步步的进行,用注释符把代码分成不同的模块


#include <cstdio>#include <string.h>#include <cstdlib>#include <cmath>#include <ctgmath>#include <iostream>#include <vector>#include <algorithm>#include <map>using namespace std;int main(){    char m[50],n[50];//定的太小会出错    while(cin>>m>>n){                //先求出m,n中最大的数字        int max_m,max_n;        max_m = max_n = 0;        int temp = 0;        for(int i = 0; i < strlen(m); i ++){            if((m[i] >= '0') && (m[i] <= '9')){                temp = (int)m[i] - 48;            }            else temp = (int)m[i] - 55;                        if(temp > max_m) max_m = temp;        }        //   cout<<max_m;        for(int i = 0; i < strlen(n); i ++){            if((n[i] >= '0') && (n[i] <= '9')){                temp = (int)n[i] - 48;            }            else temp = (int)n[i] - 55;                        if(temp > max_n) max_n = temp;        }        //  cout<<max_n;                //0 0情况做特殊处理,因为进制是从2到36的不包括1        if(max_n==0 && max_m==0) cout<<"0 (base 2) = 0 (base 2)"<<endl;        else{            //判断            int flag = 0;            for(int i = max_m+1;i<=36 && flag == 0;i++){ //跳出双重循环的技巧!在外层循环中加上一个flag!                for(int j = max_n+1; j<=36; j++){                    if (strtol(m, NULL, i) == strtol(n, NULL, j)) {                        printf("%s (base %d) = %s (base %d)\n",m,i,n,j);                        flag = 1;                        break;                                            }                }                            }                        if (flag == 0) {                printf("%s is not equal to %s in any base 2..36\n",m,n);            }        }                memset(m, 0, sizeof(m));        memset(n, 0, sizeof(n));    }    return 0;}


0 0