1679 - 通过了吗

通过次数

0

提交次数

0

时间限制 : 1 秒 内存限制 : 32 MB
小G发现了一个奇怪的问题。
"怎么ACMclub里我不会做的题目号这么有规律?"
经系统检查,题目编号含有2,3,5以外的质因子的题目小G都没有通过,反之则全部通过。

题目输入

输入有多组数据。
每组数据一行,1个正整数(不大于1000000000),代表题目号。

题目输出

对应每组数据,如果小G通过了这道题目,则输出1,否则输出0。

输入/输出样例

输入格式

15
20
47
57

输出格式

1
1
0
0

C语言解答

#include<stdio.h>
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        while(n%2==0) n/=2;
        while(n%3==0) n/=3;
        while(n%5==0) n/=5;
        if(n==1) printf("1\n");
        else printf("0\n");
    }
    return 0;
}

C++解答

#include <stdio.h>

bool IsUgly(int number)
{
    while(number % 2 == 0)
        number /= 2;
    while(number % 3 == 0)
        number /= 3;
    while(number % 5 == 0)
        number /= 5;
    return (number == 1) ? true : false;
}

int main()
{
	int n;
	while(scanf("%d",&n)!=EOF)
	{
		printf("%d\n",IsUgly(n));
	}	
	return 0;
}

Java解答

import java.math.BigInteger;
import java.util.Scanner;


public class Main {

	private static final BigInteger FIVE = new BigInteger("5");
	private static final BigInteger THREE = new BigInteger("3");
	private static final BigInteger TWO = new BigInteger("2");
	private static final BigInteger ONE = new BigInteger("1");
	private static final BigInteger ZERO = new BigInteger("0");
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		Scanner cin = new Scanner(System.in);
		BigInteger bigInteger;
		

		while(cin.hasNext())
		{
			bigInteger = cin.nextBigInteger();
			if(isPass(bigInteger))
				System.out.println("1");
			else 
				System.out.println("0");
		}
	}
	
	public static boolean isPass(BigInteger bigInteger)
	{

		while( bigInteger.equals(ONE)==false && bigInteger.mod(FIVE).equals(ZERO) )
		{
			bigInteger=bigInteger.divide(FIVE);
		}
		
		while( bigInteger.equals(ONE)==false && bigInteger.mod(THREE).equals(ZERO) )
		{
			bigInteger=bigInteger.divide(THREE);
		}
		
		while( bigInteger.equals(ONE)==false && bigInteger.mod(TWO).equals(ZERO) )
		{
			bigInteger=bigInteger.divide(TWO);
		}
		
		if( bigInteger.equals(ONE))
			return true;
		
		return false;
	}

}