概述
time limit per test 2 seconds
memory limit per test 256 megabytes
input standard input
output standard output
Little Petya very much likes computers. Recently he has received a new “Ternatron IV” as a gift from his mother. Unlike other modern computers, “Ternatron IV” operates with ternary and not binary logic. Petya immediately wondered how the xoroperation is performed on this computer (and whether there is anything like it).
It turned out that the operation does exist (however, it is called tor) and it works like this. Suppose that we need to calculate the value of the expression a tor b. Both numbers a and b are written in the ternary notation one under the other one (bunder a). If they have a different number of digits, then leading zeroes are added to the shorter number until the lengths are the same. Then the numbers are summed together digit by digit. The result of summing each two digits is calculated modulo 3. Note that there is no carry between digits (i. e. during this operation the digits aren’t transferred). For example: 1410 tor5010 = 01123 tor 12123 = 10213 = 3410.
Petya wrote numbers a and c on a piece of paper. Help him find such number b, that a tor b = c. If there are several such numbers, print the smallest one.
Input
The first line contains two integers a and c (0 ≤ a, c ≤ 109). Both numbers are written in decimal notation.
Output
Print the single integer b, such that a tor b = c. If there are several possible numbers b, print the smallest one. You should print the number in decimal notation.
Sample test(s)
input
14 34
output
50
input
50 34
output
14
input
387420489 225159023
output
1000000001
input
5 5
output
0
题意 :就是说tor是一个三进制的运算,代表的是两个三进制数的运算,两个三进制数按位逐一相加后对三取余,没有进位,但是如果两个没有相同多的位数的话,短的那个0。而那个样例就是10进制的14和10进制的59进行tor运算,就相当于分别转化成三进制在进行tor运算,a tor b = c,给你a和c,让你求b 。
思路 : 其实也没什么好说的,题懂了,基本上思路也就出来了,我也就是把a c都转化成三进制的,然后用c的三进制减a 的三进制,最后再转化成10进制即可 。
#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std ;
int aa[110],bb[110],cc[110] ;
int tran(int n ,int *s)
{
int i = 0 ;
while(n)
{
s[i++] = n%3 ;
n /= 3 ;
}
return i ;
}
int main()
{
int a,c ;
memset(aa,0,sizeof(aa)) ;
memset(bb,0,sizeof(bb)) ;
memset(cc,0,sizeof(cc)) ;
while(scanf("%d %d",&a,&c)!=EOF)
{
int lena = tran(a,aa) ;
int lenc = tran(c,cc) ;
int maxx = lena > lenc?lena:lenc ;
for(int i = 0 ; i < maxx ; i++)
{
cc[i] = cc[i] >= aa[i]?cc[i]:cc[i]+3 ;
bb[i] = cc[i]-aa[i] ;
}
int count = 1 ;
int b = 0 ;
for(int i = 0 ; i < maxx ; i++)
{
b += (count*bb[i]) ;
count *= 3 ;
}
printf("%dn",b) ;
}
return 0 ;
}
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
int a,b,c,p;
scanf("%d %d",&a,&c);
b=0;
p=1;
while(a||c)
{
b+=((c%3-a%3+3)%3)*p;
p*=3;
a/=3;
c/=3;
}
printf("%dn",b);
return 0;
}
最后
以上就是单纯保温杯为你收集整理的B. Ternary Logic(三进制)的全部内容,希望文章能够帮你解决B. Ternary Logic(三进制)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复