nyoj 1277 Decimal integer conversion

来源:互联网 发布:网络文件服务器软件 编辑:程序博客网 时间:2024/06/05 06:13
XiaoMing likes mathematics, and he is just learning how to convert numbers between different bases , but he keeps making errors since he is only 6 years old. Whenever XiaoMing converts a number to a new base and writes down the result, he always writes one of the digits wrong. For example , if he converts the number 14 into binary (i.e., base 2), the correct result should be "1110", but he might instead write down "0110" or "1111". XiaoMing never accidentally adds or deletes digits, so he might write down a number with a leading digit of " 0" if this is the digit she gets wrong. Given XiaoMing 's output when converting a number N into base 2 and base 3, please determine the correct original value of N (in base 10). (N<=10^10) You can assume N is at most 1 billion, and that there is a unique solution for N. 
输入
The first line of the input contains one integers T, which is the nember of test cases (1<=T<=8)
Each test case specifies:
* Line 1: The base-2 representation of N , with one digit written incorrectly.
* Line 2: The base-3 representation of N , with one digit written incorrectly.
输出
For each test case generate a single line containing a single integer , the correct value of N
样例输入
11010212
样例输出

14

题意:有一个十进制的数,给你两个序列,一个是它的二进制,一个是它的三进制,但是它的二进制和三进制都有一位是错的,问你这个数是n.

思路:两个循环,改变两个序列的每一位,判断得到的连个十进制数是否相同。

#include<iostream>#include<cstdio>#include<cstring>using namespace std;int text(char a[],char b[]){    if(a[0]=='0' || b[0]=='0')//第一位不可以是0        return 0;    int ans1=0,ans2=0;    int i,j;    j=1;    for(i=strlen(a)-1;i>=0;i--,j*=2){        ans1+=(a[i]-'0')*j;//得到换一位之后的二进制数得到的十进制的数    }    j=1;    for(i=strlen(b)-1;i>=0;i--,j*=3)        ans2+=(b[i]-'0')*j;//
得到换一位之后的三进制数得到的十进制的数
// printf("%d %d\n",ans1,ans2); if(ans1==ans2) return ans1; else return 0;}int main(){ int t; char s1[50],s2[50]; int l1,l2,i,j; int ans; int k; scanf("%d",&t); while(t--){ ans=0; scanf("%s%s",s1,s2); l1=strlen(s1); l2=strlen(s2); for(i=0;i<l1;i++){ if(ans) break;//因为得到答案的时候 循环还会进行 s1[i]=!(s1[i]-'0')+'0'; for(j=0;j<l2;j++){ if(ans) break; int temp=s2[j]-'0'; for(k=0;k<3;k++){//s2序列的三种可能 if(temp!=k){//除去本身的那种可能 s2[j]=k+'0'; ans=text(s1,s2); if(ans){ printf("%d\n",ans); break; } } } s2[j]=temp+'0'; } s1[i]=!(s1[i]-'0')+'0'; } }}



0 0