PAT乙级1002题python3代码

目录

      • 目录
  • PAT乙级1002题python3代码
      • 代码

PAT乙级1002题python3代码

题目:
读入一个自然数n,计算其各位数字之和,用汉语拼音写出和的每一位数字。

输入格式:每个测试输入包含1个测试用例,即给出自然数n的值。这里保证n小于10100。

输出格式:在一行内输出n的各位数字之和的每一位,拼音数字间有1 空格,但一行中最后一个拼音数字后没有空格。

输入样例:
1234567890987654321123456789
输出样例:
yi san wu

代码

    # -*- coding: utf-8 -*-
"""
Created on Mon Mar 27 17:00:59 2017

@author: xyq152438
""" 
    def shuchu(he):   #把he转换成拼音输出,仅可用于最后一个数字
        if  he ==0:
            print('ling')
        elif he == 1:
            print('yi')
        elif he == 2:
            print('er')
        elif he == 3:
            print('san')
        elif he == 4:
            print('si')
        elif he == 5:
            print('wu')
        elif he == 6:
            print('liu')
        elif he == 7:
            print('qi')
        elif he == 8:
            print('ba')
        elif he == 9:
            print('jiu')

    def shuchu_1(he):  #把he转换成拼音输出,不带空格,即不在最后一个数字使用
        if  he ==0:
            print('ling',end = " ")
        elif he == 1:
            print('yi',end = " ")
        elif he == 2:
            print('er',end = " ")
        elif he == 3:
            print('san',end = " ")
        elif he == 4:
            print('si',end = " ")
        elif he == 5:
            print('wu',end = " ")
        elif he == 6:
            print('liu',end = " ")
        elif he == 7:
            print('qi',end = " ")
        elif he == 8:
            print('ba',end = " ")
        elif he == 9:
            print('jiu',end = " ")

    def panduan(shu):     #判断输入的数字
        b = list(shu)
        he = 0
        for i in range(len(b)):   #循环遍历长度,he为各位数字之和
            he+=int(b[i])
        he_1 =he                  
        if len(b) == 1:
           shuchu(he)        
        else:
            for i in range(len(str(he)),0,-1): #倒序遍历he,判断输出条件
                if he_1/(pow(10,i-1)) ==0:
                    if i==1:
                        print('ling')
                    else:
                        print('ling',end=" ")
                else:
                    shuchu_1(int(he_1/(pow(10,i-1))))
                    he_1-=(int(he_1/(pow(10,i-1))))*(pow(10,i-1))  
                    if i==2: 
                        shuchu(he_1)
                        break

    a = input()
    panduan(a)

你可能感兴趣的:(python,python)