- 题目描述
继MIUI8推出手机分身功能之后,MIUI9计划推出一个电话号码分身的功能:
("ZERO", "ONE", "TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN", "EIGHT", "NINE"),
然后随机打乱这些字母,所生成的字符串即为电话号码对应的分身。
- 输入描述
接下来T行,每行给定一个分身后的电话号码的分身(长度在3到10000之间)。
- 输出描述
输出T行,分别对应输入中每行字符串对应的分身前的最小电话号码(允许前导0)。
- 输入
4
EIGHT
ZEROTWOONE
OHWETENRTEO
OHEWTIEGTHENRTEO
- 输出
0
234
345
0345
python代码
n = int(input())
for i in range(n):
string = []
string = input()
count = [0] * 10
count[2] = string.count('Z')
count[4] = string.count('W')
count[6] = string.count('U')
count[8] = string.count('X')
count[0] = string.count('G')
count[3] = string.count('O') - count[2] - count[4] - count[6]
count[5] = string.count('H') - count[0]
count[7] = string.count('F') - count[6]
count[9] = string.count('V') - count[7]
count[1] = string.count('I') - count[7] - count[0] - count[8]
for j in range(10):
for m in range(count[j]):
print(j, end="") #输出不换行
print()#如果使用print("\n")的话会输出两个换行符,因为print自带一个换行符
C++代码
#include <iostream>
#include <string.h>
using namespace std;
int main() {
int t;
char n[10004];
int b[10] = {0,0,0,0,0,0,0,0,0,0};
int i,s;
scanf("%d",&t);
while(t--) {
memset(b,0,sizeof(b));
scanf("%s",n);
s = strlen(n);
for(i = 0; i< s; i++) {
if(n[i] == 'Z') b[2] ++;
if(n[i] == 'W') b[4] ++;
if(n[i] == 'U') b[6] ++;
if(n[i] == 'X') b[8] ++;
if(n[i] == 'G') b[0] ++;
if(n[i] == 'O') b[3] ++;
if(n[i] == 'H') b[5] ++;
if(n[i] == 'F') b[7] ++;
if(n[i] == 'V') b[9] ++;
if(n[i] == 'I') b[1] ++;
}
b[3] = b[3] - b[2] - b[4] - b[6];
b[5] = b[5] - b[0];
b[7] = b[7] - b[6];
b[9] = b[9] - b[7];
b[1] = b[1] - b[7] - b[0] - b[8];
for(int j = 0;j < 10;j ++){
for(int m = 0;m < b[j];m ++)
cout << j;
}
cout << "\n";
}
return 0;
}
算法思想
关键是要找关键词之前的区别