我之所以写这个博客,因为关于字符串存储的问题
题目的描述是这样的
这道题的思路还是比较简单的,把这些情况全部定义成一个数组,来保存字符串,下面是我的代码:
#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
using namespace std;
string lookfor[]={"zero","one","two","three","four","five","six","seven",
"eight","nine","ten","eleven","twelve","thirteen","fourteen","fifteen",
"sixteen","seventeen","eighteen","nineteen","twenty","thirty","forty","fifty"};
int h,m;
string r="o'clock";//在这里我定义了一个未知数量的字符串数组特别重要!!!!!!
void lookfortime(int n)//不管是小时还是分钟都要通过这个函数来输出
{
int a,b;
a=n/10;
b=n%10;
if(n<=20)
{
cout<<lookfor[n];
}
else
{
cout<<lookfor[a+18];
if(b!=0)
{
cout<<' '<<lookfor[b];
}
}
}
void output()
{
if(m==0)
{
lookfortime(h);
cout<<' ' << r;
}
else
{
lookfortime(h);
cout<<' ';
lookfortime(m);
}
}
int main()
{
cin>>h>>m;
output();
return 0;
}
那个字符串存储原理: