⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 3801943_ac_297ms_2708k.java

📁 北大大牛代码 1240道题的原代码 超级权威
💻 JAVA
字号:
import java.util.HashMap;
import java.util.Scanner;
import java.util.StringTokenizer;

public class Main {

	private HashMap <String, Long> hm = new HashMap <String, Long>();
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		new Main().run();
	}

	private void init() {
		hm.put("zero", 0l);hm.put("one", 1l);hm.put("two", 2l);
		hm.put("three", 3l);hm.put("four", 4l);hm.put("five", 5l);
		hm.put("six", 6l);hm.put("seven", 7l);hm.put("eight", 8l);
		hm.put("nine", 9l);hm.put("ten", 10l);hm.put("eleven", 11l);
		hm.put("twelve", 12l);hm.put("thirteen", 13l);hm.put("fourteen", 14l);
		hm.put("fifteen", 15l);hm.put("sixteen", 16l);hm.put("seventeen", 17l);
		hm.put("eighteen", 18l);hm.put("nineteen", 19l);hm.put("twenty", 20l);
		hm.put("thirty", 30l);hm.put("forty", 40l);hm.put("fifty", 50l);
		hm.put("sixty", 60l);hm.put("seventy", 70l);hm.put("eighty",80l);
		hm.put("ninety", 90l);
	}
	
	private void run() {
		Scanner in = new Scanner(System.in);

		init();
		while (in.hasNext()) {
			String line = in.nextLine();
			if (line.length() == 0) {
				break;
			}
			System.out.println(solve(line));
		}
	}

	private long solve(String line) {
		if (line.indexOf("negative") != -1) {
			return -solve(line.substring(9));
		}
		int index;
		if ((index = line.indexOf("million")) != -1) {
			return 1000000
					* solve(line.substring(0, index - 1))
					+ (index + 7 == line.length() ? 0 : solve(line
							.substring(index + 8)));
		}
		if ((index = line.indexOf("thousand")) != -1) {
			return 1000
					* solve(line.substring(0, index - 1))
					+ (index + 8 == line.length() ? 0 : solve(line
							.substring(index + 9)));
		}
		if ((index = line.indexOf("hundred")) != -1) {
			return 100
					* solve(line.substring(0, index - 1))
					+ (index + 7 == line.length() ? 0 : solve(line
							.substring(index + 8)));
		}
		StringTokenizer st = new StringTokenizer (line);
		long ret = 0;
		while (st.hasMoreTokens()) {
			String tmp = st.nextToken();
			ret += hm.get(tmp);
		}
		return ret;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -