#include <iostream>
#include <algorithm>
#include <map>
using namespace std;
const bool MAJOR [] = {true, false, true, false, true, true, false, true, false, true, false, true};
const string LIST[] = {"C","B#","C#","Db","D","D#","Eb","E","Fb","F","E#","F#","Gb","G","G#","Ab","A","A#","Bb","B","Cb"};
map <string, pair<int, int> > PITCH; //pair:(音高,是否作大调(1=C调,2=#,3=b)).
void init ()
{
PITCH ["C"] = make_pair (0, 1);
PITCH ["C#"] = make_pair (1, 2);
PITCH ["Db"] = make_pair (1, 3);
PITCH ["D"] = make_pair (2, 2);
PITCH ["D#"] = make_pair (3, 0);
PITCH ["Eb"] = make_pair (3, 3);
PITCH ["E"] = make_pair (4, 2);
PITCH ["E#"] = make_pair (5, 0);
PITCH ["Fb"] = make_pair (4, 0);
PITCH ["F"] = make_pair (5, 3);
PITCH ["F#"] = make_pair (6, 2);
PITCH ["Gb"] = make_pair (6, 3);
PITCH ["G"] = make_pair (7, 2);
PITCH ["G#"] = make_pair (8, 0);
PITCH ["Ab"] = make_pair (8, 3);
PITCH ["A"] = make_pair (9, 2);
PITCH ["A#"] = make_pair (10, 0);
PITCH ["Bb"] = make_pair (10, 3);
PITCH ["B"] = make_pair (11, 2);
PITCH ["B#"] = make_pair (0, 0);
PITCH ["Cb"] = make_pair (11, 3);
}
string findNote (string to,int val)
{
string u = "", v = "", uf = "", vf = "", ans;
for (auto cur : LIST)
{
if (PITCH[cur].first == val)
{
u = cur;
u.swap (v);
}
}
if (u == "")
{
ans = v;
}
else
{
uf += u[0], vf += v[0];
if ((PITCH[uf].first - PITCH[vf].first + 12) % 12 == 1)
u.swap (v); //升号的放前面.
switch (PITCH[to].second)
{
case 1: //C Major.
if (u.length() < v.length()) ans = u;//选没有变化音的.
else ans = v;
break;
case 2: //# Major.
ans = u;
break;
case 3: //b Major.
ans = v;
break;
}
}
return ans;
}
int main ()
{
ios::sync_with_stdio (false);
cin.tie (nullptr);
cout.tie (nullptr);
string from, to, note, res;
int state, delta, newval;
bool firstline = true;
init();
//freopen("UVA595.out","w",stdout);
while (cin >> from && from != "*")
{
if (!firstline) cout << endl;
firstline = false;
state = 0;
cin >> to;
if (PITCH[from].second == 0)
{
state = 1;
cout << "Key of " << from << " is not a valid major key" << endl;
}
else if (PITCH[to].second == 0)
{
state = 2;
cout << "Key of " << to << " is not a valid major key" << endl;
}
else
{
cout << "Transposing from " << from << " to " << to << ":" << endl;
}
while (cin >> note && note != "*")
{
if (state) continue;
delta = PITCH[note].first - PITCH[from].first;
delta = (delta + 12) % 12;
if (!MAJOR[delta] || findNote(from, PITCH[note].first) != note)
{
cout << " " << note << " is not a valid note in the "
<< from << " major scale" << endl;
continue;
}
newval = PITCH[to].first + delta;
newval = (newval + 12) % 12;
res = findNote (to, newval);
cout << " " << note << " transposes to " << res << endl;
}
}
return 0;
}
结尾空行已经去掉了,末尾一行的换行符是uDebug上标准out中有的。刷了一整页WA。。。
求调,感谢。