HDOJ1405 The Last Practice(有坑)
来源:互联网 发布:程序员薪资城市排行榜 编辑:程序博客网 时间:2024/06/05 23:43
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.
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
6012-1
Sample Output
Case 1.2 2 3 1 5 1Case 2.2 2 3 1Hint60=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<65536break;}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();}}// 65536private 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
- HDOJ1405 The Last Practice(有坑)
- HDU1405:The Last Practice
- hdu_1405 The Last Practice
- HDU1405 The Last Practice
- hdu1405 The Last Practice
- hdu1405 The Last Practice
- The Last Practice
- The Last Practice
- HDU The Last Practice
- hd1405 The Last Practice
- HDU1405 The Last Practice
- hdu 1405 The Last Practice
- The Last Practice hdu 1405
- HDU 1405 The Last Practice
- 【HDOJ】1405 -> The Last Practice
- HDU 1405The Last Practice
- 杭电-The Last Practice
- HDU 1405 The Last Practice
- 经典算法---堆排序(Heapsort)
- IntelliJ IDEA (2017.3)激活
- Django中Mixin和View组合技巧
- java实现emqtt的消息发布
- httpclient
- HDOJ1405 The Last Practice(有坑)
- 2. 什么是对象
- G盘显示拒绝访问要如何办啊
- react-native-swiper 让人无语的坑
- 虚拟网卡 tun/tap口创建及介绍
- 郁闷的出纳员
- the mathtype dll cannot be found word2010(word2013类似)
- Python--模块之optparse
- 后缀数组