欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

电话号码分身

程序员文章站 2022-07-10 14:33:57
...

电话号码分身

题目描述

继MIUI8推出手机分身功能之后,MIUI9计划推出一个电话号码分身的功能:首先将电话号码中的每个数字加上8取个位,然后使用对应的大写字母代替 (“ZERO”, “ONE”, “TWO”, “THREE”, “FOUR”, “FIVE”, “SIX”, “SEVEN”, “EIGHT”, “NINE”), 然后随机打乱这些字母,所生成的字符串即为电话号码对应的分身。

输入描述:

第一行是一个整数T(1 ≤ T ≤ 100)表示测试样例数;接下来T行,每行给定一个分身后的电话号码的分身(长度在3到10000之间)。

输出描述:

输出T行,分别对应输入中每行字符串对应的分身前的最小电话号码(允许前导0)。

解题思路

找出数字英文字母中在剩余数字英文字母中的唯一对应字母即可

代码

import java.io.*;

public class Main {
    public static void main(String[] args)throws Exception{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());
        StringBuilder s = new StringBuilder();
        for(int i=0;i<n;i++){
            int[] count_num = new int[10];
            int[] count_ch = new int[26];
            for(char ch : br.readLine().toCharArray()) count_ch[ch-'A']++;
            //ZERO->2 Z
            count_num[2] = count_ch['Z'-'A'];
            for(int c:"ZERO".toCharArray()) count_ch[c-'A']-=count_num[2];
            //TWO->4 W
            count_num[4] = count_ch['W'-'A'];
            for(int c:"TWO".toCharArray()) count_ch[c-'A']-=count_num[4];
            //FOUR->6 U
            count_num[6] = count_ch['U'-'A'];
            for(int c:"FOUR".toCharArray()) count_ch[c-'A']-=count_num[6];
            // 对SIX->8,找X的个数
            count_num[8] = count_ch['X'-'A'];
            for (char c: "SIX".toCharArray()) count_ch[c-'A'] -= count_num[8];
            // 对EIGHT->0,找G的个数
            count_num[0] = count_ch['G'-'A'];
            for (char c: "EIGHT".toCharArray()) count_ch[c-'A'] -= count_num[0];
            // 对ONE->3,找O的个数
            count_num[3] = count_ch['O'-'A'];
            for (char c: "ONE".toCharArray()) count_ch[c-'A'] -= count_num[3];
            // 对THREE->5,找H的个数
            count_num[5] = count_ch['H'-'A'];
            for (char c: "THREE".toCharArray()) count_ch[c-'A'] -= count_num[5];
            // 对FIVE->7,找F的个数
            count_num[7] = count_ch['F'-'A'];
            for (char c: "FIVE".toCharArray()) count_ch[c-'A'] -= count_num[7];
            // 对SEVEN->9,找S的个数
            count_num[9] = count_ch['S'-'A'];
            for (char c: "SEVEN".toCharArray()) count_ch[c-'A'] -= count_num[9];
            // 对NINE->1,找I的个数
            count_num[1] = count_ch['I'-'A'];
            for (char c: "NINE".toCharArray()) count_ch[c-'A'] -= count_num[1];
            
            for(int j=0;j<10;j++){
                for(int w=0;w<count_num[j];w++){
                    s.append(j);
                }
            }
            s.append("\n");
        }
        System.out.print(s.toString());
        br.close();
    }        
}
相关标签: Leetcode