码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2018-05-10 19:59:55      阅读:206      评论:0      收藏:0      [点我收藏+]

标签:结束   getch   rac   errors   for   style   put   rate   namespace   

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

 

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

标签:结束   getch   rac   errors   for   style   put   rate   namespace   

原文地址:https://www.cnblogs.com/nr1999/p/9021358.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!