标签:algorithm
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.
-123456789
Fu yi Yi er Qian san Bai si Shi wu Wan liu Qian qi Bai ba Shi jiu
100800
yi Shi Wan ling ba Bai
// 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; }
标签:algorithm
原文地址:http://blog.csdn.net/linsheng9731/article/details/38477377