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 |
自我感觉良好~就献丑了~In Reply To:Re:让switch或者elseif快些的办法 Posted by:MaElsTroM at 2007-06-12 17:53:37 import java.io.BufferedReader; import java.io.InputStreamReader; public class Main { private static class BST { public static boolean duplicate = false; private String telnum; private int count; private BST left, right; public BST(String telnum) { this.telnum = telnum; this.left = null; this.right = null; count = 1; } private static void insert(BST root, String telnum) { if (root.telnum.equals(telnum)) { root.count++; duplicate = true; } else if (root.telnum.compareTo(telnum) > 0) if (root.left == null) root.left = new BST(telnum); else insert(root.left, telnum); else if (root.right == null) root.right = new BST(telnum); else insert(root.right, telnum); } private static void traverse(BST root) { if (root.left != null) traverse(root.left); if (root.count > 1) System.out.println(root.telnum + " " + root.count); if (root.right != null) traverse(root.right); } } private static char formalize(char chr) { if (chr >= '0' && chr <= '9') return chr; else if (chr >= 'A' && chr <= 'Y') return (char) ((chr < 'Q' ? chr - 'A' : chr - 'B') / 3 + '2'); else return ' '; } private static String formalize(String telnum) { String formed = ""; for (int i = 0; i < telnum.length(); i++) { char chr = formalize(telnum.charAt(i)); if (chr != ' ') formed += chr; } return formed.substring(0, 3) + '-' + formed.substring(3); } public static void main(String args[]) throws Exception { BufferedReader stdin = new BufferedReader(new InputStreamReader( System.in)); int n = Integer.parseInt(stdin.readLine()); BST root = new BST(formalize(stdin.readLine())); for (int i = 1; i < n; i++) BST.insert(root, formalize(stdin.readLine())); BST.traverse(root); if (!BST.duplicate) System.out.println("No duplicates."); } } Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator