1002. 写出这个数 (20)
来源:互联网 发布:网络营销策划书的作用 编辑:程序博客网 时间:2024/05/22 12:09
读入一个自然数n,计算其各位数字之和,用汉语拼音写出和的每一位数字。
输入格式:每个测试输入包含1个测试用例,即给出自然数n的值。这里保证n小于10100。
输出格式:在一行内输出n的各位数字之和的每一位,拼音数字间有1 空格,但一行中最后一个拼音数字后没有空格。
输入样例:1234567890987654321123456789输出样例:
yi san wu
思路:
这道题首先是把数字分割开来,但是用split需要定义许多字母,所以用String转char再转int通过数组来做。详细看代码。不懂的请留言。
其中要注意的是许多位数相加和为0的情况。
代码:
import java.util.Scanner;public class Main {public static void main(String[] args) {// TODO Auto-generated method stubScanner sc = new Scanner(System.in);String s = sc.nextLine();char[] a = new char[s.length()];int[] b = new int[s.length()];int sum = 0, count = 0;for (int i = 0; i < s.length(); i++) {a[i] = s.charAt(i);//分离数字b[i] = (int) a[i] - 48;//将char型转换成int型}for (int i = 0; i < s.length(); i++) {sum += b[i];//数字相加}if (sum != 0) {//判断结果是否为0int sum1 = sum;while (sum1 / 10 != 0 || sum1 % 10 > 0) {//判断数字的位数count++;sum1 = sum1 / 10;}int[] c = new int[count];for (int i = 0; i < count; i++) {//将结果存入数组c[i] = sum % 10;sum /= 10;}for (int i = count - 1; i >= 0; i--) {//判断输出if (c[i] == 0) {System.out.print("ling");} else if (c[i] == 1) {System.out.print("yi");} else if (c[i] == 2) {System.out.print("er");} else if (c[i] == 3) {System.out.print("san");} else if (c[i] == 4) {System.out.print("si");} else if (c[i] == 5) {System.out.print("wu");} else if (c[i] == 6) {System.out.print("liu");} else if (c[i] == 7) {System.out.print("qi");} else if (c[i] == 8) {System.out.print("ba");} else if (c[i] == 9) {System.out.print("jiu");}if (i != 0) {System.out.print(" ");}}}else {System.out.println("ling");}}}
阅读全文
0 0
- 1002. 写出这个数 (20)
- 1002. 写出这个数 (20)
- 1002. 写出这个数 (20)
- 1002. 写出这个数 (20)
- 1002. 写出这个数 (20)
- 1002. 写出这个数 (20)
- 1002. 写出这个数 (20)
- 1002. 写出这个数 (20)
- 1002. 写出这个数 (20)
- 1002. 写出这个数 (20)
- 1002. 写出这个数 (20)
- 1002. 写出这个数 (20)
- 1002. 写出这个数 (20)
- 1002. 写出这个数 (20)
- 1002. 写出这个数 (20)
- 1002. 写出这个数 (20)
- 1002. 写出这个数 (20)
- 1002. 写出这个数 (20)
- 【n阶乘求尾数有多少个0】
- Java多线程--CompletionService的使用
- 基于redis单节点实现分布式锁
- 极光推送
- 超大数据量存储常用数据库分表分库算法总结
- 1002. 写出这个数 (20)
- TCP粘包问题分析和解决
- elasticsearch centos7集群部署案例
- bzoj 1458(最小割)
- 测试用例编写(功能测试框架)
- leetcode 32 Longest Valid Parentheses
- 完美数列(PAT-1030)
- Android一个apk多个入口
- 资讯精选 | 系统更新不及时 87%的Android设备易受攻击