HDU 1393 Weird clock__ Simulation

Source: Internet
Author: User

Title Address:

http://acm.hdu.edu.cn/showproblem.php?pid=1393

Topic Description:
Weird Clock Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 2639 accepted Submission (s): 945


Problem Description a weird clock marked from 0 to the has only a minute hand. It won ' t move until a special coin are thrown into its box. There are different kinds of coins as your options. However Once you do your choice, you cannot use the any other kind. There are infinite number of coins of each kind, all marked with a number D (1 <= d <= 1000), meaning so this C Oin'll make the minute hand the "D times clockwise the" times. For example, if it is, and d = 2. Then the minute hand'll move clockwise the minutes and would be pointing to 15.

Now, are given the initial time S (1 <= s <=) and the coin ' s type D. Write a program to find the minimum num ber 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 1 0 0
Sample Output
1

The following:

Judge the shortest turn a few d times can be just 60 minutes.

The puzzle :

Simulation, and with the idea of indexing, repeats that not 60 of the clock will never reach 60.

Code:

#include <stdio.h>
#include <string.h>
bool visited[61] = {false};
int s=0,d=0;
int main ()
{while
    (scanf ("%d%d", &s,&d)!=eof&& (s+d) >0)
    {
        memset (visited,false , sizeof (visited));
        Visited[60]=true;
        Visited[s]=true;
        int cnt=0;
        while (s!=)
        {
            s=s+s*d;
            cnt++;
            while (s>60) s-=60;
            if (Visited[s]) break;
            else visited[s] = true;
        }
        if (s==60) printf ("%d\n", CNT);
        else printf ("impossible\n");
    }
    return (0);
}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.