9行代码AC_HDU-6374 Decimal(余数,因子)

励志用少的代码做高效表达


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.


思路与解析

规律是:10的n次方(n>0)的质因子只有2和5。

因此, 最开始想到的思路是:看该数的质因子是否只有2和5

比赛结束后和同学交流:他们的解法是:定义一个大数,如1000000,看是否能整除n


解法一代码

#include<bits/stdc++.h>
using namespace std;

bool f(int n){
    
    
	if(1==n)return false;
	else if(0==n%2) return f(n/2);
	else if(0==n%5) return f(n/5);
	else return true;
}

int main(){
    
    
	int T; cin>>T; while(T--) {
    
    
		int i; cin>>i;
		cout << (f(i)==false?"No":"Yes") << endl;
	}
return 0; }

解法二代码

#include<iostream>
using namespace std;
int main(){
    
    
	long long int a=1000000000;
	int t; cin>>t; while(t--){
    
    
		int n; cin>>n;
		cout << (a%n!=0?"Yes":"No") << endl;
	}
return 0; }

猜你喜欢

转载自blog.csdn.net/weixin_43899069/article/details/108919683
今日推荐