10785 - The Mad Numerologist

来源:互联网 发布:云智能网络 编辑:程序博客网 时间:2024/06/06 02:24

题目:10785 - The Mad Numerologist


题目大意:就是给定字符串长度,要求要找到一个总值最小,并且奇数位,偶数位按字典序的字符串。


解题思路:要求值最小,就按照题目给的各个字母的值,给出两个从左到右值一次增大的常量字符串,(元音辅音分开两个字符串)。最终结果的字符串要求用到的辅音不能重复超过5次,元音不能重复超过21次。

可是值小的字母,字典序不一定也排前,所以要奇数位,偶数位分别按字典序sort(),因此要开两个数组分别给奇数位,偶数位,最后排好序再交替输出。小心输出结果的case 1: AJ. 冒号后面有一个空格。


#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;const char v[] = "AUEOI";const char c[] = "JSBKTCLDMVNWFXGPYHQZR"; const int N = 200;char o[N], e[N];int n, len;int cmp(char a, char b) {return a < b;}int main() {int i, j;scanf("%d", &n);for(i = 0; i < n; i++) {scanf("%d", &len);int odd = (len + 1) / 2;int even = len / 2;for(j = 0; j < odd; j++) o[j] = v[j/21];for(j = 0; j < even; j++)e[j] = c[j/5];sort(o, o + odd, cmp);sort(e, e + even, cmp);printf("Case %d: ", i + 1);int s1,s2;s1 = s2 = 0;for(j = 0; j < len; j++) {if(j % 2 == 0) {printf("%c", o[s1]);s1++;}else {printf("%c", e[s2]);s2++;}}printf("\n");}return 0;}






0 0
原创粉丝点击