Week10 A - 签到题
东东在玩游戏“Game23”。
在一开始他有一个数字n,他的目标是把它转换成m,在每一步操作中,他可以将n乘以2或乘以3,他可以进行任意次操作。输出将n转换成m的操作次数,如果转换不了输出-1。
Input
输入的唯一一行包括两个整数n和m(1<=n<=m<=5*10^8).
Output
输出从n转换到m的操作次数,否则输出-1.
Simple Input 1
120 51840
Simple Output 1
7
Simple Input 2
42 42
Simple Output 2
0
Simple Input 3
48 72
Simple Output 3
-1
解题思路
就%呗
Code
#include<iostream>
#include<stdio.h>
using namespace std;
int main(){
int a,b;
int t=0;
scanf("%d %d",&a,&b);
if(b%a==0){
a=b/a;//cout<<a<<endl;cout<<a%2<<endl;
while(a%2==0){
a/=2;t++;
}
while(a%3==0){
a/=3;t++;
}
if(a==1){
cout<<t<<endl;
}else{
cout<<"-1"<<endl;
}
}else{
cout<<"-1"<<endl;
}
}