UVa_10785 - The Mad Numerologist
2014-06-24 19:35
465 查看
题意:
对26个大写字母,每个字母都赋一个权值,比如A,J,S权值为1;然后求一个字符串,满足权值和最小(辅音字母和元音字母),字母序列最小(即按照字典序排序)。
思路:
1. 先选择出满足题目要求的元音字母和辅音字母,使权值最小,元音字母序列存储在str[0][220]数组中,辅音字母存储在str[1][220]数组中;
2. 对两个数组排序,得到lexicographically order;
3.两个数组交叉输出,即可得到the numerologists first priority is to keep the vowel and consonant value minimum and then to make the name lexicographically smallest(辅音字母和元音字母权值和最小,字母序列最小)的结果。
代码如下:
对26个大写字母,每个字母都赋一个权值,比如A,J,S权值为1;然后求一个字符串,满足权值和最小(辅音字母和元音字母),字母序列最小(即按照字典序排序)。
思路:
1. 先选择出满足题目要求的元音字母和辅音字母,使权值最小,元音字母序列存储在str[0][220]数组中,辅音字母存储在str[1][220]数组中;
2. 对两个数组排序,得到lexicographically order;
3.两个数组交叉输出,即可得到the numerologists first priority is to keep the vowel and consonant value minimum and then to make the name lexicographically smallest(辅音字母和元音字母权值和最小,字母序列最小)的结果。
代码如下:
#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; char vowel[]={'A','U','E','O','I'}; char consonant[]={'J','S','B','K','T','C','L','D','M','V','N','W','F','X','G','P','Y','H','Q','Z','R'}; int cnt[26]; char str[2][220]; void Init() { for(int i=0;i<26;i++){ if(i==0||i==4||i==8||i==14||i==20) cnt[i]=21; else cnt[i]=5; } } char Select(int n) { if(n%2) { for(int i=0;i<5;i++){ if(cnt[vowel[i]-'A']>0) {cnt[vowel[i]-'A']--;return vowel[i];} } } else { for(int i=0;i<21;i++){ if(cnt[consonant[i]-'A']>0) {cnt[consonant[i]-'A']--;return consonant[i];} } } } int main() { int N,n,f1,f2; scanf("%d",&N); for(int i=1;i<=N;i++) { Init(); f1=f2=0; memset(str[0],'\0',sizeof(str[0])); memset(str[1],'\0',sizeof(str[1])); scanf("%d",&n); for(int j=0;j<n;j++){ if((j+1)%2) str[0][f1++]=Select(j+1); else str[1][f2++]=Select(j+1); } sort(str[0],str[0]+f1); sort(str[1],str[1]+f2); printf("Case %d: ",i); for(int j=0,f1=0,f2=0;j<n;j++){ if((j+1)%2) putchar(str[0][f1++]); else putchar(str[1][f2++]); } printf("\n"); } return 0; }
相关文章推荐
- UVA 10785 - The Mad Numerologist
- UVA 10785 The Mad Numerologist
- UVa 10785 - The Mad Numerologist
- uva 10785 - The Mad Numerologist
- UVa 10785 - The Mad Numerologist
- UVA - 10785 The Mad Numerologist 水题
- UVA 10785 - The Mad Numerologist
- UVa 10785 The Mad Numerologist (排序)
- uva 10785 - The Mad Numerologist
- UVA 10785 - The Mad Numerologist
- UVa 10785 - The Mad Numerologist
- UVA - 10785 The Mad Numerologist
- UVA 10785 The Mad Numerologist
- The Mad Numerologist UVA-10785 第五章
- UVA - 10785 The Mad Numerologist
- Uva 10785 - The Mad Numerologist
- uva 10785 The Mad Numerologist(遍历+sort排序)
- UVA-10785 The Mad Numerologist
- uva 10785 The Mad Numerologist
- Uva 10785 - The Mad Numerologist