HOJ 12879 The Queen's English

本文介绍了一个程序设计问题,即如何将大整数转换为符合英式英语规范的字符串格式,特别关注了数字间的连接词及特定情况下的使用规则。

The Queen's English
Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:65536KB
Total submit users: 40, Accepted users: 33
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".
Your task is to write a program that produces correct British written format for numbers of up to 9 digits (less than one "short" billion). Note that the correct spellings are "fifteen" and "fifty" ('f' instead of 'v'), and "forty" (no 'u').


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
Problem Source
AUPC 2013

题目意思:给你1e9范围内的一个整数,要你用英语的方式输出,million,thousand,hundred不加s。

想法:模拟嘛,对于一个数分成3快,a,b,c分别表示million,thousand,hundred的大小,对于a,b,c就差不多一样的处理了,因为他们最多就是3位。但是注意100000001的情况。也就是什么时候加and。三位数 hundred and ..... 像上例需要注意的也要加and。

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>
#include<stack>
#define rt return
#define sf scanf
#define pf printf
#define REP0(i,n) for(int i=0;i<(n);i++)
#define REP1(i,n) for(int i=1;i<=(n);i++)
#define REP(i,s,n) for(int i=s;i<=(n);i++)
#define db double
#define pb push_back
#define LL long long
#define INF 0x3fffffff
#define eps 1e-8
#define PI acos(-1)
#define maxn
using namespace std;
int n;
int a,b,c;
bool ok;
char one[20][20]={"0","one","two","three","four","five","six","seven","eight","nine","ten",
        "eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"};
char two[10][20]={"0","1","twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"};
void fun(int num){
    int d[4],t=0;
    memset(d,0,sizeof(d));
    bool tmp=false;
    while(num){
        d[++t]=num%10;
        num/=10;
    }
    if(d[3]){
        tmp=true;
        pf("%s hundred",one[d[3]]);
    }
    int val=d[2]*10+d[1];
    if(val==0)rt;
    if(tmp) pf(" ");
    if(tmp||(ok&&(a!=0||b!=0)))
        pf("and ");
    if(val<20)pf("%s",one[val]);
    else {
        pf("%s",two[d[2]]);
        if(d[1]!=0){
            pf("-%s",one[d[1]]);
        }
    }
}
int main(){
    #ifdef ACBang
    //freopen("in.txt","r",stdin);
    #endif
    int cas=1;
    while(~sf("%d",&n),n){
        pf("Test %d: ",cas++);
        a=n/1000000;
        b=n%1000000/1000;
        c=n%1000;
        ok=false;
        if(a){fun(a);pf(" million");if(n%1000000!=0)pf(" "); }
        if(b){fun(b);pf(" thousand");if(c!=0)pf(" "); }
        if(c){ok=true;fun(c); }
        pf("\n");
    }
    rt 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值