NYOJ 1277Decimal integer conversion (第九届河南省省赛)

本文介绍了一种通过已知但包含错误的二进制和三进制表示来确定原始十进制数值的方法。该方法涉及遍历所有可能的正确表示,并比较其对应的十进制值,最终找到匹配的正确答案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

 

题意多实例,然后每个样例给两行字符串分别是2进制的和3进制的,表示的是同一个十进制数。但是每个串都是有一个数是错的,让你通过这两个错的来找出呢个正确的十进制数。暴力找出所有情况的十进制数,然后一一对比相等说明这个十进制数就是正确的要求的数。

具体看代码,不算太难只不过中间一些小细节特别需要注意:

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<time.h>
#include<iostream>
#include<ctype.h>
#include<map>
#include<set>
#include<string>
#include<vector>
#include<algorithm>
#include<stdlib.h>
#include<queue>
#include<stack>
using namespace std;
int a[1001];
int b[1001];
char s[1001];
char s0[1001];
int h=0,k=0,len1,len2;
int f(int t)//将2进制转换为十进制存起来
{
    int i;
    int sum=0;
    for(i=0; i<=len1; i++)//主意为什么此处是小于等于,main函数有解释
    {
        sum+=pow(2,t)*(s[i]-'0');
        t--;
    }
    a[k++]=sum;
}
int ff(int t)//将3进制转换为十进制存起来
{
    int i,sum=0;
    for(i=0; i<=len2; i++)
    {
        sum+=pow(3,t)*(s0[i]-'0');
        t--;
    }
    b[h++]=sum;
}
int main()
{
    int t;
    char cc,cc2;
    int i,j;
   scanf("%d",&t);

         while(t--)
    {
        //getchar();

        scanf("%s",s);
        scanf("%s",s0);
        len1=strlen(s)-1;//这个地方本来想着减不减无所谓,循环的时候控制一下就行
        len2=strlen(s0)-1;//但是试了好几次,只有用这个,然后循环变成<=才能出数据,可能因为用char的话最后一位会自动赋值一个\0表示结束吧,然后中间运行会莫名出现问题,具体啥问题我也不清楚,但是很神奇,计算到前一位就过了
        h=0;
        k=0;
        for(i=0; i<=len1; i++)//列举2进制串所有可能情况
        {
            cc=s[i];
            if(s[i]=='0')
                s[i]='1';
            else
                s[i]='0';
            f(len1);
            s[i]=cc;
        }
        for(i=0; i<=len2; i++)//列举3进制串所有可能情况
        {
            cc2=s0[i];
            if(s0[i]=='0')
            {
                s0[i]='1';
                ff(len2);
                s0[i]='2';
                ff(len2);
            }
            else if(s0[i]=='1')
            {
                s0[i]='0';
                ff(len2);
                s0[i]='2';
                ff(len2);
            }
            else
            {
                s0[i]='0';
                ff(len2);
                s0[i]='1';
                ff(len2);
            }
            s0[i]=cc2;
        }
        for(i=0; i<k; i++)
        {
            for(j=0; j<h; j++)
            {
                if(a[i]==b[j])
                {
                    printf("%d\n",a[i]);
                    break;
                }
            }
        }
         memset(s,0,sizeof(s));
        memset(s0,0,sizeof(s0));
    }
}

 

转载于:https://www.cnblogs.com/nr1999/p/8998019.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值