HDOJ1393 Weird Clock

来源:互联网 发布:什么叫网络投资 编辑:程序博客网 时间:2024/06/05 07:40

Weird Clock

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4200    Accepted Submission(s): 1564


Problem Description
A weird clock marked from 0 to 59 has only a minute hand. It won't move until a special coin is thrown into its box. There are different kinds of coins as your options. However once you make your choice, you cannot use any other kind. There are infinite number of coins of each kind, each marked with a number d ( 1 <= d <= 1000 ), meaning that this coin will make the minute hand move d times clockwise the current time. For example, if the current time is 45, and d = 2. Then the minute hand will move clockwise 90 minutes and will be pointing to 15.

Now you are given the initial time s ( 1 <= s <= 59 ) and the coin's type d. Write a program to find the minimum number of d-coins needed to turn the minute hand back to 0.
 

Input
There are several tests. Each test occupies a line containing two positive integers s and d.

The input is finished by a line containing 0 0.
 

Output
For each test print in a single line the minimum number of coins needed. If it is impossible to turn the hand back to 0, output "Impossible".
 

Sample Input
30 10 0
 

Sample Output
1
需要读懂题意,一个时钟,只有分针,现在他正指向一个位置n,有一种硬币m,投入一次就可以使时钟顺时针转n*m分钟。
然后n的是变了(0--59)的,再继续投入这样的硬币,一直到n指向0,或者判断到他不肯指向0.
n是0--59的,每60次肯定是有一个循环的,所以暴力法解决就OK
import java.util.Scanner;public class Main{private static Scanner scanner;public static void main(String[] args) {scanner = new Scanner(System.in);while (scanner.hasNext()) {int n = scanner.nextInt();int m = scanner.nextInt();if (n == 0 && m == 0) {break;}boolean isZero = false;int i = 0;for (i = 1; i <= 60; i++) {n = (n + m*n) % 60;//System.out.println(n);if (n == 0) {isZero = true;break;}}if (isZero) {System.out.println(i);} else {System.out.println("Impossible");}}}}

原创粉丝点击