解题思路:
条件明确,数字和字母表示一一对应。所以想到运用开关语句。
注意事项:
1.函数封装。为了减少操作,利用递归思想对20到60间的非特殊数字采用递归。
参考代码:
#include<iostream>
#include<string>
using namespace std;
string s(int x) {
string y;
switch (x) {
case 0: y = "zero";
break;
case 1:y = "one";
break;
case 2:y = "two";
break;
case 3:y = "three";
break;
case 4:y = "four";
break;
case 5:y = "five";
break;
case 6:y = "six";
break;
case 7:y = "seven";
break;
case 8:y = "eight";
break;
case 9:y = "nine";
break;
case 10:y = "ten";
break;
case 11:y = "eleven";
break;
case 12:y = "twelve";
break;
case 13:y = "thirteen";
break;
case 14:y = "fourteen";
break;
case 15:y = "fifteen";
break;
case 16:y = "sixteen";
break;
case 17:y = "seventeen";
break;
case 18:y = "eighteen";
break;
case 19:y = "nineteen";
break;
case 20:y = "twenty";
break;
case 30:y = "thirty";
break;
case 40:y = "forty";
break;
case 50:y = "fifty";
break;
default:
y = s(x - x % 10) +" "+ s(x % 10); //递归一次,代码复用
break;
}
return y;
}
int main(){
int h, m;
cin >> h >> m;
string x, y;
if (m != 0) {
x = s(h);
y = s(m);
cout << x << " " << y << endl;
}
else {//m==0
x = s(h);
cout << x << " o'clock" << endl;
}
return 0;
}
0.0分
0 人评分