【PAT 甲级】1082 Read Number in Chinese (25 分)

1082 Read Number in Chinese (25 分)

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

我的思路

调试了半天,处理各种细节问题,详情看代码中的注释吧。注意:

1. PAT 已经不支持 gets(),会报错 error: 'gets' was not declared in this scope,需用cin.getline(s,长度)代替;

2. 注意100001234这种数的输出是一亿一千二百三十四

 

我的代码 

#include 
#include 
#include 
using namespace std; 
int main()
{
	//初始化 
	int f=0, length, t;
	char s[11]={'*','*','*','*','*','*','*','*','*','*'};  //避免初始化0,影响判断 
	//用于位数的汉字表示,把空格和个位表示也存入数组,便于输出格式 
	char w[][11] = {" Yi"," Qian"," Bai"," Shi"," Wan"," Qian"," Bai"," Shi",""};
	//用于数字的汉字表示,存成数组就不用switch选了 
	char n[][11] = {"ling","yi","er","san","si","wu","liu","qi","ba","jiu"};
	
	//PAT已经不支持gets(),会报错error: 'gets' was not declared in this scope 
	//gets(s);

	cin.getline(s,11);
	length = strlen(s);
	//判断正负 
	if (s[0]=='-')
	{
		printf("Fu ");
		f=1;  //数据处理时,数据从下标1开始 
	}
	t = length-f;  //求除了负号以外的位数 
	
	for (int i=f; i

 

你可能感兴趣的:(PAT甲级,C++,PAT)