Skip to content

[ACM]a008: 中文大寫數字

內容 :

我們在金融機構填寫金額時使用的不是阿拉伯數字,而是中文的大寫數字。

請寫一個程式將數字轉換為中文大寫數字

標準大寫寫法如下:零、壹、貳、參、肆、伍、陸、柒、捌、玖、拾、佰、仟、萬、億

輸入說明 :整數數字 >=0 且 <=2147483647

輸出說明 :文字字串

範例輸入 :

12345
10200

範例輸出 :

壹萬貳仟參佰肆拾伍
壹萬零貳佰

提示 :* 遇到 10 時輸出 『拾』or『壹拾』均可

出處 :http://zerojudge.tw/ShowProblem?problemid=a008

程式碼:

#include <iostream>
#include <string.h>
using namespace std;
int main()
{
	char a[10];
	int cont=0;
	int b=0;
	int c=0;
	int d=0;
	string str;
	char* tabe[] = {"零","壹","貳","參","肆","伍","陸","柒","捌","玖"};
	char* tab[] = {"零","拾","佰","仟","萬","拾","佰","仟","億","拾"};
	while(cin>>a)
	{
		cont=strlen(a)-1;
			 for (int f=0;f<=cont;f++)
				 {
					c=c+a[f]-'0';
					d=d++;
				 }
		for(int i=0;i<=cont;i++)
		{
				 b=a[i]-'0';
				 if (b!=0)
				 {
					if (cont-i!=0)
					 {
						 str=str+tabe[b]+tab[cont-i];
					 }
					else
					{
						str=str+tabe[b];
					 break;
					}
				 }
				 else if(b==0)
				 {

						if (cont-i!=0 && c!=a[0]-'0' && a[i+1]-'0'!=0)
						{
							str=str+tab[0];
						 }
						else if(d>=6 && d<=8 && a[i+1]-'0'!=0 && cont-i!=0)
						{
							str=str+tab[4];
							d=0;
						}
						else if(d>=6 && i<=3)
						{
							str=str+tab[4];
							d=0;
						}
						else if(d==10)
						{
							str=str+tab[8];
							d=0;
						}

				 }

		}
		 cout<<str<<endl;
		str="";
		c=0;
		d=0;
	}
	return 0;
}
Published inACM解題

Be First to Comment

發表迴響

你的電子郵件位址並不會被公開。 必要欄位標記為 *