一道简单题目,可以方便求出元音有多少个,辅音有多少个,然后筛选每个字符多少个能够使得分数最低,然后分别构造元音序列和辅音序列,把字典序排前面的字符优先放到序列前面就保证了字典序最小了。
#include <stdio.h>
#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <string.h>
using namespace std;
vector<char> vow; //元音
vector<char> con; //辅音
set<char> vow_set;
set<char> con_set;
map<char,int> m; //记录哪个字符用多少次
void init()
{
vow.clear();
con.clear();
vow.push_back('A');
vow.push_back('U');
vow.push_back('E');
vow.push_back('O');
vow.push_back('I');
con.push_back('J');
con.push_back('S');
con.push_back('B');
con.push_back('K');
con.push_back('T');
con.push_back('C');
con.push_back('L');
con.push_back('D');
con.push_back('M');
con.push_back('V');
con.push_back('N');
con.push_back('W');
con.push_back('F');
con.push_back('X');
con.push_back('G');
con.push_back('P');
con.push_back('Y');
con.push_back('H');
con.push_back('Q');
con.push_back('Z');
con.push_back('R');
}
void func(int n)
{
int vow_num, con_num;
int i;
char vow_string[300];
char con_string[300];
if(n%2 == 0)
{
vow_num = n/2;
con_num = n/2;
}
else
{
vow_num = n/2 +1;
con_num = n/2;
}
m.clear();
vow_set.clear();
con_set.clear();
for(i=0; i<vow.size(); i++)
{
if(!vow_num)
break;
if(vow_num >=21)
{
m[vow[i]] = 21;
vow_num -= 21;
vow_set.insert(vow[i]);
}
else
{
m[vow[i]] = vow_num;
vow_num = 0;
vow_set.insert(vow[i]);
}
}
for(i=0; i<con.size(); i++)
{
if(!con_num)
break;
if(con_num >=5)
{
m[con[i]] = 5;
con_num -= 5;
con_set.insert(con[i]);
}
else
{
m[con[i]] = con_num;
con_num = 0;
con_set.insert(con[i]);
}
}
//分别构造元音和辅音的序列
int char_num;
int j;
char ch;
set<char>::iterator it;
i = 0;
for(it=vow_set.begin(); it!=vow_set.end(); it++)
{
ch = *it;
char_num = m.find(ch)->second;
for(j=1; j<=char_num; j++)
vow_string[i++] = ch;
}
vow_string[i] = '\0';
i = 0;
for(it=con_set.begin(); it!=con_set.end(); it++)
{
ch = *it;
char_num = m.find(ch)->second;
for(j=1; j<=char_num; j++)
con_string[i++] = ch;
}
con_string[i] = '\0';
for(i=0; i<strlen(con_string); i++)
{
printf("%c%c", vow_string[i], con_string[i]);
}
if(n%2)
printf("%c\n", vow_string[i]);
else
printf("\n");
}
int main()
{
int N, n, i;
init();
scanf("%d", &N);
for(i=1; i<=N; i++)
{
scanf("%d", &n);
printf("Case %d: ", i);
func(n);
}
return 0;
}