这是一道音乐上的题目,说有三个琴键同时按下组成和弦,如果三个键从左到右分别相差4、3个音阶的话算作major和弦,相差4、3个则为minor,啥都不是就会很strange
给三个琴键问是哪种和弦。
枚举当然可以啦,反正也就3!=6种组合,只是感觉这么来的话没有用到算法多不高兴呀~
那么我们就先在钢琴上给他们排个序呗,排好了序之后称作A\B\C(在一个12音阶中严格从左到右),那么我们只需要判断 ABC\BCA\CAB 三种情形下是否满足相差3、4或者4、3即可。
#include <cstdio>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
// http://codeforces.com/contest/88
// Chord
string notes[12]={"C","C#","D","D#","E","F","F#","G","G#","A","B","H"};
int dist(string a, string b, string c)
{
int pos[3]={0};
for(int i=0;i<12;i++)
{
if(notes[i]==a) pos[0]=i;
if(notes[i]==b) pos[1]=i;
if(notes[i]==c) pos[2]=i;
}
sort(pos,pos+3);
//cout<<pos[0]<<pos[1]<<pos[2]<<endl;
int dis[3]={ pos[1]-pos[0],
pos[2]-pos[1],
pos[0]+12-pos[2] };
for(int i=0;i<3;i++)
{
if(dis[i]==4 && dis[(i+1)%3]==3)return 1;
if(dis[i]==3 && dis[(i+1)%3]==4)return 2;
}
return 0;
}
int main()
{
string X,Y,Z;
cin>>X>>Y>>Z;
int flag=dist(X,Y,Z);
if(flag==1)cout<<"major";
else if(flag==2)cout<<"minor";
else cout<<"strange";
return 0;
}