PAT 甲级 1082 Read Number in Chinese (25分) string字符串的应用

题目

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 Input1:

-123456789

Sample Output1:

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

Sample Input2:

100800

Sample Output2:

yi Shi Wan ling ba Bai

Note

“边读边输出”。
变量解释:

  • len:数字的位数(若是负数则等于字符串的长度-1)
    len≤0时,break直接退出while
  • is0:判断0的存在。这里is0有三个状态
    1、is0=0:判断0在头部,即0在非零数字之前。当is0等于0时,第一个非零数字之前的0不用读。(如:0001,应该读作yi)
    2、is0=1:判断0在中部。这时需要判断0是否包括了万这个位。
    例如:100001,读作yi Shi Wan ling yi;
    110001,读作yi Shi yi Wan ling yi。
    若0跨越了万位,需要输出Wan;反之不需要输出。
    3、is0=2:判断末尾是否有0。若0在末尾,不需要额外输出0。(例如:100,读作yi Bai)

特殊的测试点?

100000
000000
100001
110001

Code:

#include<bits/stdc++.h>
using namespace std;

int main(){
	string n;
	string number[10]={"ling","yi","er","san","si","wu","liu","qi","ba","jiu"};
	string weight[10]={"0","1","Shi","Bai","Qian","Wan","Shi","Bai","Qian","Yi"};
	cin>>n;
	int i=0,len=n.length(),is0=0,flag=0;
	if(n[i]=='-'){
		len--;
		i++;
		printf("Fu");
		flag=1;
	}
	while(len>0){
		while(is0==0&&n[i]=='0'){
			//第一个非零数前面的0不需要读
			len--;
			i++;
		}
		if(is0==0&&n[i]!='0') is0=1;
		if(len<=0){
			//筛选0和0000这种情况
			printf("ling");
			break;
		}
		
		while(is0==1&&n[i]!='0'&&len>0){
			if(flag==1) cout<<" ";
			else if(flag==0) flag=1;
			//先输出数字 再输出单位
			cout<<number[n[i]-'0'];
			if(len>1) cout<<" "<<weight[len];
			i++;
			len--;
		}
		if(len<=0) break;  //len<=0 直接退出
		
		int before0=n.length();
		if(is0==1&&n[i]=='0'){
			is0=2;
			before0=len;  //记录中间部分第一个0的位置
		}
		
		while(is0==2&&n[i]=='0'){
			len--;
			i++;
		}
		
		if(before0>=5&&len<5) cout<<" Wan";  //0跨越了万位,需要输出Wan
		if(is0==2) is0=1;
		if(before0!=len&&len!=0) cout<<" ling";  //如果中间部分有0,则需要读出来
	}
	
	return 0;
} 

AC~

AC

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值