此题关键在于理解题目,题目意思是:假如输入的是 10 2
投第1枚硬币:10->30(10+10*2)
投第2枚硬币:30->90(30+30*2)==90%60==30
投第3枚硬币:30->90(30+30*2)==90%60==30
所以可知输入 10 2 的结果是 Impossible
如何判断它是否是 Impossible 很简单,只要投入硬币数到第61枚还没归0说明是 Impossible 投入硬币后的结果最多就60种可能,而到第61枚还没出结果,说明肯定除以循环状态,即结果为Impossible
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
int s = sc.nextInt();
int d = sc.nextInt();
if (s==0 && d==0) {
break;
}
int count = 0;
while (true) {
if (s%60==0) {
System.out.println(count);
break;
}
s = (s+s*d)%60;
count++;
if (count>61) {
System.out.println("Impossible");
break;
}
}
}
sc.close();
}
}