ZOJ 1274 Getting Chorded(模拟)

于 2011年01月23日 发布在 算法&&ACM 跳到评论

zoj 1274 Getting Chorded

“钢琴的音符有12种,A,A# (A-sharp), B, C, C#, D, D#, E, F, F#, G, and G#,同时按下三个音符,有三种情况,一种是Major chord,一种是Minor chord,最后一种是什么都不是unrecognized,假设按下的这三个音符按顺序排列分别是a,b,c,如果ab中间隔3个,bc之间隔2个,那么整个结果就是a Major chord,如果ab中间隔2个,bc之间隔3个,那么整个结果是a Minor chord(假定12种音符在琴键上是循环排列),给定三个音符,然你判断结果是上述三种中的哪一种”

由于他给的音符的顺序不确定,大小写也不确定,一个音符的两个名称给哪个也不确定,而且输出是要求原样,类型要输出大写,-___-……所以我们一步一步来。。。保留输入的形式方便输出;把音符的两个名称转化为带#的形式,然后转换成大写;比较恶心的是他的顺序也不确定,但是给定的三个它们的最终结果是一样的,那么给12种音符赋值为20,21,22,23,2……210,211,  三种的值相加是唯一的,所以。。。。。

代码很丑,体现了我做模拟题的一贯风格:

#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cctype>
using namespace std;

int change(char *c)
{
    char a[5];
    int i;
    strcpy(a,c);
    if( a[1] == 'b' )
    {
        switch(a[0])
        {
            case 'B':strcpy(a,"A#");break;
            case 'D':strcpy(a,"C#");break;
            case 'E':strcpy(a,"D#");break;
            case 'G':strcpy(a,"F#");break;
            case 'A':strcpy(a,"G#");break;
            default :break;
        }
    }
    for( i = 0; a[i]; i++ )
        a[i] = toupper(a[i]);
    if( strcmp(a,"A") == 0 ) return 1;
    if( strcmp(a,"A#") == 0 ) return 2;
    if( strcmp(a,"B") == 0 ) return 4;
    if( strcmp(a,"C") == 0 ) return 8;
    if( strcmp(a,"C#") == 0 ) return 16;
    if( strcmp(a,"D") == 0 ) return 32;
    if( strcmp(a,"D#") == 0 ) return 64;
    if( strcmp(a,"E") == 0 ) return 128;
    if( strcmp(a,"F") == 0 ) return 256;
    if( strcmp(a,"F#") == 0 ) return 512;
    if( strcmp(a,"G") == 0 ) return 1024;
    if( strcmp(a,"G#") == 0 ) return 2048;
    return 0;
}

int main(void)
{
    char a[5],b[5],c[5];
    int sum;
    while( cin >> a >> b >> c )
    {
        sum = change(a) + change(b) + change(c);
        cout << a << ' ' << b << ' ' << c << ' ';

        switch( sum )
        {
            case 145:cout << "is a C Major chord.\n";break;
            case 145*2:cout << "is a A# Major chord.\n";break;
            case 145*4:cout << "is a B Major chord.\n";break;
            case 145*8:cout << "is a C Major chord.\n";break;
            case 145*16:cout << "is a C# Major chord.\n";break;

            case 545:cout << "is a D Major chord.\n";break;
            case 545*2:cout << "is a D# Major chord.\n";break;
            case 545*4:cout << "is a E Major chord.\n";break;

            case 265:cout << "is a F Major chord.\n";break;
            case 265*2:cout << "is a F# Major chord.\n";break;
            case 265*4:cout << "is a G Major chord.\n";break;
            case 265*8:cout << "is a G# Major chord.\n";break;
            // MAJOR CHORD IS OVER@~~
            case 137:cout << "is a A Minor chord.\n";break;
            case 137*2:cout << "is a A# Minor chord.\n";break;
            case 137*4:cout << "is a B Minor chord.\n";break;
            case 137*8:cout << "is a C Minor chord.\n";break;
            case 137*16:cout << "is a C# Minor chord.\n";break;

            case 289:cout << "is a D Minor chord.\n";break;
            case 289*2:cout << "is a D# Minor chord.\n";break;
            case 289*4:cout << "is a E Minor chord.\n";break;
            case 289*8:cout << "is a F Minor chord.\n";break;

            case 529:cout << "is a F# Minor chord.\n";break;
            case 529*2:cout << "is a G Minor chord.\n";break;
            case 529*4:cout << "is a G# Minor chord.\n";break;
            //MINOR CHORD IS OVER@~~
            default : cout << "is unrecognized.\n";break;
        }

    }

    return 0;
}

本文共有 4条评论 | 沙发:文章评论

  1. 小媛说道:

    = =。。。哎~~~受不鸟你的代码了。。。。

  2. WilliamKyle说道:

    你没开Akismet?
    防垃圾留言啊

  3. laoda说道:

    我去啊,党神V5……

留下评论!

:wink: :twisted: :roll: :oops: :mrgreen: :lol: :idea: :evil: :cry: :arrow: :?: :-| :-x :-o :-P :-D :-? :) :( :!: 8-O 8)