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#=Db | D | D#=Eb | E | F | F#=Gb | G | G#=Ab |
Ab minor | A# major | A# minor | C# major | Db minor |
D# major | D# minor | Gb major | Gb minor | G# major |
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码小一的字符)这样省时省力。
相关文章推荐
- 杭电-A Famous Music Composer
- A Famous Music Composer
- A Famous Music Composer
- A Famous Music Composer
- hdu 4245 A Famous Music Composer--map
- ACM-A Famous Music Composer
- [4245]:A Famous Music Composer
- 题目25-A Famous Music Composer
- NYOJ 题目25 A Famous Music Composer
- A Famous Music Composer
- HDU4245:A Famous Music Composer
- nyoj 25 A Famous Music Composer
- NYOJ 25 A Famous Music Composer
- A Famous Music Composer
- A Famous Music Composer
- HDU4245 A Famous Music Composer
- A Famous Music Composer(map)
- A Famous Music Composer
- hdoj-4245-A Famous Music Composer
- HDU4245 A Famous Music Composer