@sensitive-cs
2016-10-19T16:32:09.000000Z
字数 1396
阅读 821
有一种运算规则,存在a和b两个数,a的第i位与b的第i位相加,所得的结果对3取余就是c的第i位,现在给出a和c,求出b。
将a和c转化为3进制数然后用字符串保存。
对a和c的每一位,如果a和c的第i位是相同的,那么b的第i位是0;如果a的第i位大于c的第i位,那么b的第i位就等于a的第i位减去c的第i位;若a的第i位小于c的第i位,那么b的第i位就等于a的第i位加上3减去c的第i位。
当c的位数大于a,那么c超出a的位数的数全部等于b对应的位数的数;当a的位数大于c,那么对于a比c多出的位数,如果存在0,则b的对应位数的的数等于0,不存在0的话,那么b对应的位数的数等于3-a的此位上的数。然后将b所对应的三进制数转化为10进制输出。
#include <stdio.h>
#include <math.h>
char a[30];
char c[30];
char b[30];
int main()
{
long long result = 0,x,y;
int i = 0,j = 0,m;
scanf("%I64d%I64d",&x,&y);
while (x != 0)
{
a[i] = x % 3;
x /= 3;
i++;
}
while (y != 0)
{
c[j] = y % 3;
y /= 3;
j++;
}
if (j == i)
{
for (m = 0;m < j;m++)
{
if (c[m] == a[m])
b[m] = 0;
if (c[m] == 0 && a[m] == 1)
b[m] = 2;
if (c[m] == 0 && a[m] == 2)
b[m] = 1;
if (c[m] == 1 && a[m] == 0)
b[m] = 1;
if (c[m] == 1 && a[m] == 2)
b[m] = 2;
if (c[m] == 2 && a[m] == 0)
b[m] = 2;
if (c[m] == 2 && a[m] == 1)
b[m] = 1;
}
}
else if (j > i)
{
for (m = 0;m < i;m++)
{
if (c[m] == a[m])
b[m] = 0;
if (c[m] == 0 && a[m] == 1)
b[m] = 2;
if (c[m] == 0 && a[m] == 2)
b[m] = 1;
if (c[m] == 1 && a[m] == 0)
b[m] = 1;
if (c[m] == 1 && a[m] == 2)
b[m] = 2;
if (c[m] == 2 && a[m] == 0)
b[m] = 2;
if (c[m] == 2 && a[m] == 1)
b[m] = 1;
}
for (m = i;m < j;m++)
b[m] = c[m];
}
else
{
for (m = 0;m < j;m++)
{
if (c[m] == a[m])
b[m] = 0;
if (c[m] == 0 && a[m] == 1)
b[m] = 2;
if (c[m] == 0 && a[m] == 2)
b[m] = 1;
if (c[m] == 1 && a[m] == 0)
b[m] = 1;
if (c[m] == 1 && a[m] == 2)
b[m] = 2;
if (c[m] == 2 && a[m] == 0)
b[m] = 2;
if (c[m] == 2 && a[m] == 1)
b[m] = 1;
}
for (m = j;m < i;m++)
{
if (a[m] == 0 && c[m] == 0)
b[m] = 0;
else
b[m] = 3 - a[m];
}
}
int max;
if (j >= i)
max = j;
else
max = i;
for (m = 0;m < max;m++)
{
if (m == 0)
result += b[m];
else
result += b[m] * (long long)pow(3,m);
}
printf("%I64d\n",result);
return 0;
}