题解 | #A + B#用map做个映射就行

A + B

https://www.nowcoder.com/practice/5fb3548802bb4a13a10c2e53a6fbfdd9

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Map<String, Integer> map = new HashMap<>();
        map.put("zero", 0);
        map.put("one", 1);
        map.put("two", 2);
        map.put("three", 3);
        map.put("four", 4);
        map.put("five", 5);
        map.put("six", 6);
        map.put("seven", 7);
        map.put("eight", 8);
        map.put("nine", 9);
        map.put("ten", 10);
        while (sc.hasNextLine()) {
            String[] s = sc.nextLine().split(" ");
            int a = 0, b = 0, i;
            for (i = 0; !s[i].equals("+"); i++) a = a * 10 + map.get(s[i]);
            for (++i; !s[i].equals("="); i++) b = b * 10 + map.get(s[i]);
            if (a + b == 0) break;
            System.out.println(a + b);
        }
    }
}

全部评论

相关推荐

vip牛牛:测试吧,开发现在至少212
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务