算法题:把阿拉伯金额转化为汉字表示的金额2013-11-14 51cto博客 tonglingn年没写算法题了,今天用了20分钟写了一个,要求如题,感觉算法有所退步,老了
using System;using System.Text;namespace money{class Program{static void Main(string[] args){StringBuilder sb=new StringBuilder();var strValue = Console.ReadLine();var strlist = strValue.Split(".");if (strlist.Length >= 2){var temp = strlist[1];if (temp.Length >= 2){if (temp[0] != "0")sb.Append(GetChinese(Convert.ToInt32(temp[0].ToString()))).Append("角").Append(GetChinese(Convert.ToInt32(temp[1].ToString()))).Append("分");elsesb.Append(GetChinese(0));}elsesb.Append(GetChinese(Convert.ToInt32(temp[0].ToString()))).Append("角");}int stateNum = 0;int tempNum = Convert.ToInt32(strlist[0]);string outline = string.Empty;while (tempNum>0){int g = tempNum%10;if (g == 0){outline += (stateNum!=0)?GetChinese(g):string.Empty;while (g == 0){stateNum++;tempNum = tempNum / 10;g = tempNum % 10;}}else{outline += (stateNum <= 4) ? GetDW(stateNum) : GetDW((stateNum + 1) / 4);outline += GetChinese(g);tempNum = tempNum / 10;stateNum++;}if (stateNum == 5)stateNum++;}string right = string.Empty;for (int i = outline.Length-1; i >= 0; i--){right += outline[i].ToString();}Console.WriteLine(right+sb.ToString());Console.ReadKey();}private static string GetDW(int i){switch (i){case 0:return "元";case 1:return "十";case 2:return "百";case 3:return "千";case 4:return "万";}return null;}private static string GetChinese(int i){switch (i){case 1:return "一";case 2:return "二";case 3:return "三";case 4:return "四";case 5:return "五";case 6:return "六";case 7:return "七";case 8:return "八";case 9:return "九";case 0:return "零";}return null;}}}