1082 Read Number in Chinese

44 篇文章 0 订阅

description

Given an integer with no more than 9 digits, you are supposed to read it in the traditional Chinese way. Output Fu first if it is negative. For example, -123456789 is read as Fu yi Yi er Qian san Bai si Shi wu Wan liu Qian qi Bai ba Shi jiu. Note: zero (ling) must be handled correctly according to the Chinese tradition. For example, 100800 is yi Shi Wan ling ba Bai.

Input Specification:

Each input file contains one test case, which gives an integer with no more than 9 digits.

Output Specification:

For each test case, print in a line the Chinese way of reading the number. The characters are separated by a space and there must be no extra space at the end of the line.

Sample Input 1:

-123456789

Sample Output 1:

Fu yi Yi er Qian san Bai si Shi wu Wan liu Qian qi Bai ba Shi jiu

Sample Input 2:

100800

Sample Output 2:

yi Shi Wan ling ba Bai

idea

  • x yyyy zzzz
    • x亿,若x不为0,直接输出
    • yyyy只要其中有非零数,需要输出Wan,之间同样各十百千,个位无单位
    • zzzz之间各十百千,个位无单位
  • 零的处理,只输出两边是非零数的零,头尾的不输出
  • 测试点:
    • 测试点2是后半部分的零不输出,例如800800,后两位0不输出

solution

#include <stdio.h>
#include <string.h>
int main(){
	char str[20], num[10][10] = {"ling", "yi", "er", "san", "si", "wu", "liu", "qi", "ba", "jiu"}, power[8][10] = {"Shi", "Bai", "Qian", "Wan", "Shi", "Bai", "Qian", "Yi"};
	int first = 0, zero = 0, yi = 0, wan = 0, wanS = 0, ge = 0, ling = 0, n = 0, end = 0, t;
	gets(str);
	if(str[0] == '-'){
		printf("Fu ");
		n++;	
	} 
	for(int i = (n > 0) ? 1 : 0; i < strlen(str); i++){
		t = strlen(str) - i;
		if(str[i] != '0'){
			if(zero) {
				if(yi || wanS || ge) {
					printf(" "); 
					yi = wanS = ge = 0;	
				}
				printf("ling");
				ling = 1;
				zero = 0; 
			} 
			if(!first) first = 1;
			if(t == 9) {
				printf("%s Yi", num[str[i] - '0']);
				yi = 1;	//后面有非零数时,再输出空格 
			}
			else if(t < 9 && t > 4){
				if(yi){
					printf(" ");
					yi = 0;
				}
				wan = 1;
				if(t == 5) printf("%s ", num[str[i] - '0']);
				else printf("%s %s ", num[str[i] - '0'], power[t - 6]);
			}
			else{
				if(yi || wanS || ling || ge){
					printf(" ");
					yi = wanS = ge = ling = 0;
				}
				if(t != 1)printf("%s %s", num[str[i] - '0'], power[t - 2]);
				else printf("%s", num[str[i] - '0']);
				ge = 1;
			}
		}
		else if(first && !zero){//不是首位数且第一次出现 
				zero = 1;//后续有非零数时输出0 
		}
		if(t == 5 && wan) {
			printf("Wan");
			wan = 0;
			wanS = 1;//后面有非零数时,再输出空格 
		}
	}
	if(!first) printf("ling");
	return 0;
} 

反思

理清逻辑后,还是有过不去的测试点的话,从是不是有低级错误的角度验证(例如本次zero更新了个寂寞……)
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值