PAT【乙级】JAVA——1002 写出这个数之习题思路

目录

一、练习题目

二、遇到问题

三、练习实战

3.1 练习实战

3.1.1 自我练习

3.1.2 自我练习总结

3.2 参考答案

3.2.1 日沉云起Github【转载】

3.3 自我修正 


一、练习题目

https://pintia.cn/problem-sets/994805260223102976/problems/994805324509200384

二、遇到问题

暂无

三、练习实战

3.1 练习实战

3.1.1 自我练习

package lambda;

/**
 * Created by Administrator on 2019/8/13.
 */

/**
 * @Description : 1002 写出这个数
 * @Auth : Xuhy
 * @Date : 2019-08-13
 * @Time : 10:45
 */
public class Test1002 {
    public static void main(String[] args) {
        int idx = 0;
        int sum = 0;

        String n = "1234567890987654321123456789";
        while (idx <= n.length() - 1) {
            char idxChar = n.charAt(idx);
            sum += Integer.valueOf(idxChar + "");
            // System.out.println("idxChar = [" + idxChar + "] sum = [" + sum + "]");
            idx++;
        }
        String str = chinese(sum);
        System.out.println("result 为 [" + str + "]");
    }

    /**
     * 对于不同的数字,转化成不同的中文拼音
     *
     * @param  sum
     * @return 对应中文拼音
     * @date Xuhy,2019-08-13 11:16
     */
    private static String chinese(int sum) {
        StringBuffer buffer = new StringBuffer();

        // sum转化成字符串
        String sumStr = sum + "";
        // sum的长度
        int sumStrSize = sumStr.length();
        // sum的下标
        int sumIdx = 0;

        while (sumStrSize > 0) {
            // 获取每一个下标的字符
            char c = sumStr.charAt(sumIdx);
            String change = c + "";

            // 字符串转化
            switch (change) {
                case "1":
                    change = "yi";
                    break;
                case "2":
                    change = "er";
                    break;
                case "3":
                    change = "san";
                    break;
                case "4":
                    change = "si";
                    break;
                case "5":
                    change = "wu";
                    break;
                case "6":
                    change = "liu";
                    break;
                case "7":
                    change = "qi";
                    break;
                case "8":
                    change = "ba";
                    break;
                case "9":
                    change = "jiu";
                    break;
                case "10":
                    change = "shi";
                    break;
                default:
            }
            sumIdx++;
            sumStrSize--;

            // 输出结构拼接
            buffer.append(change + " ");
        }

        return buffer.substring(0, buffer.length() - 1);
    }
}

3.1.2 自我练习总结

感觉这么写有带你复杂,考试的话时间不够⊙﹏⊙b汗。

3.2 参考答案

3.2.1 日沉云起Github【转载】

#include
using namespace std;
int main(){
    string s;
    cin>>s;
    int sum=0;
    for(int i=0;i0?" ":"",temp[s[i]-'0'].c_str());
    return 0;
}

3.3 自我修正 

思路:根据1对应”yi“操作数组,创建数组!!!

    public static void main(String[] args) {
        StringBuffer buffer = new StringBuffer();

        String s = "1234567890987654321123456789";
        int sum = 0;

        // 求和
        for (int i = 0; i < s.length(); ++i) {
            sum += s.charAt(i) - '0';
        }

        // 将s覆盖成sum的字符串
        s = (sum) + "";
        // 创建对应拼音数组
        String temp[] = {"ling", "yi", "er", "san", "si", "wu", "liu", "qi", "ba", "jiu"};

        // 循环获取拼音拼接
        for (int i = 0; i < s.length(); ++i) {
            int idx = Integer.parseInt(s.charAt(i) + "");
            buffer.append(temp[idx] + " ");
        }
        System.out.println(buffer.toString().substring(0, buffer.toString().length() - 1));
    }

 

你可能感兴趣的:(PAT,乙级)