Weird Clock My Tags (Edit) Source : ZJU Time limit : 3 sec Memory limit : 32 M
Submitted : 9417, Accepted : 1821
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 ( 0 <= 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 ( 0 <= 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
有必要說下的是這種標記方法 被訪問的用 1 標記 , 沒訪問的用 0 標記; 數數組去記錄標記的位置
演算法思想:
第一步: 資料結構:要想到使用一個數組來標記 鈡所走的位置 1表示走過了 0 表示還沒有走過
第二步: :演算法求其餘數 在一個while迴圈裡面完成 知道餘數為0 停止 見代碼 while迴圈中 這裡最關鍵
第三遍 :列印就可以了
原始碼:
WA 的不知道為什麼 WA
#include<iostream>#include<cstring>using namespace std;int main(){ int a, b; int c[61]; while(cin>>a>>b) { memset(c, 0, sizeof(c)); if(a == 0 && b == 0) { return 0; } if(a == 0) { cout<<"0"<<endl; } if(a != 0 && b == 0) { cout<<"Impossible"<<endl; } int t = 0; while(a && c[a] == 0) {//開始迴圈記錄求餘 記錄指標停的位置 c[a] = 1; a = a*(b+1)%60; t++; // t 用來記錄金幣使用的個數 } if(a != 0) { cout<<"Impossible"<<endl; } else { cout<<t<<endl; } } return 0;}再來一個 AC 的原始碼:
#include <stdio.h>int main(){ int s, p; while(scanf("%d %d", &s, &p) == 2){ if((s == 0) && (p == 0)){ return 0; }else if((s == 0) && (p != 0)){ printf("0\n"); }else if(s*(p+1) % 60 == 0){ printf("1\n"); }else if(s*(p+1)*(p+1) % 60 == 0){ printf("2\n"); }else{ printf("Impossible\n"); } } return 0;}
這裡要說的是 最多使用兩個金幣 2 到二就可以停止了 ,為什麼了 。。。。。好吧不知道。。。。