convert numbers

来源:互联网 发布:天网塞班软件站 编辑:程序博客网 时间:2024/05/18 03:25

Description

XiaoMing likes mathematics, and heis just learning how to convert numbers between different

bases , but he keeps making errorssince he is only 6 years old. Whenever XiaoMing converts a

number to a new base and writes downthe result, he always writes one of the digits wrong.

For example , if he converts thenumber 14 into binary (i.e., base 2), the correct result should be

"1110", but he mightinstead write down "0110" or "1111". XiaoMing neveraccidentally adds or

deletes digits, so he might writedown a number with a leading digit of " 0" if this is the digit she

gets wrong.

Given XiaoMing 's output whenconverting a number N into base 2 and base 3, please determine

the correct original value of N (inbase 10). (N<=10^10)

You can assume N is at most 1billion, and that there is a unique solution for N.

Input

The first line of the input containsone integers T, which is the nember of test cases (1<=T<=8)

Each test case specifies:

* Line 1: The base-2 representationof N , with one digit written incorrectly.

* Line 2: The base-3 representationof N , with one digit written incorrectly.

Output

For each test case generate a singleline containing a single integer ,  the correct value of N

Sample Input

1 1010 212

Sample Output

14

 

给出一个二进制数a和一个三进制数b,每个数字中总会有一个数写错,求出对应的唯一十进制数

暴力,找出a,b的所以可能情况,然后比较若相同直接输出即可


#include<stdio.h>#include<string.h>#include<math.h>char str[1000],str1[1000];int m=0,n=0,a[1000],b[1000],len,len1;int f(int t){    int i,s=0;    for(i=0;i<=len;i++)    {        s+=pow(2,t)*(str[i]-'0');        t--;    }    a[m++]=s;}int ff(int t){    int i,s=0;    for(i=0;i<=len1;i++)    {        s+=pow(3,t)*(str1[i]-'0');        t--;    }    b[n++]=s;}int main(){    int t,i,j,s,k,k1;    char ch,ch1;    while(scanf("%d",&t)!=EOF)    {        while(t--)        {            scanf("%s",str);            scanf("%s",str1);            len=strlen(str);            len=len-1;            len1=strlen(str1);            len1=len1-1;            m=0,n=0;            for(i=0;i<=len;i++)            {                ch=str[i];                if(str[i]=='0')                    str[i]='1';                else                    str[i]='0';                f(len);                str[i]=ch;            }            for(i=0;i<=len1;i++)            {                ch1=str1[i];                if(str1[i]=='0')                {                    str1[i]='1';                    ff(len1);                    str1[i]='2';                    ff(len1);                }                else if(str1[i]=='1')                {                    str1[i]='0';                    ff(len1);                    str1[i]='2';                    ff(len1);                }                else                {                    str1[i]='0';                    ff(len1);                    str1[i]='1';                    ff(len1);                }                str1[i]=ch1;            }            /*for(i=0;i<m;i++)                printf("%d ",a[i]);            printf("\n");            for(i=0;i<n;i++)                printf("%d ",b[i]);            printf("\n");*/            for(i=0;i<m;i++)            {                for(j=0;j<n;j++)                {                    if(a[i]==b[j])                    {                        printf("%d\n",a[i]);                        break;                    }                }            }            memset(str,0,sizeof(str));            memset(str1,0,sizeof(str1));        }    }    return 0;}



原创粉丝点击