convert numbers

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;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值