12期算法擂台解答

来源:互联网 发布:量子经济学 知乎 编辑:程序博客网 时间:2024/05/16 05:25
 
时间表达
英文口语表达时间有如下6种句型:
l         It is five past seven.(7点5分)
l         It is eleven to ten. (9点49分)
l         It is half past nine. (9点30分)
l         It is a quarter past eight. (8点15分)
l         It is a quarter to ten. (9点45分)
l         It is three o’clock. (3点)
请根据输入的时间,输出相应的英文表达。
程序从键盘输入多行数据,每一行为两个非负整数A和B,其中0<=A<13,0<=B<60,分别表示小时数和分钟数。如果输入的某一行为0 0,则表示输入已结束。程序输出要求:对于输入的每一行(最后的0 0行除外),输出相应的时间表达字符串,要求为上面6种句型之一。输入输出样例见下:
 
样例输入
8 21
12 45
5 0
0 30
0 0
样例输出
It is twenty-one past eight.
It is a quarter to thirteen.
It is five o’clock.
It is half past zero.
#include <iostream>
        #include <string>
        using namespace std;
int main()
{
 int hour, minute;  // 小时数和分钟数
 const string STR[] = {"zero", "one",  "two",  "three", "four",  "five",  "six", "seven",
        "eight", "nine",  "ten",  "eleven", "twelve", "thirteen","fourteen",
        "a quarter", "sixteen", "seventeen", "eighteen", "nineteen", "twenty",
        "twenty-one", "twenty-two", "twenty-three", "twenty-four", "twenty-five",
        "twenty-six", "twenty-seven", "twenty-eight", "twenty-nine", "half"};
 while (cin >> hour >> minute)
 {
  if (hour == 0 && minute == 0) // 0 0行,退出
   break;
  if (minute == 0) // 整点
   cout << "It is " << STR[hour] << " o'clock." << endl;
  else if (minute <= 30) // 不超过30分
   cout << "It is " << STR[minute] << " past " << STR[hour] << "." << endl;
  else // 超过30分
   cout << "It is " << STR[60 - minute] << " to " << STR[hour + 1] << "." << endl;
 }
 return 0;
}