首页 > 代码库 > pat解题报告【1082】

pat解题报告【1082】

1082. Read Number in Chinese (25)

时间限制  
400 ms
内存限制  
32000 kB
代码长度限制  
16000 B
判题程序    
Standard    
作者    
CHEN, Yue

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
 
简单的模拟,只是过程描述有点啰嗦。
首先这句话什么意思  Note: zero ("ling") must be handled correctly according to the Chinese tradition
什么叫Chinese tradition,其实就是对0的特殊处理。
【1】尾部的0不发音
【2】多个连续的0,只发一个音
【3】多个不连续的0,都要发音
【4】大于4位的数肯定要发 Wan 音
【5】大于8位的数一定要发 Yi 音
而且还有个规律: Shi Bai Qian 这三个音循环出现。
 
Ac代码:
 
// pat-1082.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"

#include "iostream"
#include "string"
#include "algorithm"
#include "vector"
#include "stack"

using namespace std;
stack<string> ans;
string num[]={"ling", "yi" ,"er" , "san" , "si" ,  "wu",  "liu" , "qi",  "ba",  "jiu"};
string pos[]={ "Shi","Bai","Qian", "Wan","Yi" };
int main()
{
	long int n=0;
	bool firstout=true;
	cin>>n;
	if (n==0)
	{
		cout<<"ling";
		goto end;
	}
	if (n<0)
	{   
		cout<<"Fu";
		firstout=false;
		n=-(n);
	}
	int cnt=0;
	bool wan_flag=false;
	bool zero=false;
	bool first=true;
	while(n)
	{
	  int temp=n%10;
	  //0 特殊处理
	  if (temp==0)
	  { 
		  if (cnt==3)
		  {
			  wan_flag=true;
		  }
		  n/=10;
		  if (!first)
		  {  
			  cnt++;
		  }
	      first=false;
		  if (zero)//第一次遇到0
		  {
			  ans.push(num[0]);
			  zero=false;
			  continue;
		  }
		  else
		  {
			  continue;
		  }
	  }
	  zero=true;
	  if(first)//忽略个位
	  {
		    ans.push(num[temp]);//yi er san si ~~~~~
			 n/=10;
			  first=false;
			 continue;
	  }
	 if(cnt<7)
	 {  
		 if (wan_flag)
		 {
			 wan_flag=false;
			 ans.push(pos[3]);
		 }
	     ans.push(pos[cnt%4]);//shi bai qian wan 
	  }
	 else
     {
	  ans.push(pos[4]);//yi
	 }
	 
	  ans.push(num[temp]);//yi er san si ~~~~~
	 
	  n/=10;
	  cnt++;
	 
	}
	while (!ans.empty())
	{
		string temp=ans.top();
		ans.pop();
		if (firstout)
		{
			firstout=false;
			cout<<temp;
			continue;
		}
		cout<<" "<<temp;
	}
end:	return 0;
}