先來個簡單的:MOOC上面看到的
輸入一個整數,輸出每個數字對應的拼音。當整數爲負數時,先輸出“fu”字。十個數字對應的拼音如下:
0: ling
1: yi
2: er
3: san
4: si
5: wu
6: liu
7: qi
8: ba
9: jiu
輸入格式:
輸入在一行中給出一個整數,如: 1234 。
提示:整數包括負數、零和正數。
輸出格式:
在一行中輸出這個整數對應的拼音,每個數字的拼音之間用空格分開,行末沒有最後的空格。如 yi er san si。
輸入樣例: -600 輸出樣例: fu liu ling ling
我至少有N種方法實現...
數組索引法(後面例子用到了一些),分支選擇法,遞歸拼接,還有能用枚舉...還有什麼呢?不告訴你
下面看這個實例的代碼:
#include<stdio.h> int main() { void int_to_string(int n) { switch (n) { case -1: printf("%s", "fu"); break; case 0: printf("%s", "ling"); break; case 1: printf("%s", "yi"); break; case 2: printf("%s", "er"); break; case 3: printf("%s", "san"); break; case 4: printf("%s", "si"); break; case 5: printf("%s", "wu"); break; case 6: printf("%s", "liu"); break; case 7: printf("%s", "qi"); break; case 8: printf("%s", "ba"); break; case 9: printf("%s", "jiu"); break; default: break; } } void string_output(int num) { if (num / 10) { string_output(num / 10); if ((num / 10) != 0) { printf(" "); } int_to_string(num % 10); } else { int_to_string(num); } } void int_to_reverse(int num) { if (num < 0) { num = -num; int_to_string(-1); printf(" "); string_output(num); } else if (num == 0) { int_to_string(0); } else { string_output(num); } } int num; scanf("%d", &num); int_to_reverse(num); return 0; }
╮(╯▽╰)╭這個是很笨的方法了
哦,忘記了,給觀衆們留個地址你懂的,你懂的好東西
下面看一個複雜的,上面的都會了下面的那個肯定小意思咯
//金額轉換,阿拉伯數字的金額轉換成中國傳統的形式
//如:105600123 => 壹億零仟伍佰陸拾零萬零仟壹佰貳拾叄圓整
電腦不行╮(╯▽╰)╭,eclipse gcc + java 雙開快掛了
/** * Project Interview * Package com.java.interview.algorithm.other * FileName Money_Convert.java * Description TODO * Company ITSer LTD. * Copyright 2014 * All rights Reserved, Designed By ITSer * * @author Dev-Wangxin * @version V1.0 * Createdate 2014年8月28日 下午7:04:04 * * Modification History * Date Author Version Description * ----------------------------------------------------------------------------------- * 2014年8月28日 Wangxin 1.0 1.0 * Why & What is modified */ package com.java.interview.algorithm.other; /** * ClassName Money_Convert<BR> * Description 貨幣轉換<BR> * * @author Dev-Wangxin * @date 2014年8月28日 下午7:04:04 * */ public class Money_Convert { public static void main(String[] args) { String money = convert_To_Chinese(105600123); System.out.println(money); int i = convert_To_Number(money); System.out.println(i); } private static final char[] data = new char[] { '零', '壹', '貳', '叄', '肆', '伍', '陸', '柒', '捌', '玖' }; private static final char[] units = new char[] { '圓', '拾', '佰', '仟', '萬', '拾', '佰', '仟', '億' }; /** * * <p> * MethodName: convert_To_Number<BR> * Description: 將大寫中文型貨幣轉換爲阿拉伯數字型<BR> * Create_By: Wangxin<BR> * Create_Date: 2014年8月28日 下午7:06:50<BR> * Modification with annotation or not<BR> * </p> * * @param money * @return int */ public static int convert_To_Number(String money) { char[] m = money.toCharArray(); int length = m.length; StringBuffer sb = new StringBuffer(); for (int i = length - 3; i >= 0; i -= 2) { switch (m[i]) { case '零': sb.insert(0, 0); break; case '壹': sb.insert(0, 1); break; case '貳': sb.insert(0, 2); break; case '叄': sb.insert(0, 3); break; case '肆': sb.insert(0, 4); break; case '伍': sb.insert(0, 5); break; case '陸': sb.insert(0, 6); break; case '柒': sb.insert(0, 7); break; case '捌': sb.insert(0, 8); break; case '玖': sb.insert(0, 9); break; } } return Integer.parseInt(sb.toString()); } /** * * <p> * MethodName: convert_To_Chinese<BR> * Description: 將阿拉伯數字型貨幣轉換爲大寫中文<BR> * Create_By: Wangxin<BR> * Create_Date: 2014年8月28日 下午7:06:43<BR> * Modification with annotation or not<BR> * </p> * * @param money * @return String */ public static String convert_To_Chinese(int money) { StringBuffer sb = new StringBuffer("整"); int unit = 0; while (money != 0) { sb.insert(0, units[unit++]); int number = money % 10; sb.insert(0, data[number]); money /= 10; } return sb.toString(); } }