求所有约数,暴力求解(UVA 10892)

import java.io.*;
import java.util.*;
import java.math.*;

public classMain {
	
	int n, len;
	LinkedList<Integer>list;
	
	int GCD(int a, int b) {
		return b == 0 ? a : GCD(b, a % b);
	}	
	
	int LCM(int a, int b) {
		return a / GCD(a, b) * b;
	}
	
	boolean isEqual() {
		return Math.sqrt(n) == (int)Math.sqrt(n); 
	}
	
	void run() {
		while (true) {
			n = cin.nextInt();
			if (n == 0) break;
			list = new LinkedList<Integer>();
			for (int i = 1; i * i <= n; i++) {
				if (n % i == 0) {
					list.add(i);
					list.add(n / i);  // 可能有i == n / i
				}
			}
			int count = 1;
			for (int i = 0; i < list.size(); i++) {
				for (int j = i + 1; j < list.size(); j++) {
					if (LCM(list.get(i), list.get(j)) == n) count++;
				}
			}
			if (isEqual()) count--;
			System.out.println(n + " " + count);
		}
	}               
	
	public static void main(String[] args) {
		Mainsolved = new Main();
		solved.run();
	}
	
	static InputStreaminputStream = System.in;
	static InputReadercin = new InputReader(inputStream);
	
//	Scanner cin = new Scanner(new BufferedInputStream(System.in));
	
}


classInputReader { 

    private InputStreamstream; 
    private byte[] buf = new byte[1024]; 
    private int curChar; 
    private int numChars; 

    public InputReader(InputStreamstream) { 
        this.stream = stream; 
    } 

    public int read() { 
        if (numChars == -1) 
            return -1; 
            //throw new InputMismatchException(); 
        if (curChar >= numChars) { 
            curChar = 0; 
            try { 
                numChars = stream.read(buf); 
            } catch (IOExceptione) { 
                throw new InputMismatchException(); 
            } 
            if (numChars <= 0) 
                return -1; 
        } 
        return buf[curChar++]; 
    } 

    public int nextInt() { 
        int c = read(); 
        if (c == -1) 
            return -1; 
        while (isSpaceChar(c)) 
            c = read(); 
        int sgn = 1; 
        if (c == '-') { 
            sgn = -1; 
            c = read(); 
        } 
        int res = 0; 
        do { 
            if (c < '0' || c > '9') 
                throw new InputMismatchException(); 
            res *= 10; 
            res += c - '0'; 
            c = read(); 
        } while (!isSpaceChar(c)); 
        return res * sgn; 
    } 
     
    public Stringnext() {   
        StringBuilderstr = new StringBuilder();   
        int ch;   
        while (isSpaceChar(ch = read()));   
        if (ch == -1)   
            return null;   
        do {   
            str.appendCodePoint(ch);   
        } while (!isSpaceChar(ch = read()));   
        return str.toString();   
    }  
     
    public static boolean isSpaceChar(int c) { 
        return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; 
    } 

    public char nextCharacter() { 
        int c = read(); 
        while (isSpaceChar(c)) 
            c = read(); 
        return (char) c; 
    } 

} 

posted on 2013-02-20 08:49  Sure_Yi  阅读(220)  评论(0编辑  收藏  举报

导航