首页 > 试题广场 >

电话号码分身

[编程题]电话号码分身
  • 热度指数:23444 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
继MIUI8推出手机分身功能之后,MIUI9计划推出一个电话号码分身的功能:首先将电话号码中的每个数字加上8取个位,然后使用对应的大写字母代替 ("ZERO", "ONE", "TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN", "EIGHT", "NINE"), 然后随机打乱这些字母,所生成的字符串即为电话号码对应的分身。

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


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

输入

4
EIGHT
ZEROTWOONE
OHWETENRTEO
OHEWTIEGTHENRTEO

输出

0
234
345
0345
要先依次解码具有独一无二字符的数字,具有这样特点的数字有五个分别是FOUR(U),SIX(X),
TWO(W),EIGHT(G),ZERO(Z),可以根据独特字符的个数直接判断有多少个相应的数字,例如有
3个U那么就一定有3个FOUR...,解码完成这五个数字之后另外的数字也会由于这些数字的移除
而具有了独一无二的字符,这样的数字有FIVE(F),THREE(T),FIVE找到之后,只有SEVEN含
有V,所以又可以依据V字符的个数解码SEVEN的个数,最后剩下的ONE和NINE也具有了自己的
标志性字符分别是ONE(O),NINE(I),需要注意的是原始数字和最终出现的数字还有一个转换
的过程(加8取个位数),所以还要相应转换回去。最后要注意的是,要求每行字符串对应的
分身前的最小电话号码,不要傻傻的按照字典序排序,这样时间复杂度过高,对于这个问题
可以用桶排序,10个bucket分别用于统计0-9出现的次数,最终桶中存储的结果依次输出就是
所有组合中最小的数字。
#include <iostream>
#include <string.h>
#include <vector>
#include <algorithm>
using namespace std;

int main(){
	int i = 0, j = 0;
	int n;
	while (cin >> n){
		string s;	
		for (i = 0; i < n; i++){
			cin >> s;
			vector<int> iimap(256);
			for (j = 0; j < s.size(); j++){
				iimap[s[j]]++;
			}
			vector<int> res(10);
			int count = iimap['U'];
			res[6] = count;
			iimap['F'] -= count;
			iimap['O'] -= count;
			iimap['U'] -= count;
			iimap['R'] -= count;

			count = iimap['X'];
			res[8] = count;
			iimap['S'] -= count;
			iimap['I'] -= count;
			iimap['X'] -= count;

			count = iimap['W'];
			res[4] = count;
			iimap['T'] -= count;
			iimap['W'] -= count;
			iimap['O'] -= count;

			count = iimap['G'];
			res[0] = count;
			iimap['E'] -= count;
			iimap['I'] -= count;
			iimap['G'] -= count;
			iimap['H'] -= count;
			iimap['T'] -= count;

			count = iimap['Z'];
			res[2] = count;
			iimap['Z'] -= count;
			iimap['E'] -= count;
			iimap['R'] -= count;
			iimap['O'] -= count;

			count = iimap['F'];
			res[7] = count;
			iimap['F'] -= count;
			iimap['I'] -= count;
			iimap['V'] -= count;
			iimap['E'] -= count;

			count = iimap['V'];
			res[9] = count;
			iimap['S'] -= count;
			iimap['E'] -= count;
			iimap['V'] -= count;
			iimap['E'] -= count;
			iimap['N'] -= count;

			count = iimap['T'];
			res[5] = count;
			iimap['T'] -= count;
			iimap['H'] -= count;
			iimap['R'] -= count;
			iimap['E'] -= count;
			iimap['E'] -= count;

			count = iimap['O'];
			res[3] = count;
			iimap['O'] -= count;
			iimap['N'] -= count;
			iimap['E'] -= count;

			count = iimap['I'];
			res[1] = count;
			iimap['N'] -= count;
			iimap['I'] -= count;
			iimap['N'] -= count;
			iimap['E'] -= count;

			for (int k = 0; k < res.size(); k++){
				for (int l = 0; l < res[k]; l++)
					cout << k;
			}
			cout << endl;
		}
	}
	return 0;
}

编辑于 2017-08-24 15:49:06 回复(18)
**,**题

import java.util.Scanner;

/**
 * Created by Evergreen on 2017/8/10.
 */
public class Main {
    public static void handle(String str){
        str=str.toLowerCase();
        int[] hash=new int[10];
        StringBuffer sb=new StringBuffer(str);
        while(sb.toString().contains("z")){//zero
            //2
            hash[2]++;
            sb.deleteCharAt(sb.indexOf("z"));
            sb.deleteCharAt(sb.indexOf("e"));
            sb.deleteCharAt(sb.indexOf("r"));
            sb.deleteCharAt(sb.indexOf("o"));
        }
        while(sb.toString().contains("x")){//six
            hash[8]++;
            sb.deleteCharAt(sb.indexOf("s"));
            sb.deleteCharAt(sb.indexOf("i"));
            sb.deleteCharAt(sb.indexOf("x"));
        }
        while(sb.toString().contains("s")){//seven
            hash[9]++;
            sb.deleteCharAt(sb.indexOf("s"));
            sb.deleteCharAt(sb.indexOf("e"));
            sb.deleteCharAt(sb.indexOf("v"));
            sb.deleteCharAt(sb.indexOf("e"));
            sb.deleteCharAt(sb.indexOf("n"));
        }
        while(sb.toString().contains("u")){//four
            hash[6]++;
            sb.deleteCharAt(sb.indexOf("f"));
            sb.deleteCharAt(sb.indexOf("o"));
            sb.deleteCharAt(sb.indexOf("u"));
            sb.deleteCharAt(sb.indexOf("r"));
        }
        while(sb.toString().contains("f")){//five
            hash[7]++;
            sb.deleteCharAt(sb.indexOf("f"));
            sb.deleteCharAt(sb.indexOf("i"));
            sb.deleteCharAt(sb.indexOf("v"));
            sb.deleteCharAt(sb.indexOf("e"));
        }
        while(sb.toString().contains("g")){//eight
            hash[0]++;
            sb.deleteCharAt(sb.indexOf("e"));
            sb.deleteCharAt(sb.indexOf("i"));
            sb.deleteCharAt(sb.indexOf("g"));
            sb.deleteCharAt(sb.indexOf("h"));
            sb.deleteCharAt(sb.indexOf("t"));
        }
        while(sb.toString().contains("w")){//two
            hash[4]++;
            sb.deleteCharAt(sb.indexOf("t"));
            sb.deleteCharAt(sb.indexOf("w"));
            sb.deleteCharAt(sb.indexOf("o"));
        }
        while(sb.toString().contains("h")&&!sb.toString().contains("g")){//three
            hash[5]++;
            sb.deleteCharAt(sb.indexOf("t"));
            sb.deleteCharAt(sb.indexOf("h"));
            sb.deleteCharAt(sb.indexOf("r"));
            sb.deleteCharAt(sb.indexOf("e"));
            sb.deleteCharAt(sb.indexOf("e"));
        }
        while(sb.toString().contains("o")&&!sb.toString().contains("z")){//one
            hash[3]++;
            sb.deleteCharAt(sb.indexOf("o"));
            sb.deleteCharAt(sb.indexOf("n"));
            sb.deleteCharAt(sb.indexOf("e"));
        }
        while(sb.toString().contains("n")) {//nine
            hash[1]++;
            sb.deleteCharAt(sb.indexOf("n"));
            sb.deleteCharAt(sb.indexOf("i"));
            sb.deleteCharAt(sb.indexOf("n"));
            sb.deleteCharAt(sb.indexOf("e"));
        }
        for(int i=0;i<10;i++){
            for(int j=1;j<=hash[i];j++){
                System.out.print(i);
            }
        }
        System.out.println();
    }
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        while(sc.hasNextInt()){
            int n=sc.nextInt();
            for(int i=0;i<n;i++)
                handle(sc.next());
        }
    }
}


发表于 2017-08-10 14:23:11 回复(18)

思路基本是一楼的思路 但是他的代码太长了

实际上0 2 4 6 8这几个数字可以通过Z W U X G唯一确定  
剩下的1 3 5 7可以通过O T F S唯一确定 
而剩下的9随便哪个都可以确定 姑且用I
将这些按顺序写成一个表
这些都可以打表来简化代码 代码如下


#include<iostream>
#include<string>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std;
string a[10] = {"ZERO", "TWO", "FOUR", "SIX", "EIGHT",
                "ONE", "THREE", "FIVE", "SEVEN",  
                "NINE"};
int num_map[] = {0, 2, 4, 6, 8, 1, 3, 5, 7, 9}; 
char dic[] = {'Z', 'W', 'U', 'X', 'G', 'O', 'T', 'F', 'S', 'I'};
int ha[26];
vector<int> ans;
int main(){
    int T; scanf("%d", &T);
    while(T--){
        string s; cin>>s;
        ans.clear();
        memset(ha, 0, sizeof(ha));
        for(int i = 0; i < s.length(); i++) ha[s[i]-'A']++;
        for(int i = 0; i < 10; i++){
            int times = ha[dic[i]-'A'];
            if(times > 0){
                for(int j = 0; j < a[i].size(); j++) 
                    ha[a[i][j]-'A'] -= times;
                for(int k = times; k > 0; k--)
                    ans.push_back((num_map[i]+2) % 10);
            }
        }
        sort(ans.begin(), ans.end());
        for(int i=0;i<ans.size();i++) printf("%d", ans[i]);
        printf("\n");
    }
}
编辑于 2018-10-03 20:00:55 回复(0)
    统计拥有的字母数量,然后可以找到规律,只有0(ZERO)有字母Z,也就是说,有多少个Z就有多少个0。同理,只有2有字母W,4有字母U,6有字母X,8有字母G。

    将上述单词从字母统计中删去,发现剩下的单词中,只有5有字母F,1有字母O,3有字母R,7有字母S。

    再删去,发现只有9有字母I。

    将以上数字统计后排序可得。

import java.util.*;
import java.lang.*;

public class Main {
    static String[] table = { "ZERO", "ONE", "TWO", "THREE", "FOUR", "FIVE",
            "SIX", "SEVEN", "EIGHT", "NINE" };
    static int retL = 0;
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int i, j;
        int n = sc.nextInt();
        sc.nextLine();
        String str;
        int[] mark = new int[26];
        int[] ret = new int[10000];
        int temp = 0;
        while (sc.hasNext()) {
            retL = 0;
            str = sc.nextLine();
            for(i=0;i<str.length();i++) {
                mark[str.charAt(i)-'A']++;
            }

            if(mark['Z' - 'A'] != 0) handle(mark, ret, 2, 'Z');
            if(mark['W' - 'A'] != 0) handle(mark, ret, 4, 'W');
            if(mark['U' - 'A'] != 0) handle(mark, ret, 6, 'U');
            if(mark['X' - 'A'] != 0) handle(mark, ret, 8, 'X');
            if(mark['G' - 'A'] != 0) handle(mark, ret, 0, 'G');
            if(mark['F' - 'A'] != 0) handle(mark, ret, 7, 'F');
            if(mark['O' - 'A'] != 0) handle(mark, ret, 3, 'O');
            if(mark['R' - 'A'] != 0) handle(mark, ret, 5, 'R');
            if(mark['S' - 'A'] != 0) handle(mark, ret, 9, 'S');
            if(mark['I' - 'A'] != 0) handle(mark, ret, 1, 'I');

            Arrays.sort(ret,0,retL);
            for (i = 0; i < retL; i++) {
                System.out.print(ret[i]);
            }
            System.out.println();
        }
    }

    public static void handle(int[] mark, int[] ret, int num, char point) {
        int temp = mark[point - 'A'],i;
        for (i = 0; i < temp; i++) {
            ret[retL++] = num;
        }
        for (i = 0; i < table[(num+8)%10].length(); i++) {
            mark[table[(num+8)%10].charAt(i) - 'A'] -= temp;
        }
    }
}
编辑于 2017-08-09 05:11:54 回复(1)
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Main {     public static void main(String[] args) {         // TODO Auto-generated method stub
        Scanner sc=new Scanner(System.in);
        int num=Integer.parseInt(sc.nextLine());//行数
        String[] sArr=new String[num];//构建一个String数组存放输入的每行字符串
        for(int i=0;i<num;i++) {
            sArr[i]=sc.nextLine();
        }
        for(String each:sArr) {
        //构建一个map用于存储代表性字母的个数
        //0-->Z; 2-->W; 4-->U; 6-->X; 8-->G;(唯一对应,每个字母的个数就代表其数字的个数)
        //1-->O(1的个数计算方法:num(O)-num(0)-num(2)-num(4)= map(O)-map(Z)-map(W)-map(U))
        //3-->R(3的个数计算方法:num(R)-num(0)-num(4)= map(R)-map(Z)-map(U))
        //5-->F(5的个数计算方法:num(F)-num(4)= map(F)-map(U))
        //7-->S(7的个数计算方法:num(S)-num(6)= map(S)-map(X))
        //9-->N(注意,9里面有两个N,故计算结果应除以2:(num(N)-num(1)-num(7))/2=(map(N)-count(1)-count(7))/2)
            Map<Character,Integer> map=new HashMap<Character,Integer>() {
                {
                    put('Z',0);
                    put('W',0);
                    put('U',0);
                    put('X',0);
                    put('G',0);
                    put('O',0);
                    put('R',0);
                    put('F',0);
                    put('S',0);
                    put('N',0);                     }
            };
            for(int i=0;i<each.length();i++) {//统计当前字符串中有代表性字母的个数
                char c=each.charAt(i);
                if(map.containsKey(c))
                    map.put(c, 1+map.get(c));
                else
                    continue;//遇到不在字典里的字母就跳过
            }
            int[] count=new int[10];
            count[0]=map.get('Z');
            count[2]=map.get('W');
            count[4]=map.get('U');
            count[6]=map.get('X');
            count[8]=map.get('G');
            count[1]=map.get('O')-map.get('Z')-map.get('W')-map.get('U');
            count[3]=map.get('R')-map.get('Z')-map.get('U');
            count[5]=map.get('F')-map.get('U');
            count[7]=map.get('S')-map.get('X');
            count[9]=(map.get('N')-count[1]-count[7])/2;//NINE中有两个N
            int[] countnew=new int[10];
            for(int i=0;i<10;i++){
                countnew[i]=count[(i+8)%10];//使用countnew数组存储加8以前的个数
            }
            for(int i=0;i<10;i++) {//按照从0到9的顺序输出
                for(int j=0;j<countnew[i];j++) {//每个数字输出的次数
                    System.out.print(i);
                }
            }
            System.out.println();//输出完一个字符串换行
        }     }
}

发表于 2018-07-26 09:04:35 回复(0)
一张图说明一切问题
发表于 2018-03-29 16:44:43 回复(2)
#include<stdio.h>
#include<string.h>
int v[10]={2,4,6,8,0,3,5,7,9,1},n,i,j,hash[10],tmp[26],cnt[26];
char dst[10][10]={"ZERO","TWO","FOUR","SIX",
"EIGHT","ONE","THREE","FIVE","SEVEN","NINE"},s[10005];
void dfs(int *x){
    int i,cnt=0,j;
    for(i=0;i<26;i++)
        if(x[i]==0) cnt++;
    if(cnt==26){
        for(i=0;i<10;i++)
            for(j=0;j<hash[i];j++) printf("%d",i); printf("\n");
        return;
    }
    for(i=0;i<26;i++) tmp[i]=x[i];
    for(i=0;i<10;i++){
        int flag=1;
        for(j=0;j<dst[i][j]!='\0';j++){
            x[dst[i][j]-'A']--;
            if(x[dst[i][j]-'A']<0){
                flag=0;break;
            }
        }
        if(flag==1){
            hash[v[i]]++,dfs(x);break;
        }
        for(j=0;j<26;j++) x[j]=tmp[j];
    }
}
int main(){
    for(scanf("%d",&n);n--;dfs(cnt),memset(hash,0,sizeof(hash))){
        scanf("%s",s);
        memset(cnt,0,sizeof(cnt));
        for(j=0;s[j]!='\0';j++) cnt[s[j]-'A']++;
    }
}//dfs删除,注意删除顺序,0 2 4 6 8是具备唯一特征字母的
 //排除0 2 4 6 8,之后,1 3 5 7是具备唯一特征的,最后就剩9了

编辑于 2017-10-23 08:49:06 回复(0)
var n=readline();
while(str=readline()){     changedNum(str);
}
function changedNum(str){
var reg1=[/Z/g,/W/g,/U/g,/X/g,/G/g];//0,2,4,6,8;
var reg2=[/O/g,/H/g,/F/g,/S/g,/N/g];//1,3,5,7,9;
var result=[];
var num_arr=new Array(10).fill(0);
for(let i=0,j=0;i<reg1.length;i++){     var num=str.match(reg1[i]);     if(num){         num_arr[i+j]=num.length;     }     j++;
}
for(let i=0,j=1;i<reg2.length;i++){     var num=str.match(reg2[i]);     switch(i){         case 0:{//1="O"-0-2-4             if(num){                 num_arr[i+j]=num.length-num_arr[0]-num_arr[2]-num_arr[4];             }             break;         }         case 1:{//3="H"-8             if(num){                 num_arr[i+j]=num.length-num_arr[8];             }             break;         }         case 2:{//5="F"-4             if(num){                 num_arr[i+j]=num.length-num_arr[4];             }             break;         }         case 3:{//7="S"-6             if(num){                 num_arr[i+j]=num.length-num_arr[6];             }             break;         }         case 4:{//9=("N"-1-7)/2             if(num){                 num_arr[i+j]=(num.length-num_arr[1]-num_arr[7])/2;             }             break;         }         default:
            break;     }         j++;
}
for(let i=0;i<num_arr.length;i++){     for(let j=0;j<num_arr[i];j++){         if(i>=8){             result.push(i-8);                     }else{             result.push(i+2);         }             }     }
console.log(result.sort((x,y)=>{return x-y}).join(""));
}

发表于 2018-07-31 19:29:54 回复(0)
一开始忘记“’NINE”是两个N了,9的时候在数N             /(ㄒoㄒ)/~~
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;
int main()
{
	char ch[] = { 'Z', 'W', 'X', 'S', 'U', 'O', 'R', 'F', 'G', 'I' };
	int dig[] = { 0, 2, 6, 7, 4, 1, 3, 5, 8, 9 };
	string ch_all[] = { "ZERO", "TWO", "SIX", "SEVEN", "FOUR", "ONE", "THREE", "FIVE", "EIGHT", "NINE" };
	int T;
	for (auto &i : dig)
	{
		i-= 8;
		if (i < 0)
			i+= 10;
	}
	
	while (cin >> T)
	{
		while (T--)
		{
			int count[26] = { 0 };
			string str;
			cin >> str;
			vector<int> dig_cout;
			for (auto i : str)
			{
				count[i - 'A']++;
			}
			for (int i = 0; i < 10; i++)
			{
				int tmp_c = count[ch[i] - 'A'];
				if (tmp_c>0)
					for (auto j : ch_all[i])
						count[j - 'A'] -= tmp_c;
				while (tmp_c--)
					dig_cout.push_back(dig[i]);		
			}
			sort(dig_cout.begin(), dig_cout.end());
			for (auto i : dig_cout)
				cout << i;
			cout << endl;
		}	
	}
	return 0;
}

发表于 2017-08-21 17:32:48 回复(1)
#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n;cin>>n;
    while(n--)
    {
        string s;
        cin>>s;
        vector<int> v;
        int a[10]={0};
        while(s.find('Z')!=s.npos)
        {
            s.erase(s.find('Z'),1);
            s.erase(s.find('E'),1);
            s.erase(s.find('R'),1);
            s.erase(s.find('O'),1);
            a[2]++;
        }
         while(s.find('G')!=s.npos)
        {
            s.erase(s.find('G'),1);
            s.erase(s.find('E'),1);
            s.erase(s.find('I'),1);
            s.erase(s.find('H'),1);
            s.erase(s.find('T'),1);a[0]++;
        }
         while(s.find('X')!=s.npos)
        {
            s.erase(s.find('X'),1);
            s.erase(s.find('I'),1);
            s.erase(s.find('S'),1); a[8]++;
        }
        while(s.find('S')!=s.npos)
        {
            s.erase(s.find('V'),1);
            s.erase(s.find('E'),1);
            s.erase(s.find('E'),1);
            s.erase(s.find('S'),1);
            s.erase(s.find('N'),1);a[9]++;
        }
        while(s.find('W')!=s.npos)
        {
            s.erase(s.find('W'),1);
            s.erase(s.find('T'),1);
            s.erase(s.find('O'),1);a[4]++;
        }
        while(s.find('U')!=s.npos)
        {
            s.erase(s.find('F'),1);
            s.erase(s.find('U'),1);
            s.erase(s.find('O'),1);
            s.erase(s.find('R'),1);a[6]++;
        }
        while(s.find('F')!=s.npos)
        {
            s.erase(s.find('F'),1);
            s.erase(s.find('I'),1);
            s.erase(s.find('V'),1);
            s.erase(s.find('E'),1);a[7]++;
        }
        while(s.find('T')!=s.npos)
        {
            s.erase(s.find('T'),1);
            s.erase(s.find('H'),1);
            s.erase(s.find('R'),1);
            s.erase(s.find('E'),1);
            s.erase(s.find('E'),1);a[5]++;
        }
        while(s.find('I')!=s.npos)
        {
            s.erase(s.find('N'),1);
            s.erase(s.find('I'),1);
            s.erase(s.find('N'),1);
            s.erase(s.find('E'),1);a[1]++;
        }
        a[3]=s.length()/3;
        for(int i=0;i<10;i++)
            v.insert(v.begin(),a[i],i );
        sort(v.begin(), v.end());
        for(int i=0;i<v.size();i++)    cout<<v[i];
        cout<<endl;
   
    }
}

很LOW 的题,只要复制粘贴的快,再长也是一分钟的事。
发表于 2022-04-06 20:54:00 回复(0)
#coding=utf-8
n=int(input())
di=[[['Z','ZERO',2],['W','TWO',4],['U','FOUR',6],['X','SIX',8],['G','EIGHT',0]], #查询字典,分2部分,'ZERO','TWO'等包含特定唯一字符'z','w'
    [['O','ONE',3],['R','THREE',5],['F','FIVE',7],['S','SEVEN',9]]]              #'ONE','THREE'去掉上面的唯一字符后,也只包含特定唯一字符'O','R'
for i in range(n):                                                               #最后只剩'NINE'需要特殊处理
    num=list(input()) #输入
    num.sort() #排序
    re=[]
    for j in range(2): #调用字典
        for d in di[j]: #字典内部遍历
            if d[0] in num: #是否包含特定唯一字符
                k=num.count(d[0]) #唯一特定字符个数就是代表数字个数,也是其他字母个数,如有k个'Z'就有k个'ERO'
                for d0 in d[1]:
                    s=num.index(d0)
                    del num[s:s+k] #删除k个'ZERO'
                re+=[d[2]]*k #代表的数字(减去8)
    re+=[1]*(int(len(num)/4)) #剩余都是NINE
    re.sort()#排序,找到最小的
    re=[str(i) for i in re]
    re=''.join(re)#组合
    print(re)
发表于 2020-08-05 22:47:27 回复(0)
def func(x): #计算乱序字母中的ZERO,ONE,TWO...NINE的数量
dict = [ 0 for i in range(10)]
tab = [ 0 for i in range(10)]

num_Z = x.count('Z') #0 ZERO
num_W = x.count('W') #2 TWO
num_U = x.count('U') #4 FOUR
num_X = x.count('X') #6 SIX
num_G = x.count('G') #8 EIGHT
num_S = x.count('S') - num_X  # 7 SEVEN
num_F = x.count('F') - num_U  # 5 FIVE
num_T = x.count('T') - num_W - num_G # 3 THREE
num_O = x.count('O') - num_Z - num_W - num_U # 1 ONE
num_I = x.count('I') - num_F - num_X - num_G # 9 NINE

dict[0] = num_Z
dict[1] = num_O
dict[2] = num_W
dict[3] = num_T
dict[4] = num_U
dict[5] = num_F
dict[6] = num_X
dict[7] = num_S
dict[8] = num_G
dict[9] = num_I

tab[0] = dict[8]
tab[1] = dict[9]
tab[2] = dict[0]
tab[3] = dict[1]
tab[4] = dict[2]
tab[5] = dict[3]
tab[6] = dict[4]
tab[7] = dict[5]
tab[8] = dict[6]
tab[9] = dict[7]
s = ""
for i in range(len(tab)):
    s += str(i)*tab[i]
return s

import sys
x = sys.stdin.readlines()
for i in range(1,len(x)):
print(func(x[i]))

编辑于 2018-09-05 10:38:15 回复(0)
//特别要注意9 nine 不能用n去判断除非除以2
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * @author Y.bear
 * @version
创建时间:2018年9月3日 下午2:54:32 类说明
 */
public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        int times = Integer.valueOf(reader.readLine());
        String[] results=new String[times];
        for (int i = 0; i <times; i++) {
            int[] result = new int[10];
            String readLine = reader.readLine();
            transform(readLine, result);    
            print(result,i,results);
        }
        for (String string : results) {
            System.out.println(string);
        }

    }

    public static void print(int[] result,int index,String[] results) {
        StringBuffer buffer=new StringBuffer();
        for (int i = 0; i < result.length; i++) {
            while (result[i] > 0) {
                result[i]--;
                buffer.append(i);
            }
        }
        results[index]=buffer.toString();
        
    }

    public static void getResult(int num, int count, int[] result) {
        while (count > 0) {
            count--;
            result[(num + 2) % 10]++;
        }
    }

    public static void transform(String line, int[] result) {
        char[] letters = new char[26];
        char[] charArray = line.toUpperCase().toCharArray();
        for (char c : charArray) {
            letters[c - 'A']++;
        }
        int zero = letters['Z' - 'A'];
        getResult(0, zero, result);
        letters['Z' - 'A'] -= zero;
        letters['E' - 'A'] -= zero;
        letters['R' - 'A'] -= zero;
        letters['O' - 'A'] -= zero;

        int two = letters['W' - 'A'];
        getResult(2, two,result);
        letters['T' - 'A'] -= two;
        letters['W' - 'A'] -= two;
        letters['O' - 'A'] -= two;

        int four = letters['U' - 'A'];
        getResult(4, four, result);
        letters['F' - 'A'] -= four;
        letters['O' - 'A'] -= four;
        letters['U' - 'A'] -= four;
        letters['R' - 'A'] -= four;

        int six = letters['X' - 'A'];
        getResult(6, six, result);
        letters['S' - 'A'] -= six;
        letters['I' - 'A'] -= six;
        letters['X' - 'A'] -= six;

        int eight = letters['G' - 'A'];
        getResult(8, eight, result);
        letters['E' - 'A'] -= eight;
        letters['I' - 'A'] -= eight;
        letters['G' - 'A'] -= eight;
        letters['H' - 'A'] -= eight;
        letters['T' - 'A'] -= eight;

        int one = letters['O' - 'A'];
        getResult(1, one, result);
        letters['O' - 'A'] -= one;
        letters['N' - 'A'] -= one;
        letters['E' - 'A'] -= one;

        int three = letters['T' - 'A'];
        getResult(3, three, result);
        letters['T' - 'A'] -= three;
        letters['H' - 'A'] -= three;
        letters['R' - 'A'] -= three;
        letters['E' - 'A'] -= three;
        letters['E' - 'A'] -= three;

        int five = letters['F' - 'A'];
        getResult(5, five, result);
        letters['F' - 'A'] -= five;
        letters['I' - 'A'] -= five;
        letters['V' - 'A'] -= five;
        letters['E' - 'A'] -= five;

        int seven = letters['S' - 'A'];
        getResult(7, seven, result);
        letters['S' - 'A'] -= seven;
        letters['E' - 'A'] -= seven;
        letters['V' - 'A'] -= seven;
        letters['E' - 'A'] -= seven;
        letters['N' - 'A'] -= seven;

        int nine = letters['I' - 'A'];
        getResult(9, nine, result);
        letters['N' - 'A'] -= nine;
        letters['I' - 'A'] -= nine;
        letters['N' - 'A'] -= nine;
        letters['E' - 'A'] -= nine;

    }
}
 
发表于 2018-09-03 16:48:21 回复(0)
//number代表字符串,bucket是转化后的数组
#include <iostream>
#include <string>
#include <map>
 using namespace std;

int main(){
    
    int n;
    cin>>n;
    while(n--){
        string s;
        cin >> s;
        map <char,int> number;
        int* bucket = new int [10];
        for(int i = 0; i < s.length(); i++)
            number[s[i]]++;
        
        bucket[2] = number['Z'];
        bucket[4] = number['W'];
        bucket[6] = number['U'];
        bucket[8] = number['X'];
        bucket[0] = number['G'];
        
        bucket[3] = number['O'] - bucket[2] - bucket[4] - bucket[6];
        bucket[5] = number['R'] - bucket[2] - bucket[6];
        bucket[7] = number['F'] - bucket[6];
        bucket[9] = number['S'] - bucket[8];
        
        bucket[1] = number['I'] - bucket[7] - bucket[8] - bucket[0];
        
        for(int i = 0;i<10;i++){
            while(bucket[i]--)
                cout<<i;
        }
        cout<<endl;
    }
    
    
    return 0;
}

发表于 2018-04-17 20:39:17 回复(0)
//丧心病狂的题目
import java.util.Scanner; public class Main { public static void main(String[] args) {
        Scanner in = new Scanner(System.in);  int t = Integer.parseInt(in.nextLine());  for (int i = 0; i < t; i++) { doWork(in);
        }
    } public static void doWork(Scanner in) {
        String s = in.nextLine(); int[] cs = new int[27]; for (int i = 0; i < s.length(); i++) {
            cs[s.charAt(i) - 'A']++;
        } int[] num = new int[10];
        num[0] = cs['Z' - 'A'];
        num[6] = cs['X' - 'A'];
        num[2] = cs['W' - 'A'];
        num[4] = cs['U' - 'A'];
        num[8] = cs['G' - 'A'];
        num[3] = cs['T' - 'A'] - num[2] - num[8];
        num[7] = cs['S' - 'A'] - num[6];
        num[5] = cs['V' - 'A'] - num[7];
        num[1] = cs['O' - 'A'] - num[0] - num[2] - num[4];
        num[9] = (cs['N' - 'A'] - num[7] - num[1]) / 2;
        StringBuilder builder = new StringBuilder(); for (int i = 8; i <=9; i++) { for (int j = 0; j < num[i]; j++) {
                builder.append((i+2)%10);
            }
        } for (int i = 0; i <=7; i++) { for (int j = 0; j < num[i]; j++) {
                builder.append(i+2);
            }
        }
        System.out.println(builder.toString());
    }
}


发表于 2018-03-07 21:10:19 回复(0)
我的也是10%,我感觉测试数据给错了……
测试数据:
84
……
但是结果并没有84个测试数据,只有5个。
我的程序思路和大家大同小异:判断关键字。
比如ZERO,关键字为Z,那么数组a[10]内a[0]++,a[n]表示n的个数。
假如其他字母有O作为关键字,比如ONE,那么遇到Z,就执行:a[0]++,a[1]--;
a[1]--,就是要抵消掉ZERO里面的O给ONE带来的影响。

C++程序:

#include <iostream>
#include <string>
using namespace std;

//输入字符串str,返回对字符串总数字统计的结果a[10]
void check(int a[], string str);
//输出统计后的数组,比如a[0]=3,就输出3个0
void out_num(int a[10]);

int main() {
    int num;
    string str;
    int a[100][10] = { 0 };
    //网上测试,会用文件代替cin
    while (cin>>num) {
        for (int k = 0; k < num; k++) {
            a[k][10] = { 0 };
            cin >> str;
            check(a[k], str);
        }
        for (int i = 0; i < num; i++) {
            out_num(a[i]);
        }
    }
}

void out_num(int a[10]) {
    //输出数字,8→0,9→1,0→2……
    for (int k = 0; k < a[8]; k++)
        cout << 0;
    for (int k = 0; k < a[9]; k++)
        cout << 1;
    for (int j = 0; j < 8; j++)
        for (int k = 0; k < a[j]; k++)
            cout << j + 2;
    cout<<endl;
}

void check(int a[], string str) {
    int i = 0;
    //假如用scanf,就要用\0判断结束标志,预留一个字节系统自动放置\0;
    //假如用gets,就用\n判断;因为它会把你的\n也会读过来。
    while (str[i] != '\0') {
        switch (str[i]) {
        case 'Z':
            a[0]++;
            a[1]--;    //O
            break;
        case 'O':
            a[1]++;
            break;
        case 'W':
            a[2]++;
            a[1]--;    //O
            break;
        case 'H':
            a[3]++;
            break;
        case 'U':
            a[4]++;
            a[1]--;    //O
            break;
        case 'V':
            a[5]++;
            a[9]--;    //I
            break;
        case 'X':
            a[6]++;
            a[7]--;    //S
            a[9]--;    //I
            break;
        case 'S':
            a[7]++;
            a[5]--;    //V
            break;
        case 'G':
            a[8]++;
            a[3]--;    //H
            a[9]--;    //I
            break;
        case 'I':
            a[9]++;
            break;
        default:
            break;
        }
        i++;
    }
}
发表于 2018-02-05 16:13:22 回复(2)
提示格式错误,但是没找出来,有人能帮忙看看吗?
import java.util.Scanner;

public class Number {
    public static void main(String[] argv) {
        Scanner sin = new Scanner(System.in);
        int count = sin.nextInt();
        while (count-- >= 0) {

            String str = sin.nextLine();
            int[] stringNumber = new int[26];
            String reString = "";
            String[] array = { "EIGHT", "NINE", "ZERO", "ONE", "TWO", "THREE",
                    "FOUR", "FIVE", "SIX", "SEVEN" };

            char[] strs = str.toCharArray();
            for (int i = 0; i < strs.length - 1; i++) {
                stringNumber[strs[i] - 'A'] += 1;
            }

            for (int i = 0; i < array.length; i++) {
                reString = reString
                        + dealString(stringNumber, array[i], i + "");
            }
            System.out.printf(reString + "\n");

        }
    }

    private static String dealString(int[] stringNumber, String str, String tag) {
        String string = "";
        char[] numStrs = str.toCharArray();

        for (int i = 0; i < numStrs.length - 1; i++) {
            if (stringNumber[numStrs[i] - 'A'] <= 0) {
                return string;
            }
        }
        for (int i = 0; i < numStrs.length - 1; i++) {
            stringNumber[numStrs[i] - 'A'] -= 1;
        }
        return tag + dealString(stringNumber, str, tag);
    }
}
 
发表于 2018-01-30 23:13:30 回复(0)
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main()
{     char ch[] = {'Z','W','X','S','U','O','R','F','G','I'};     int d[] = {0,2,6,7,4,1,3,5,8,9};     string num[] = {"ZERO","TWO","SIX","SEVEN","FOUR","ONE","THREE","FIVE","EIGHT","NINE"};     int T;          for(int i=0;i<10;i++)     {         d[i] -= 8;         if(d[i]<0)             d[i] += 10;     }          while(cin>>T)         {         while(T--)         {             int count[26]={0};             string s;             cin>>s;             vector<int> result;             for(int i=0;i<s.length();i++)                 count[s[i]-'A']++;             for(int i=0;i<10;i++)             {                 int t = count[ch[i]-'A'];                 if(t>0)                     for(int j=0;j<num[i].length();j++)                         count[num[i][j]-'A'] -= t;                 while(t--)                     result.push_back(d[i]);             }             sort(result.begin(), result.end());             for(int i=0;i<result.size();i++)                 cout<<result[i];             cout<<endl;         }     }     return 0;
}

发表于 2018-01-17 01:19:33 回复(0)
#include 
#include 
#include 
#include 
#include 
using namespace std;
typedef struct 
{
    int num;
    char index;
    string numword;
    int numcnt;
}node;
void handle(string &numstr)
{
    vector nodevec(10);
    nodevec[0].num = 2;    
    nodevec[0].index = 'Z';    
    nodevec[0].numword = "ZERO";
    nodevec[1].num = 4;    
    nodevec[1].index = 'W';    
    nodevec[1].numword = "TWO";
    nodevec[2].num = 6;    
    nodevec[2].index = 'U';    
    nodevec[2].numword = "FOUR";
    nodevec[3].num = 8;    
    nodevec[3].index = 'X';    
    nodevec[3].numword = "SIX";
    nodevec[4].num = 0;    
    nodevec[4].index = 'G';    
    nodevec[4].numword = "EIGHT";
    nodevec[5].num = 3;    
    nodevec[5].index = 'O';    
    nodevec[5].numword = "ONE";
    nodevec[6].num = 5;    
    nodevec[6].index = 'T';    
    nodevec[6].numword = "THREE";
    nodevec[7].num = 7;    
    nodevec[7].index = 'F';    
    nodevec[7].numword = "FIVE";
    nodevec[8].num = 9;    
    nodevec[8].index = 'S';    
    nodevec[8].numword = "SEVEN";
    nodevec[9].num = 1;    
    nodevec[9].index = 'E';    
    nodevec[9].numword = "NINE";
    vector res;
    for(vector::iterator iter = nodevec.begin();iter != nodevec.end();++iter)
    {
        node n = *iter;
        n.numcnt = count_if(numstr.begin(), numstr.end(), bind2nd(equal_to(), n.index));
        for(int k = 0;k < n.numcnt;k++)
        {
            for(string::iterator iter2 = n.numword.begin();iter2 != n.numword.end();++iter2)
            {
                numstr.erase(find_if(numstr.begin(), numstr.end(), bind2nd(equal_to(), *iter2)));
            }
            res.push_back(n.num);
        }
    }
    sort(res.begin(), res.end(), less());
    copy(res.begin(), res.end(), ostream_iterator(cout));
    cout << endl;
}
int main()
{
    int cnt;
    while(cin >> cnt)
    {
        vector strvec(cnt);
        for(int i = 0;i < cnt;i++)
        {
            cin >> strvec[i];
        }
        for(int i = 0;i < cnt;i++)
        {
            string numstr = strvec[i];
            handle(numstr);
        }
    }
    return 0;
}

时间:2018年8月14日

#include <iostream>
#include <string>
#include <map>
#include <vector>
using namespace std;

vector<int> func(string str) {
    map<int, int> resmap;
    map<char, int> amap;
    for(int i = 0;i < str.size();i++) {
        amap[str[i]]++;
    }
    //2
    resmap[2] += amap['Z'];
    for(int i = 0;i < resmap[2];i++) {
        amap['Z']--;
        amap['E']--;
        amap['R']--;
        amap['O']--;
    }

    resmap[4] += amap['W'];
    for(int i = 0;i < resmap[4];i++) {
        amap['T']--;
        amap['W']--;    
        amap['O']--;    
    }

    resmap[6] += amap['U'];
    for(int i = 0;i < resmap[6];i++) {
        amap['F']--;    
        amap['O']--;    
        amap['U']--;        
        amap['R']--;    
    }

    resmap[8] += amap['X'];
    for(int i = 0;i < resmap[8];i++) {
        amap['S']--;    
        amap['I']--;
        amap['X']--;
    }

    resmap[0] += amap['G'];
    for(int i = 0;i < resmap[0];i++) {
        amap['E']--;
        amap['I']--;
        amap['G']--;
        amap['H']--;
        amap['T']--;
    }

    resmap[3] += amap['O'];
    for(int i = 0;i < resmap[3];i++) {
        amap['O']--;
        amap['N']--;
        amap['E']--;
    }

    resmap[5] += amap['R'];
    for(int i = 0;i < resmap[5];i++) {
        amap['T']--;    
        amap['H']--;    
        amap['R']--;    
        amap['E']--;    
        amap['E']--;
    }

    resmap[7] += amap['F'];
    for(int i = 0;i < resmap[7];i++) {
        amap['F']--;    
        amap['I']--;    
        amap['V']--;    
        amap['E']--;        
    }

    resmap[9] += amap['V'];
    for(int i = 0;i < resmap[9];i++) {
        amap['S']--;
        amap['E']--;
        amap['V']--;
        amap['E']--;
        amap['N']--;    
    }

    resmap[1] += amap['I'];
    for(int i = 0;i < resmap[1];i++) {
        amap['N']--;
        amap['I']--;
        amap['N']--;
        amap['E']--;
    }

    vector<int> vec;
    for(auto i : resmap) {
        for(int j = 0;j < i.second;j++){
            vec.push_back(i.first);
        }
    }

    return vec;
}


int main() {
    int n;
    cin >> n;
    for(int i = 0;i < n;i++) {
        string str;
        cin >> str;
        vector<int> vec = func(str);

        for(auto num : vec) {
            cout << num;
        }
        cout << endl;
    }

    return 0;
}
编辑于 2018-08-14 14:12:33 回复(0)
ZRA头像 ZRA
//找到数字和单词的对应关系,单词和字母的对应关系
import java.util.HashMap;
import java.util.Scanner;
/**
 * 
 * @author JSZha0
 */
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        String[] sb = new String[n];
        in.nextLine();
         while(in.hasNext())
             System.out.println(minPhoneNumber(in.nextLine()));
        }
    }
    public static String minPhoneNumber(String str) {
        //hashmap存储字符串中字母个数
        HashMap<Character, Integer> map = new HashMap<>();
         for(char i=65; i<91; i++) {
             map.put(i, 0);
         }
         //统计字母个数
        for(char s : str.toCharArray()) {
            map.put(s, map.get(s)+1);
        }
        //数组对应关系,index表示数字,存储的值表示数字的个数
        int[] ch = new int[10];
                                    //         
        ch[2] = map.get('Z');       //  ZERO     0     2
        ch[4] = map.get('W');        //  TWO      2     4
        ch[6] = map.get('U');       //  FOUR     4     6
        ch[8] = map.get('X');        //  SIX      6     8
        
        ch[0] = map.get('G');        //  EIGHT     8     0
        
        ch[5] = map.get('H')-ch[0]; // THREE-GIGHT      3     5
        ch[7] = map.get('F')-ch[6]; //...
        ch[9] = map.get('V')-ch[7];
        
        ch[3] = map.get('O')-ch[2]-ch[4]-ch[6];
        ch[1] = map.get('I')-ch[0]-ch[8]-ch[7];
        
        String s = "";
        for(int i=0; i<10;i++) {
            while(ch[i]>0) {
                s += i;
                ch[i]--;
            }
        }
        return s;
    }
    
}

发表于 2017-10-04 20:42:19 回复(0)

问题信息

难度:
136条回答 20412浏览

热门推荐

通过挑战的用户

查看代码
电话号码分身