有人做了美图的中文大写数字转阿拉伯数字吗?本地测试了几十个数都好使,然后在线IDE显示测试用例一个没对。。。
发布于 2017-04-16 21:08 2902 次浏览 0 赞 来自 我要提问  
public class Q1 {
	private static Map<Character, Integer> map = new HashMap<>();
	static {
		map.put('个', 0);
		map.put('十', 1);
		map.put('百', 2);
		map.put('千', 3);
		map.put('万', 4);
		map.put('亿', 8);
		map.put('兆', 12);
		map.put('京', 16);
	}
	private static Map<Character, Integer> map0 = new HashMap<>();
	static {
		map0.put('一', 1);
		map0.put('二', 2);
		map0.put('两', 2);
		map0.put('三', 3);
		map0.put('四', 4);
		map0.put('五', 5);
		map0.put('六', 6);
		map0.put('七', 7);
		map0.put('八', 8);
		map0.put('九', 9);
	}

	/* 请完成下面这个函数,实现题目要求的功能 */
	/* 当然,你也可以不按照下面这个模板来作答,完全按照自己的想法来 ^-^ */
	/****************************** 开始写代码 ******************************/
	static long transfer(String str) {
		StringBuilder sb = new StringBuilder("0000000000000000000");
		char init = '个';
		int i = str.length() - 1;

		while (i >= 0) {
			if (map.containsKey(str.charAt(i)) && map.get(str.charAt(i)) >= 4
					&& map.get(str.charAt(i)) > map.get(init)) {
				init = str.charAt(i);

			} else if (map0.containsKey(str.charAt(i))) {
				sb.setCharAt(18 - map.get(init), (char) (map0.get(str.charAt(i)) + 48));

			} else if (map.containsKey(str.charAt(i))) {
				char c = str.charAt(i--);
				if (i < 0 || !map0.containsKey(str.charAt(i))) {
					sb.setCharAt(18 - map.get(init) - map.get(c), (char) (1 + 48));
					i++;
				} else
					sb.setCharAt(18 - map.get(init) - map.get(c), (char) (map0.get(str.charAt(i)) + 48));

			}
			i--;
		}
		return Long.parseLong(sb.toString());
	}

	/****************************** 结束写代码 ******************************/

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		long res;

		String _str;
		try {
			_str = in.nextLine();
		} catch (Exception e) {
			_str = null;
		}

		res = transfer(_str);
		System.out.println(String.valueOf(res));

	}
}


13 条回复

握手~

2017-04-16 21:10

同,这道题他妈100%有问题,我跟管理员反映说是我自己不了解OJ规则。。。。

2017-04-16 21:10

+1本地都过,线上不是WA就是RE

2017-04-16 21:10

一样,本地IDE测试了20多个数据没问题,在线通过率为0,

2017-04-16 21:12

其他都做完了,这题整一个半小时也没对

2017-04-16 21:12

握手~

2017-04-16 21:15

连兆和京都考虑上了

2017-04-16 21:17

+1

2017-04-16 21:18

这个你还真是不了解oj规则,类名必须是Main。你再好好看看。

2017-04-17 06:27
test 回复 test

工作的时候这个很重要,有很多时候不是你的逻辑不对,是没遵循某种规范。

2017-04-17 06:28

二百三这样的用例会有吗?是不是因为第一个用例太奇葩了

2017-04-17 10:02

想知道最后公布的题和答案在哪儿找

2017-04-17 10:37
1
acmcoderU6mfUyci 回复 test

大道理你还是省省吧,类名不是Main会提示修改类名而不是0%通过,楼主这明显是把IDE的代码贴出来的

2017-04-17 11:06
添加回复
回到顶部