您的位置:首页 > 其它

NYOJ1277 Decimal integer conversion(模拟)(河南省第九届ACM省赛)

2017-04-17 15:39 429 查看
题目:


Decimal integer conversion

时间限制:1000 ms  |  内存限制:65535 KB
难度:2

描述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

样例输入
1
1010
212


样例输出
14


来源河南省第九届省赛
上传者onlinejudge
思路:
这个题先给了一行二进制数,又给了一行3进制数,这两个数表示的十进制数应该是相同的,但是他给的数中,2进制和3进制数中的某一位错了,所以你需要做的是求出这两个数原来表示的十进制的数是多少。。。
首先,我们先用两个数组求出这两个数所有可能的变换,然后依次遍历找出相同的数输出就行,具体看代码
代码:
#include <cstdio>
#include <cstring>
#include <cctype>
#include <string>
#include <set>
#include <iostream>
f245
#include <stack>
#include <cmath>
#include <queue>
#include <vector>
#include <algorithm>
#define mem(a,b) memset(a,b,sizeof(a))
#define inf 0x3f3f3f3f
#define mod 10000007
#define debug() puts("what the fuck!!!")
#define N 100+20
#define M 1000000+10
#define ll long long
using namespace std;
int a
,b
;
int main()
{
int t;
cin>>t;
while(t--)
{
mem(a,0);
mem(b,0);
string s1,s2;
cin>>s1>>s2;
for(int i=0; i<s1.length(); i++)
{
s1[i]=(s1[i]-'0'+1)%2+'0';//错一位
for(int j=s1.length()-1; j>=0; j--)
a[i]+=(s1[j]-'0')*(int)pow(2,s1.length()-j-1);//转换成十进制
s1[i]=(s1[i]-'0'+1)%2+'0';//变成原来的
}
int k=0;
for(int i=0; i<s2.length(); i++,k++)
{
s2[i]=(s2[i]-'0'+1)%3+'0';//因为是3进制有3个数,所以要进行两次数字变换
for(int j=s2.length()-1; j>=0; j--)
b[k]+=(s2[j]-'0')*(int)pow(3,s2.length()-j-1);
k++;
s2[i]=(s2[i]-'0'+1)%3+'0';
for(int j=s2.length()-1; j>=0; j--)
b[k]+=(s2[j]-'0')*(int)pow(3,s2.length()-j-1);
s2[i]=(s2[i]-'0'+1)%3+'0';
}
int flag=0;
for(int i=0; i<s1.length(); i++)
{
for(int j=0; j<k; j++)
{
if(a[i]==b[j])//找到相等的输出
{
cout<<a[i]<<endl;
flag=1;
break;
}
}
if(flag)break;
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: