先来个简单的: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();
}
}
转载于:https://blog.51cto.com/wangxin88/1546266