= =重庆市的ACM比赛,规模估计比不上别的市,问题是还是要培训,今天写了道蛋疼的题
Description:
计算机经常用于工资和帐号支付应用等支票写入系统。许多怪事常常出现。如每月工资支票上错误的多写一百万美元。由于人和机器的错误,使支票写入系统写出不正常数值。系统设计人员在系统中建立控制,防止发生出这种错误支票。另一个严重的问题是有些人故意改变支票金额,想窃取钱财。要防止改变支票金额,大多数支票写入系统采用支票保护(Check Protection)技术。一个常用的安全方法是写出支票金额的大写,即使支票的数字好改,大写金额也难篡改。编写一个程序,输入数字金额,输出大写金额。如112.43写成ONE HUNDRED TWELVE Dollars and 43/100。
Input:
输入数据有若干,数据范围在0.00到999.99。
Output:
对于每个数据输出其大写的英语表示。如12.45写成TWELVE Dollars and 45/100。如果是整数部分是零或一美元,则Dollar不加s。如果没有角分,则角分不用输出。
Sample Input:
0.45
34
56.89
Sample Output:
ZERO Dollar and 45/100
THIRTY FOUR Dollars
FIFTY SIX Dollars and 89/100
代码如下
#include < fstream > #include < string > #include < algorithm > using namespace std; int main( void ){ ifstream fin( " test17.txt " ); ofstream fout( " estdout.pc2 " ); bool point; string num[] = { " ZERO " , " ONE " , " TWO " , " THERE " , " FOUR " , " FIVE " , " SIX " , " SEVEN " , " EIGHT " , " NINE " , " TEN " , " ELEVEN " , " TWELVE " , " THIRTEEN " , " FOURTEEN " , " FIFTEEN " , " SIXTEEN " , " SEVENTEEN " , " EIGHTEEN " , " NINETEEN " }; string ten[] = { " TWENTY " , " THIRTY " , " FORTY " , " FIFTY " , " SIXTY " , " SEVENTY " , " EIGHTY " , " NINETY " }; while ( ! fin.eof()) { string number; string integer; string decimal ; fin >> number; if (number == "" ) break ; if (find(number.begin(),number.end(), ' . ' != * number.end())) { point = false ; for ( int i = 0 ;i < number.size(); ++ i) { if ( number[i] == ' . ' ) { point = true ; continue ; } if ( ! point) integer += number[i]; else decimal += number[i]; } } else { point = false ; for ( int i = 0 ; i < number.size(); ++ i) { integer += number[i]; } } if (integer.size() == 3 ) { fout << num[integer[ 0 ] - ' 0 ' ] << ' ' ; if (integer[ 0 ] == ' 1 ' ) fout << " HUNDRED " ; else fout << " HUNDREDS " ; } if (integer.size() >= 2 ) { if (integer[integer.size() - 2 ] > ' 1 ' ) { fout << ten[ ( integer[integer.size() - 2 ] - ' 2 ' )] << ' ' ; } else if (integer[integer.size() - 2 ] == ' 1 ' ) { fout << num[ 10 * (integer[integer.size() - 2 ] - ' 0 ' ) + (integer[integer.size() - 1 ] - ' 0 ' )] << ' ' ; } } if (integer.size() == 1 ) fout << num[integer[integer.size() - 1 ] - ' 0 ' ] << ' ' ; if (integer.size() > 1 && integer[integer.size() - 2 ] != ' 1 ' && integer[integer.size() - 1 ] != ' 0 ' ) fout << num[integer[integer.size() - 1 ] - ' 0 ' ] << ' ' ; if (integer.size() == 1 && integer[integer.size() - 1 ] <= ' 1 ' ) fout << " Dollar " ; else fout << " Dollars " ; if (point) { fout << " and " ; fout << decimal << " /100 " ; } fout << endl; } return 0 ;} 坏习惯改不了了…………老是不习惯写成函数…………每次都是直接堆到主函数里= =不过我有接口,函数调用需要时间,比赛是有时限的。不过其实可以弄成内联函数的= =