(Python实现)PAT 1005 Spell It Right (20 分)

1005 Spell It Right (20 分)
Given a non-negative integer N, your task is to compute the sum of all the digits of N, and output every digit of the sum in English.

Input Specification:
Each input file contains one test case. Each case occupies one line which contains an N (≤10
​100
​​ ).

Output Specification:
For each test case, output in one line the digits of the sum in English words. There must be one space between two consecutive words, but no extra space at the end of a line.

Sample Input:

12345

Sample Output:

one five

作者:CHEN, Yue
单位:浙江大学
代码长度限制:16 KB
时间限制:400 ms
内存限制:64 MB

解题思路:
先求和计算,然后字符串转换为列表进行遍历。

代码:

num = input('')
sum_ = 0
for i in range(len(num)):
    sum_ = sum_ + int(num[i])
lst = list(str(sum_))
for j in range(len(lst)):
    if j != 0:
        print(' ', end = '')
    if lst[j] == '1':
        print('one', end = '')
    if lst[j] == '2':
        print('two', end = '')
    if lst[j] == '3':
        print('three', end = '')
    if lst[j] == '4':
        print('four', end = '')
    if lst[j] == '5':
        print('five', end = '')
    if lst[j] == '6':
        print('six', end = '')
    if lst[j] == '7':
        print('seven', end = '')
    if lst[j] == '8':
        print('eight', end = '')
    if lst[j] == '9':
        print('nine', end = '')
    if lst[j] == '0':
        print('zero', end = '')

提交记录:
在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Vicky__3021

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值