Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
Re:第一次弄这个练习In Reply To:Re:第一次弄这个练习 Posted by:dreamwf at 2009-04-14 21:17:02 我写的, 可读性好点, 呵呵 import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.List; import java.util.Map; public class P1002 { private static String convert(String line) { String ret = ""; for (int i = 0; i < line.length(); i++) { if (line.charAt(i) >= '0' && line.charAt(i) <= '9') { ret += line.charAt(i); } else if (line.charAt(i) >= 'A' && line.charAt(i) <= 'Z') { char c = line.charAt(i); if (c == 'A' || c == 'B' || c == 'C') { ret += "2"; } else if (c == 'D' || c == 'E' || c == 'F') { ret += "3"; } else if (c == 'G' || c == 'H' || c == 'I') { ret += "4"; } else if (c == 'J' || c == 'K' || c == 'L') { ret += "5"; } else if (c == 'M' || c == 'N' || c == 'O') { ret += "6"; } else if (c == 'P' || c == 'R' || c == 'S') { ret += "7"; } else if (c == 'T' || c == 'U' || c == 'V') { ret += "8"; } else if (c == 'W' || c == 'Y' || c == 'X') { ret += "9"; } } } return ret.substring(0, 3) + "-" + ret.substring(3); } public static void main(String[] args) throws Exception { Map<String, Integer> map = new HashMap<String, Integer>(); BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in)); String line = stdin.readLine(); int n = Integer.parseInt(line); for (int i = 0; i < n; i++) { line = stdin.readLine(); String result = convert(line); if (map.get(result) == null) { map.put(result, 1); } else { int newValue = map.get(result) + 1; map.put(result, newValue); } } if (map.keySet().size() == n) { System.out.println("No duplicates."); } else { List<Count> list = new ArrayList<Count>(); for (Map.Entry<String, Integer> entry : map.entrySet()) { if (entry.getValue() > 1) { list.add(new Count(entry.getKey(), entry.getValue())); } } Collections.sort(list, new Comparator() { public int compare(Object arg0, Object arg1) { Count c1 = (Count) arg0; Count c2 = (Count) arg1; return c1.s.compareTo(c2.s); } }); for (Count c: list) { System.out.println(c.s + " " + c.number); } } } static class Count { String s; int number; public Count(String s, int number) { this.s = s; this.number = number; } } } > import java.util.Scanner; > > > public class Main { > > private int n; > private String[] result; > private String[] input;; > > > public String[] input() { > Scanner in=new Scanner(System.in); > n=Integer.parseInt(in.nextLine()); > input=new String[n]; > char[] single=new char[7]; > for(int i=0;i<n;i++){ > int au=0; > String temp=in.nextLine(); > for(int j=0;j<temp.length();j++){ > int asc=temp.charAt(j); > if(asc==81||asc==113){continue;} > if(asc>=48&&asc<=57||asc>=65&&asc<90||asc>=97&&asc<122){ > if(asc>=97){ > asc=asc-32; > System.out.println(asc); > char a=this.mapping(asc); > single[au++]=(char) a; > }else{ > char a=this.mapping(asc); > single[au++]=(char) a; > } > }else{ > continue; > } > } > input[i]=new String(single); > } > return input; > } > > public char mapping(int asc){ > char num='2'; > char cha='A'-1; > char temp=(char) asc; > for(int i=1;i<=25;i++){ > cha++; > if(cha=='Q'){ > i--; > continue; > } > if(temp==cha){ > temp=num; > return temp; > } > if(i%3==0)num++; > } > return temp; > } > > public String[] sort(String[] input){ > for(int i=0;i<input.length;i++){ > for(int j=i;j<input.length;j++){ > String t=null; > if(Integer.parseInt(input[i])>Integer.parseInt(input[j])){ > t=input[i]; > input[i]=input[j]; > input[j]=t; > } > } > } > return input; > } > > public String[] classify(String[] aaa){ > String[] result =new String[n]; > int numbers=1; > for(int i=0;i<aaa.length-1;i++){ > if(aaa[i].equals(aaa[i+1])){ > numbers++; > > //result[i]=new String(aaa[i]+" "+numbers); > }else{ > if(numbers!=1){ > result[i]=new String(aaa[i]+" "+numbers); > numbers=1; > } > } > } > return result; > > } > > public boolean isallnull(String[] bb ){ > for(int i=0;i<bb.length;i++){ > if(bb[i]!=null)return false; > } > return true; > } > public static void main(String[] args){ > Main t=new Main(); > String[] input=t.input(); > input=t.sort(input); > String[] result =t.classify(input); > if(t.isallnull(result)){ > System.out.println("No duplicates"); > }else{ > for(int i=0;i<result.length;i++) > { > if(result[i]!=null){ > String str1=new String(result[i].substring(0, 3)); > String str2=new String(result[i].substring(3)); > result[i]=new String(str1+"-"+str2); > System.out.println(result[i]); > } > } > } > } > } > 这个是我写的代码。才学编程没半年呢。程序写的很垃圾。高手指点下 Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator