关闭

110704 Factovisors

标签: 编程
248人阅读 评论(0) 收藏 举报
分类:


import java.util.*;
import static java.lang.Math.*;

public class Main {
	
	private static boolean IsPrime(int x)
	{
		int mid = (int)(sqrt(x));
		for (int i = 2; i <= mid; ++i)
			if ((x % i) == 0)
				return false;
		return true;
	}
	
	private static int GetGCD(int n, int m)
	{
		if (n < m)
			return GetGCD(m, n);
		
		if (n == m)
			return m;
		
		while (true)
		{
			int tmp = n % m;
			if (tmp == 0)
				return m;
			
			n = m;
			m = tmp;
		}
	}
	
	private static boolean IsDividable(int n, int m)
	{
		if (m == 1)
			return true;
		
		if (m == 0)
			return false;
		
		if (n == 0)
			return false;
		
		boolean needToCheckPrime = true;
		while (m > n)
		{
			if (n <= 2)
				return false;
			
			if (needToCheckPrime && IsPrime(m))
				return false;
			
			int gcd = GetGCD(n, m);
			needToCheckPrime = (gcd > 1) ? true : false;
			m /= GetGCD(n, m);
			--n;
		}
		
		return true;
	}
	
	private static void Handle(int n, int m)
	{		
		if (IsDividable(n, m))
			System.out.println(m + " divides " + n + "!");
		else
			System.out.println(m + " does not divide " + n + "!");
	}
	
	public static void main(String[] args)
	{
		Scanner inScanner = new Scanner(System.in);
		while (inScanner.hasNextInt())
		{
			int n = inScanner.nextInt();
			int m = inScanner.nextInt();
			Handle(n, m);
		}
	}

}


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:21712次
    • 积分:736
    • 等级:
    • 排名:千里之外
    • 原创:55篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条
    文章分类