The Queen's English |
Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:65536KB |
Total submit users: 24, Accepted users: 19 |
Problem 12879 : No special judgement |
Problem description |
In the official British written word format for numbers, tens and units are separated by a hyphen, and hundreds are separated from tens or units by the word "and". For example the number 123 would be written "one hundred and twenty-three". Large numbers are written in "triads" (groups of three digits) followed by the appropriate suffixes. For example 123456 is "one hundred and twenty-three thousand four hundred and fifty-six". As a special case, if the final triad of a large number has a tens or units component but no hundreds component, it needs an "and". Thus 1001001 is "one million one thousand and one". |
Input |
Input will consist of specifications for a series of tests. Information for each test is a single line containing an integer 1 <= n < 1000000000 that specifies the value to process. A line containing the value 0 terminates the input. |
Output |
Output should consist of one line for each test comprising the test number (formatted as shown) followed by a single space and the correct British word form of the input value, with a single space between words. |
Sample Input |
123
1001001
900090009
0 |
Sample Output |
Test 1: one hundred and twenty-three
Test 2: one million one thousand and one
Test 3: nine hundred million ninety thousand and nine |
题目要求就是给出一个小于1e9的整数 用英文表示该整数
是一个模拟题 因为英文数字表达是以三位为一组的 所以预先处理出a,b,c的值 分别代表该分组内的值。。
输出要求比较多 写了好久。。码力不足啊。。。
最后输出没有空格
代码如下:
<span style="font-family:KaiTi_GB2312;font-size:18px;">#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string.h>
#include <string>
#define eps 1e-8
#define op operator
#define MOD 10009
#define MAXN 10000
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define FOV(i,a,b) for(int i=a;i>=b;i--)
#define REP(i,a,b) for(int i=a;i<b;i++)
#define REV(i,a,b) for(int i=a-1;i>=b;i--)
#define MEM(a,x) memset(a,x,sizeof a)
#define ll __int64
using namespace std;
char eng[10][6]={"zero","one","two","three","four","five","six","seven","eight","nine"};
char s[10][10] = {"ten","twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"};
char t[10][15] = {"eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"};
int main()
{
//freopen("ceshi.txt","r",stdin);
//freopen("out.ou","w",stdout);
int n;
int cnt=1;
while(scanf("%d",&n)!=EOF)
{
if(n==0) break;
printf("Test %d: ",cnt++);
bool f1=0,f2=0;
int a=n/1000000;
if(a>0) f1=1;
int b=n/1000%1000;
if(b>0) f2=1;
int c=n%1000;
bool flag1=0,flag2=0,flag3=0,flag4=0;
if(a>=100)
{
flag1=1;
int a1=a/100;
printf("%s hundred",eng[a1]);
a%=100;
if(a!=0) printf(" ");
}
if(a>=10)
{
flag2=1;
int a2=a/10;
if(a2>1)
{
if(flag1)
printf("and %s",s[a2-1]);
else
{
printf("%s",s[a2-1]);
}
}
else
{
flag4=1;
int b2=a%10;
if(b2==0)
{
if(flag1)
printf("and %s",s[0]);
else printf("%s",s[0]);
}
// printf("%s",s[0]);
else
{
if(flag1)
printf("and %s",t[b2-1]);
else printf("%s",t[b2-1]);
}
}
a%=10;
}
if(a>0)
{
flag3=1;
if(!flag4)
{
if(flag2)
{
printf("-%s",eng[a]);
}
else
{
if(flag1)
printf("and %s",eng[a]);
else
printf("%s",eng[a]);
}
}
}
if(flag1||flag2||flag3)
printf(" million");
if((b>0||c>0)&&(flag1||flag2||flag3)) printf(" ");
flag1=flag2=flag3=flag4=0;
if(b>=100)
{
flag1=1;
int a1=b/100;
printf("%s hundred",eng[a1]);
b%=100;
if(b!=0) printf(" ");
}
if(b>=10)
{
flag2=1;
int a2=b/10;
if(a2>1)
{
if(flag1)
printf("and %s",s[a2-1]);
else
{
printf("%s",s[a2-1]);
}
}
else
{
flag4=1;
int b2=b%10;
if(b2==0)
{
if(flag1)
printf("and %s",s[0]);
else printf("%s",s[0]);
}
// printf("%s",s[0]);
else
{
if(flag1)
printf("and %s",t[b2-1]);
else printf("%s",t[b2-1]);
}
}
b%=10;
}
if(b>0)
{
flag3=1;
if(!flag4)
{
if(flag2)
{
printf("-%s",eng[b]);
}
else
{
if(flag1)
printf("and %s",eng[b]);
else
printf("%s",eng[b]);
}
}
}
if(flag1||flag2||flag3)
printf(" thousand");
if(c>0&&(flag1||flag2||flag3)) printf(" ");
flag1=flag2=flag3=flag4=0;
if(c>=100)
{
flag1=1;
int a1=c/100;
printf("%s hundred",eng[a1]);
c%=100;
if(c!=0) printf(" ");
}
if(c>=10)
{
flag2=1;
int a2=c/10;
if(a2>1)
{
if(flag1)
printf("and %s",s[a2-1]);
else
{
if(f1||f2)
printf("and %s",s[a2-1]);
else printf("%s",s[a2-1]);
}
// if(c%10==0) printf(" ");
}
else
{
flag4=1;
int b2=c%10;
if(b2==0)
{
if(flag1) printf("and %s",s[0]);
else
{
if(f1||f2) printf("and %s",s[0]);
else printf("%s",s[0]);
}
// printf("%s",s[a2-1]);
}
else
{
if(flag1)
printf("and %s",t[b2-1]);
else
{
if(f1||f2)
printf("and %s",t[b2-1]);
else printf("%s",t[b2-1]);
}
}
}
c%=10;
}
if(c>0)
{
flag3=1;
if(!flag4)
{
if(flag2)
{
printf("-%s",eng[c]);
}
else
{
if(flag1) printf("and %s",eng[c]);
else
{
if(f1||f2)
printf("and %s",eng[c]);
else printf("%s",eng[c]);
}
}
}
}
printf("\n");
}
return 0;
}
</span>