HDOJ1405 The Last Practice(有坑)

The Last Practice

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 11287    Accepted Submission(s): 2583


Problem Description
Tomorrow is contest day, Are you all ready?
We have been training for 45 days, and all guys must be tired.But , you are so lucky comparing with many excellent boys who have no chance to attend the Province-Final.

Now, your task is relaxing yourself and making the last practice. I guess that at least there are 2 problems which are easier than this problem.
what does this problem describe?
Give you a positive integer, please split it to some prime numbers, and you can got it through sample input and sample output.
 

Input
Input file contains multiple test case, each case consists of a positive integer n(1<n<65536), one per line. a negative terminates the input, and it should not to be processed.
 

Output
For each test case you should output its factor as sample output (prime factor must come forth ascending ), there is a blank line between outputs.
 

Sample Input
  
  
60 12 -1
 

Sample Output
  
  
Case 1. 2 2 3 1 5 1 Case 2. 2 2 3 1
Hint
60=2^2*3^1*5^1
  这个题目最坑的莫过于空格问题,看题目是没说明的,看Sample Output也看不出来,第一次就PE了。
其次就是素数因子的问题,其实从2开始除,能除尽的都是素数了。
并没必要打表,我刚开始是打表的,被自己蠢哭了。
下面AC代码:
import java.util.Scanner;

public class Main{
	private static Scanner scanner;

	public static void main(String[] args) {
		scanner = new Scanner(System.in);
		int cases = 1;
		while (scanner.hasNext()) {
			int n = scanner.nextInt();
			if (n < 0) {// 1<n<65536
				break;
			}
			if (cases > 1) {
				System.out.println();
			}
			System.out.println("Case " + cases + ".");
			cases++;
			for (int i = 2; i <= Math.sqrt(n); i++) {
				int count = 0;
				while (n % i == 0) {
					n /= i;
					count++;
				}
				if (count > 0) {
					System.out.print(i + " " + count + " ");
				}
			}
			if (n != 1) {
				System.out.print(n + " " + 1 + " ");
			}
			System.out.println();
		}
	}
}

然后附上我第一次使用的方法(使用了集合,虽然AC了,但太复杂,仅供参考):
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.TreeMap;

public class Main{
	private static Scanner scanner;
	private static List<Integer> list;

	public static void main(String[] args) {
		dabiao();
		scanner = new Scanner(System.in);
		int cases = 1;
		while (scanner.hasNext()) {
			int n = scanner.nextInt();
			if (n < 0) {
				break;
			}
			int s = n, t = 0;
			// key是素数,value是该素数对应的次方
			Map<Integer, Integer> map = new TreeMap<Integer, Integer>();
			while (s != 1) {
				for (int i = t; i < list.size(); i++) {
					int key = list.get(i);
					if (s % key == 0) {
						// System.out.println(key);
						if (map.containsKey(key)) {
							int value = map.get(key);
							map.replace(key, ++value);
						} else {
							map.put(key, 1);
						}
						t = i;
						s /= key;
						break;
					}
				}
			}
			Iterator<Entry<Integer, Integer>> iterator = map.entrySet().iterator();
			if (cases > 1) {
				System.out.println();
			}
			System.out.println("Case " + cases + ".");
			cases++;
			if (n == 1) {
				System.out.println(1 + " " + 1 + " ");
			}
			// boolean isFirst = true;
			while (iterator.hasNext()) {
				Map.Entry<Integer, Integer> entry = iterator.next();
				int key = entry.getKey();
				int value = entry.getValue();
				System.out.print(key + " " + value + " ");//后面都有个空格
			}
			System.out.println();
		}
	}

	// 65536
	private static void dabiao() {
		boolean isPrime[] = new boolean[65536];
		for (int i = 2; i < isPrime.length; i++) {
			isPrime[i] = true;
		}
		for (int i = 2; i < isPrime.length; i++) {
			if (isPrime[i]) {
				for (int j = i + i; j < isPrime.length; j += i) {
					isPrime[j] = false;
				}
			}
		}
		list = new ArrayList<Integer>();
		for (int i = 2; i < isPrime.length; i++) {
			if (isPrime[i]) {
				list.add(i);
			}
		}
	}

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值