Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

看了别人AC的程序,感觉没什么不同呀,怎么还是WA呢??路过高手帮看看

Posted by walkandsing at 2007-03-06 16:16:31 on Problem 1016
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		String input = null;
		while (!(input = in.next()).equals("-1")) {
			String gen = inventory(input);
			if(input.equals(gen)){
				System.out.println(input+" is self-inventorying");
				continue;
			}
			Map<String,Integer> generated = new HashMap<String,Integer>();
			generated.put(gen, 0);
			boolean flag = false;
			for (int i = 1; i < 15; i++) {
				gen = inventory(gen);
				if(generated.containsKey(gen)){
					int index = generated.get(gen);
					if(index == i-1){
						System.out.println(input+" self-inventorying after "+i+" steps");
						flag = true;
						break;
					}else{
						System.out.println(input+" enters an inventory loop of length "+(i-index));
						flag = true;
						break;
					}
				}else{
					generated.put(gen, i);
				}
			}
			if (!flag) {
				System.out.println(input+" can not be classified after 15 iterations");
			}
		}
	}

	static String inventory(String input) {
		int[] digit = new int[10];
		for (int i = 0; i < digit.length; i++) {
			digit[i] = 0;
		}
		for (int i = 0; i < input.length(); i++) {
			digit[(int) input.charAt(i) - (int) '0']++;
		}
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < digit.length; i++) {
			if (digit[i] != 0) {
				sb.append(digit[i]);
				sb.append(i);
			}
		}
		return sb.toString();
	}
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator