hoj 1006_ACM-類比

來源:互聯網
上載者:User
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 到二就可以停止了 ,為什麼了 。。。。。好吧不知道。。。。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.