您的位置:首页 > 其它

A Famous Music Composer

2014-11-25 16:00 309 查看

A Famous Music Composer

时间限制:1000 ms | 内存限制:65535 KB难度:1描述Mr. B is a famous music composer. One of his most famous work was his set of preludes. These 24 pieces span the 24 musical keys (there are musically distinct 12 scale notes, and each may use major or minor tonality). The 12 distinct scale notes are:
A A#=Bb B C C#=DbD D#=Eb E F F#=Gb G G#=Ab
Five of the notes have two alternate names, as is indicated above with equals sign. Thus, there are 17 possible names of scale notes, but only 12 musically distinct notes. When using one of these as the keynote for a musical key, we can further distinguishbetween major and minor tonalities. This gives 34 possible keys, of which 24 are musically distinct.In naming his preludes, Mr. B used all the keys except the following 10, which were named instead by their alternate names:
Ab minor A# majorA# minor C# major Db minor
D# major D# minorGb major Gb minor G# major
Write a program that, given the name of a key, give an alternate name if it has one, or report the key name is unique.输入Each test case is described by one line having the format "note tonality", where "note" is one of the 17 names for the scale notes given above, and "tonality" is either "major" or "minor" (quotes for clarify).输出For each case output the required answer, following the format of the sample.样例输入
Ab minor
D# major
G minor
样例输出
Case 1: G# minor
Case 2: Eb major
Case 3: UNIQUE
解题思路:本题使用的方法就是字符串中元素的替换,然而其缺点就是代码冗长,耗时较长。程序代码:
#include<stdio.h>
int main()
{
char a[10];
int i=1;
while(gets(a))
{
//printf("%s\n",a);
if(a[0]=='A'&&a[1]=='#'&&a[2]==' ')
{
a[0]='B';
a[1]='b';
printf("Case %d: %s\n",i,a);
}
else if(a[0]=='C'&&a[1]=='#'&&a[2]==' ')
{
a[0]='D';
a[1]='b';
printf("Case %d: %s\n",i,a);
}
else if(a[0]=='D'&&a[1]=='#'&&a[2]==' ')
{
a[0]='E';
a[1]='b';
printf("Case %d: %s\n",i,a);
}
else if(a[0]=='F'&&a[1]=='#'&&a[2]==' ')
{
a[0]='G';
a[1]='b';
printf("Case %d: %s\n",i,a);
}
else if(a[0]=='G'&&a[1]=='#'&&a[2]==' ')
{
a[0]='A';
a[1]='b';
printf("Case %d: %s\n",i,a);
}
else if(a[0]=='B'&&a[1]=='b'&&a[2]==' ')
{
a[0]='A';
a[1]='#';
printf("Case %d: %s\n",i,a);
}
else if(a[0]=='D'&&a[1]=='b'&&a[2]==' ')
{
a[0]='C';
a[1]='#';
printf("Case %d: %s\n",i,a);
}
else if(a[0]=='E'&&a[1]=='b'&&a[2]==' ')
{
a[0]='D';
a[1]='#';
printf("Case %d: %s\n",i,a);
}else if(a[0]=='G'&&a[1]=='b'&&a[2]==' ')
{
a[0]='F';
a[1]='#';
printf("Case %d: %s\n",i,a);
}
else if(a[0]=='A'&&a[1]=='b'&&a[2]==' ')
{
a[0]='G';
a[1]='#';
printf("Case %d: %s\n",i,a);
}
else
printf("Case %d: UNIQUE\n",i);
i++;
}
return 0;
}
最优代码:
#include<iostream>
#include<string>
using namespace std;
string trans(string a){
string b="";
if(a[1]=='#'){
b+=char((a[0]-'A'+1)%7+'A');
b+='b';
}else{
b+=char((a[0]-'A'+6)%7+'A');
b+='#';
}
return b;
}
int main(){
string a,b;
for(int t=1; cin>>a>>b; t++){
cout<<"Case "<<t<<": ";
if(a.length()==1)
cout<<"UNIQUE"<<endl;
else
cout<<trans(a)<<" "<<b<<endl;
}
return 0;
}
改进方法:通过字符之间的关系进行操作(当a[1]为‘#’时,a[1]都用‘b'替代,而此时a[0]变成比其ASC码大一的字符;当a[1]为’b'时,a[i]都用‘#’替代,而此时a[0]变成比其ASC码小一的字符)这样省时省力。

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