【Java】7-23 币值转换 (20 分)

7-23 币值转换 (20 分)

输入一个整数(位数不超过9位)代表一个人民币值(单位为元),请转换成财务要求的大写中文格式。如23108元,转换后变成“贰万叁仟壹百零捌”元。为了简化输出,用小写英文字母a-j顺序代表大写数字0-9,用S、B、Q、W、Y分别代表拾、百、仟、万、亿。于是23108元应被转换输出为“cWdQbBai”元。

输入格式:

输入在一行中给出一个不超过9位的非负整数。

输出格式:

在一行中输出转换后的结果。注意“零”的用法必须符合中文习惯。

输入样例1:

813227345

输出样例1:

iYbQdBcScWhQdBeSf

输入样例2:

6900

输出样例2:

gQjB

Java 代码:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
	public static int[] split(int num) {
		int[] array = new int[3];
		for (int i = 0; i < array.length; i++) {
			array[i] = num % 10000;
			num /= 10000;
		}
		return array;
	}

	public static void main(String[] args) throws NumberFormatException, IOException {
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		int num = Integer.parseInt(in.readLine());
		StringBuilder sb = new StringBuilder();
		int[] array = split(num);
		String[] unit = { "", "S", "B", "Q" };
		// 用于输出每千位中间0,并且只输出一次
		boolean f = true;
		for (int i = 0; i < 3; i++) {
			int x = array[i];
			int idx = 0;
			// 万位单位和亿位单位
			if (i == 1 && x > 0) {
				sb.insert(0, "W");
			} else if (i == 2 && x > 0) {
				sb.insert(0, "Y");
			}
			int cnt = 0;
			// 末尾多0的情况,只需要判断最后一位是不是0
			int end = x % 10;
			// 每千位小于10的数就不考虑0的输出
			boolean isLess = x < 10;
			while (cnt < 4) {
				char c = (char) (x % 10 + 97);
				if (f && c == 'a' && i < 2 && end != 0 && !isLess) {
					sb.insert(0, 'a');
					f = false;
				}
				sb.insert(0, c > 'a' ? c + unit[idx] : "");
				x /= 10;
				idx++;
				cnt++;
			}
		}
		System.out.println(sb.length() > 0 ? sb : 'a');

	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值