题解 P1617 【爱与愁的一千个伤心的理由】
头一次见到这么烦人的题目 就不得不写一篇题解泄愤
这题虽然表面上相当简单,但是有非常非常多要注意的点
1、and什么时候加
这题的语法是比较谜的 千和百之间没有and 百和十之间 千和十(中间没有百)之间要有and等等等等 代码中将会一一列出
2、关于zero
本人的代码由于太过蒟蒻,居然不会自己输出zero,成功地坑掉我一次提交
3、11~19这一段的表示方式不同,别忘了特判
具体见代码
#include<cstdio>
#include<string>
#include<iostream>
using namespace std;
string c[100];
int n;
int t,h,a,b;
int main()
{
c[0]="zero";
c[1]="one";
c[2]="two";
c[3]="three";
c[4]="four";
c[5]="five";
c[6]="six";
c[7]="seven";
c[8]="eight";
c[9]="nine";
c[10]="ten";
c[11]="eleven";
c[12]="twelve";
c[13]="thirteen";
c[14]="fourteen";
c[15]="fifteen";
c[16]="sixteen";
c[17]="seventeen";
c[18]="eighteen";
c[19]="nineteen";
c[20]="twenty";
c[30]="thirty";
c[40]="forty";
c[50]="fifty";
c[60]="sixty";
c[70]="seventy";
c[80]="eighty";
c[90]="ninety";//大串的预处理 打完之后感觉身体被掏空
scanf("%d",&n);
b=n%10;
n/=10;
a=n%10;
n/=10;
h=n%10;
n/=10;
t=n%10;
n/=10;//存储千位 百位 十位 个位
if(t>0)//玄学判断开始
{//如果有千
cout << c[t] << " thousand ";//输出千
if(h==0 && (a>0 || b>0)) cout << "and ";//判断要不要加and 当没有百位而有十位或个位时要加and
//于是我们得出了:千和百之间没有and 当百为0时 千和十、个之间有and
}
if(h>0)
{//如果有百
cout << c[h] << " hundred ";//输出百
if(a==0 && b>0) cout << "and ";//判断要不要输出and 得出:百与十或个之间要加and
}
if(a==1) cout << c[a+b];//这里就是上述的第3点 11~19之间的特判
else
{
if(a>0) cout << c[a*10] << " ";
if(b>0) cout << c[b];//无脑输出剩下的十位、百位即可
}
if(t==0 && h==0 && a==0 && b==0) cout << c[0];//特判0
return 0;//结束
}