hdu 6734:Decimal

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

Problem Description

Given a positive integer n, determine if 1n is an infinite decimal in decimal base. If the answer is yes, print “Yes” in a single line, or print “No” if the answer is no.

 

 

Input

The first line contains one positive integer T (1 ≤ T ≤ 100), denoting the number of test cases.
For each test case:
Input a single line containing a positive integer n (1 ≤ n ≤ 100).

 

 

Output

Output T lines each contains a string “Yes” or “No”, denoting the answer to corresponding test case.

 

 

Sample Input

2
5
3

Sample Output

No
Yes

Hint

1/5 = 0.2, which is a finite decimal. 1/3 = 0.333 · · · , which is an infinite decimal.

题意分析:

给出数n,求1/n是否为无限小数.

解题思路:

模拟1/n的过程,如果一个余数出现了两次,那么一定会进入循环,小数一定是个无限小数.

#include <bits/stdc++.h>
using namespace std;
bool book[120];
int main()
{
    int T, n;
    scanf("%d", &T);
    while(T--)
    {
        scanf("%d", &n);
        memset(book, false, sizeof(book));
        int x=1, temp=0;
        while(1)
        {
            if(x%n==0) {
                temp=1;
                break;
            }
            x=x%n;
            if(book[x]==true) {
                temp=0;
                break;
            }
            book[x]=true;
            x*=10;    
        }
        if(temp==1)
            printf("No\n");
        else
            printf("Yes\n");
    }
    return 0;
} 

 

posted @ 2019-09-29 14:52  宿星  阅读(156)  评论(0编辑  收藏  举报