POJ 1002 487-3279
来源:互联网 发布:只有我知bilibili电影 编辑:程序博客网 时间:2024/06/05 18:48
这个题是按要求输出电话号码,开始很快写了一版出来,用给的测试案例测试没问题,但是提交一直提示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){// inputScanner 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 numberinput = parseChar(input);Arrays.sort(input);// printint number = 1;boolean isPrint = false;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
- POJ 1002 487-3279
- POJ 1002 487-3279
- POJ 1002 487-3279
- poj 1002 "487-3279"
- POJ 1002 487-3279
- poj 1002 487-3279
- poj 1002 487-3279
- POJ-1002 487-3279
- poj 1002 487-3279
- POJ 1002 487-3279
- poj 1002 487-3279
- (POJ 1002) 487-3279
- POJ 1002 487-3279
- POJ 1002 487-3279
- [POJ]1002 487-3279
- POJ 1002 487-3279
- poj 1002 487-3279
- 二、修改表、字段属性设置、索引设置、约束设置
- 二分法求多项式单根【数据结构测试3.1】
- char str1[11] = "12345\06789"; 引发的深刻教训---原来'\067'与'7'是相等的
- OpenCV系统学习(基本了解完)
- 深入了解Windows句柄到底是什么
- POJ 1002 487-3279
- 自己编写脚本启动hadoop各个守护进程需要的特别配置
- Sphere - AABB Collision Detaction
- 工业互联网不是什么?
- erlang send剖析及参数意义
- 回顾框架搭建问题总结
- Java内存管理(一)--内存分区
- 新手学Android之setContentView(R.layout.XXX);会报错
- prepareStatement的用法和解释