码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 1002 487-3279

时间:2015-01-18 22:42:49      阅读:226      评论:0      收藏:0      [点我收藏+]

标签:

这个题是按要求输出电话号码,开始很快写了一版出来,用给的测试案例测试没问题,但是提交一直提示Wrong Answer,从网上看到有人用如下测试案例

2

0000-00-0

00-0-0-0-0-0

顿时意识到我的电话号码都是当作int来处理的,这样两端的0都不见了。其实,问题的起因是我之前只知道有个Arrays.sort(int[] temp),而不知道还有个Arrays.sort(char[] temp)。为了使用Arrays.sort(int[] temp),我将中间结果都先转换成int,然后调用来排序,然后再一个数字一个数字的计算之后取出来再打印。这个时候没有Wrong Answer了,但是这种方式添加了很多额外的处理,提交一直是Runtime Error,估计是消耗时间太长了,没办法,将代码分为几段,逐段进行优化。

最后发现有Arrays.sort(char[] temp)可以用,不再需要先转化为int[]来排序,而且排序完之后也不需要一个数字一个数字的取出来打印,可以直接用subString()函数取。

import java.util.Arrays;
import java.util.Scanner;

public class Main {
	public static void main(String[] args){
		// input
		Scanner scanner = new Scanner(System.in);
		int count = scanner.nextInt();
		
		String[] input = new String[count];
		for(int x=0; x<count; x++){
			input[x] = scanner.next();
		}
		
		// parse char to number
		input = parseChar(input);
		Arrays.sort(input);
		
		// print
		int number = 1;
		boolean isPrint = false;
		if(count == 2) {
			if(input[0].equals(input[1])) {
				System.out.println(input[0].substring(0, 3) + "-" 
						+ input[0].substring(3, 7) + " " + 2);
				isPrint = true;
			}
		} else if(count > 2){
			for(int m=0; m<count-1; m++){
				if(input[m+1].equals(input[m])){
					number ++;
					
					if(m+2 == count){
						if(number > 1) {
							System.out.println(input[m].substring(0, 3) + "-" 
									+ input[m].substring(3, 7) + " " + number);
							isPrint = true;
						}
					}
				} else {
					if(number > 1) {
						System.out.println(input[m].substring(0, 3) + "-" 
								+ input[m].substring(3, 7) + " " + number);
						isPrint = true;
					}
					number = 1;
				}
			}
		}
		
		if(!isPrint) System.out.println("No duplicates.");
		
	}
	
	
	public static String[] parseChar(String[] input){
		int count = input.length;
		
		for (int i=0; i<count; i++){
			char[] val2 = new char[7];
			
			for(int j=0, m=0; j<input[i].length() && m<7; j++, m++){
				// switch...case and if...else has the same effect.
				switch(input[i].toCharArray()[j]) {
				case 'A': 
				case 'B': 
				case 'C': val2[m] = '2'; break;
				case 'D': 
				case 'E': 
				case 'F': val2[m] = '3'; break;
				case 'G': 
				case 'H': 
				case 'I': val2[m] = '4'; break;
				case 'J': 
				case 'K': 
				case 'L': val2[m] = '5'; break;
				case 'M': 
				case 'N': 
				case 'O': val2[m] = '6'; break;
				case 'P': 
				case 'R': 
				case 'S': val2[m] = '7'; break;
				case 'T': 
				case 'U': 
				case 'V': val2[m] = '8'; break;
				case 'W': 
				case 'X': 
				case 'Y': val2[m] = '9'; break;
				case '-': m--;           break;
				default: val2[m] = input[i].toCharArray()[j];
				}
			}
			input[i] = String.valueOf(val2);
		}
		return input;
	}
}


完成这道题花了2个晚上,不断调整不断优化代码,当然整个过程有很多思考,回过头来总结也有很多收获。

总结两点:

1、解题步骤:先构思,再编码实现。找数据测试,做到没有Wrong Answer错误。如果出现Runtime Error,那就将代码分为几段,分段进行优化。

2、熟悉常用函数。在使用某函数之前,需要先大致了解其相关的,采用最合适的,而不是找到一个可用就用起来了,最后发现走了很多弯路。这题因为Arrays.sort(char[] temp)不熟悉而走了很多弯路,POJ 1001也是同样的问题(不熟悉Scanner,BigDecimal的常用函数而走很多弯路)。

POJ 1002 487-3279

标签:

原文地址:http://blog.csdn.net/luopeng123456789/article/details/42845327

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!