您的位置:首页 > 编程语言 > C语言/C++

The Mad Numerologist UVA-10785 第五章

2013-10-03 12:00 369 查看


  The Mad Numerologist 
Numerology is a science that is used by many people to find out a mans personality, sole purpose of life, desires to experience etc. Some calculations of numerology are very complex, while others are quite simple.
You can sit alone at home and do these easy calculations without taking any ones help. However in this problem you wont be asked to find the value of your name.



To find the value of a name modern numerologists have assigned values to all the letters of English Alphabet. The table on the left shows the numerical values of all letters of English alphabets. Five letters
A, E, I, O, U are vowels. Rests of the letters are consonant. In this table all letters in column 1 have value 1, all letters in column 2 have value 2 and so on. So T has value 2, F has value 6, R has value 9, O has value 6 etc. When calculating the value
of a particular name the consonants and vowels are calculated separately. The following picture explains this method using the name ``CHRISTOPHER RORY PAGE".



So you can see that to find the consonant value, the values of individual consonants are added and to find the vowel value the values of individual vowels are added. A mad Numerologist suggests people many strange
lucky names. He follows the rules stated below while giving lucky names.
The name has a predefined length N.
The vowel value and consonant value of the name must be kept minimum.
To make the pronunciation of the name possible vowels and consonants are placed in alternate positions. Actually vowels are put in odd positions and consonants are put in even positions. The leftmost letter of a name has position 1; the position right to
it is position 2 and so on.
No consonants can be used in a name more than five times and no vowels can be used in a name more than twenty-one times
Following the rules and limitations above the name must be kept lexicographically smallest. Please note that the numerologists first priority is to keep the vowel and consonant value minimum and then to make the name lexicographically smallest.

Input 

First line of the input file contains an integer N ( 0
N

250) that indicates how many sets of inputs are there.
Each of the next N lines contains a single set of input. The description of each set is given
below: Each line contains an integer n ( 0
n < 211) that indicates the predefined length of the name.

Output 

For each set of input produce one line of output. This line contains the serial of output followed by the name that the numerologist would suggest following the rules above. All letters in the output should be
uppercase English letters.

Sample Input 

3
1
5
5

Sample Output 

Case 1: A
Case 2: AJAJA
Case 3: AJAJA


Miguel Revilla 2004-12-02

一道简单的加权排序题目,在尽量使加权值最小的情况下,还要使整个字符串的字典序最小,我的思路是先把使字典序最小的元音字母和辅音字母求出来,再给它排序,麻烦了不少啊。

代码如下:

#include<iostream>
#include<algorithm>

using namespace std;

struct T
{
char ch1;
int value1,t1;
}vowel[5];

struct P
{
char ch2;
int value2,t2;
}consonant[21];

bool cmp1(T x,T y)
{
if(x.value1!=y.value1) return x.value1<y.value1;
else return x.ch1<y.ch1;
}

bool cmp2(P x,P y)
{
if(x.value2!=y.value2) return x.value2<y.value2;
else return x.ch2<y.ch2;
}

bool cmp3(char x,char y)
{
return x<y;
}

int main()
{
int n,i;
cin>>n;
char s1[]={'A','U','E','O','I'};
char s2[]={'J','S','B','K','T','C','L','D','M','V','N','W','F','X','G','P','Y','H','Q','Z','R'};
for(i=1;i<=n;i++)
{
string str1,str2;
int m,j;
for(j=0;j<5;j++)
{
vowel[j].ch1=s1[j];
vowel[j].value1=(s1[j]-'A')%9+1;
vowel[j].t1=0;
}
for(j=0;j<21;j++)
{
consonant[j].ch2=s2[j];
consonant[j].value2=(s2[j]-'A')%9+1;
consonant[j].t2=0;
}
sort(vowel,vowel+5,cmp1);
sort(consonant,consonant+21,cmp2);
cin>>m;
cout<<"Case "<<i<<": ";
int k=0,d=0;
for(j=1;j<=m;j++)
{
if(j%2!=0)
{
if(vowel[k].t1<21)
{
str1=str1+vowel[k].ch1;
vowel[k].t1++;
}
if(21==vowel[k].t1)
{
k++;
}
}
else
{
if(consonant[d].t2<5)
{
str2=str2+consonant[d].ch2;
consonant[d].t2++;
}
if(5==consonant[d].t2)
{
d++;
}
}
}
sort(str1.begin(),str1.end(),cmp3);
sort(str2.begin(),str2.end(),cmp3);
int t=str2.size();
if(str1.size()==str2.size())
{
for(int a=0;a<str1.size();a++)
{
cout<<str1[a]<<str2[a];
}
}
else
{
cout<<str1[0];
for(int a=0;a<str2.size();a++)
{
cout<<str2[a]<<str1[a+1];
}
}
cout<<endl;
}
return 0;
}



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  C++ ACM