首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

北大acm 1002 求救解决思路

2012-01-05 
北大acm 1002 求救网上提供的java源代码 1002题import java.io.BufferedInputStreamimport java.io.DataI

北大acm 1002 求救
网上提供的java源代码 1002题
import java.io.BufferedInputStream;  
import java.io.DataInputStream;  
import java.io.IOException;  
import java.util.Iterator;  
import java.util.Map;  
import java.util.Scanner;  
import java.util.Set;  
import java.util.TreeMap;  
public class Main {  
  
  public static char getNum(char c) {  
  if (Character.isDigit(c)) {  
  return c;  
  }  
  if (c == 'A' || c == 'B' || c == 'C') {  
  return '2';  
  }  
  if (c == 'D' || c == 'E' || c == 'F') {  
  return '3';  
  }  
  if (c == 'G' || c == 'H' || c == 'I') {  
  return '4';  
  }  
  if (c == 'J' || c == 'K' || c == 'L') {  
  return '5';  
  }  
  if (c == 'M' || c == 'N' || c == 'O') {  
  return '6';  
  }  
  if (c == 'P' || c == 'R' || c == 'S') {  
  return '7';  
  }  
  if (c == 'T' || c == 'U' || c == 'V') {  
  return '8';  
  }  
  if (c == 'W' || c == 'X' || c == 'Y') {  
  return '9';  
  }  
  return '#';  
  }  
  
  public static void main(String[] args) throws IOException {  
  DataInputStream scan = new DataInputStream(new BufferedInputStream(System.in));  
  Map< String, Integer> tm = new TreeMap();  
  int n = Integer.parseInt(scan.readLine().trim());
  for (int i = 0; i < n; i++) {  
  String s = scan.readLine().replace("-", "");  
  StringBuilder sb = new StringBuilder();  
  for (int k = 0; k < s.length(); k++) {  
  char c = getNum(s.charAt(k));  
  if (Character.isDigit(c)) {  
  sb.append(c);  
  }  
  }  
  String result = sb.toString().substring(0, 3) + '-' + sb.toString().substring(3);  
  if (tm.containsKey(result)) {  
  int count = tm.get(result) + 1;  
  tm.put(result, count);  
  } else {  
  tm.put(result, 1);  
  }  
  }  
  Set se = tm.keySet();  
  Iterator it = se.iterator();  
  boolean flag = false;  
  while (it.hasNext()) {  
  String s = it.next().toString();  
  int count = tm.get(s);  
  if (count > 1) {  
  flag = true;  
  System.out.println(s + " " + count);  
  }  
  }  
  if (!flag) {  
  System.out.println("No duplicates. ");  
  }  
  }  
}  
编译提示出错
 DataInputStream scan = new DataInputStream(new BufferedInputStream(System.in));  
此类 在jdk5.0中已经被弃用 敢问高手怎么修改才好



[解决办法]

Java code
import java.io.IOException;import java.util.Iterator;import java.util.Map;import java.util.Scanner;import java.util.Set;import java.util.TreeMap;public class acm1002 {    public static char getNum(char c) {        if (Character.isDigit(c)) {            return c;        }        if (c == 'A' || c == 'B' || c == 'C') {            return '2';        }        if (c == 'D' || c == 'E' || c == 'F') {            return '3';        }        if (c == 'G' || c == 'H' || c == 'I') {            return '4';        }        if (c == 'J' || c == 'K' || c == 'L') {            return '5';        }        if (c == 'M' || c == 'N' || c == 'O') {            return '6';        }        if (c == 'P' || c == 'R' || c == 'S') {            return '7';        }        if (c == 'T' || c == 'U' || c == 'V') {            return '8';        }        if (c == 'W' || c == 'X' || c == 'Y') {            return '9';        }        return '#';    }    public static void main(String[] args) throws IOException {//修改的开始        Scanner reader = new Scanner(System.in);        Map<String, Integer> tm = new TreeMap();        int n = Integer.parseInt(reader.next().trim());        for (int i = 0; i < n; i++) {            String s = reader.next().replace("-", "");            //修改的结束            StringBuilder sb = new StringBuilder();            for (int k = 0; k < s.length(); k++) {                char c = getNum(s.charAt(k));                if (Character.isDigit(c)) {                    sb.append(c);                }            }            String result = sb.toString().substring(0, 3) + '-' + sb.toString().substring(3);            if (tm.containsKey(result)) {                int count = tm.get(result) + 1;                tm.put(result, count);            } else {                tm.put(result, 1);            }        }        Set se = tm.keySet();        Iterator it = se.iterator();        boolean flag = false;        while (it.hasNext()) {            String s = it.next().toString();            int count = tm.get(s);            if (count > 1) {                flag = true;                System.out.println(s + " " + count);            }        }        if (!flag) {            System.out.println("No duplicates. ");        }    }} 

热点排行