题解 P1617 【爱与愁的一千个伤心的理由】
怎么c++题解都这么长???那我来发一个短点的题解吧
上代码,绝对很短,打数表就占了一半长度
#include<cstdio>
#include<string>
#include<iostream>
using namespace std;
int main(){
int a,qian=0,bai=0,shi=0;
string ch[21],c[10]; //存数表,ch是<=20的数,c是几十的数
scanf("%d",&a);
if (a==0) {printf("zero\n"); return 0;} //特判为0的情况
ch[1]="one"; ch[2]="two"; ch[3]="three"; ch[4]="four";
ch[5]="five"; ch[6]="six"; ch[7]="seven"; ch[8]="eight";
ch[9]="nine"; ch[10]="ten"; ch[11]="eleven"; ch[12]="twelve";
ch[13]="thirteen"; ch[14]="fourteen"; ch[15]="fifteen"; ch[16]="sixteen";
ch[17]="seventeen"; ch[18]="eighteen"; ch[19]="nineteen"; ch[20]="twenty";
c[3]="thirty"; c[4]="forty"; c[5]="fifty"; c[6]="sixty";
c[7]="seventy"; c[8]="eighty"; c[9]="ninety"; //这么多行都是数表
if (a>=1000){//如果有千位处理千位
cout<<ch[a/1000]<<" thousand "; a=a%1000; //输出并去掉千位
qian=1; //qian表示原数千位是否为0
}
if (a>=100){//有百位就处理
cout<<ch[a/100]<<" hundred "; a=a%100; //输出并去掉百位
bai=1; //表示原数百位是否为0
}
if (a>=10){//处理十位
if (qian==1&&bai==0) printf("and "); //如果千位有百位没有就输一个and
if (a<=20) {cout<<ch[a]; return 0;} //剩余部分小于等于20直接输出,不用管个位了
cout<<c[a/10]<<" "; //输出十位,与前面稍有不同
a=a%10; //去掉十位
shi=1; //表示原数十位是不是0
}
if (a!=0){//最后判个位
if (shi==0&&(qian==1||bai==1)) printf("and "); //如果十位没有且千位,百位至少有一个就输and
cout<<ch[a]; //输出个位,结束了啊啊啊
}
return 0;
}