110704 Factovisors


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);
		}
	}

}


阅读更多
文章标签: 编程
个人分类: 算法练习
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

110704 Factovisors

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭