1.Description:

2.Example:

Input:
12345
Output:

one five

3.solutions:

C Version:

 #include <stdio.h>
#include <string.h>
#include <stdlib.h> int main() {
char digits[] = {};
char NUMBERS[][] = {"zero", "one", "two", "three", "four", "five","six",
"seven", "eight", "nine", "ten"};
scanf("%s", digits);
int sum = ;
unsigned i;
for (i = ; digits != NULL && i < strlen(digits); ++i) {
sum += digits[i] - ; // '0'的Ascill码为48
}
char* output;
sprintf(output, "%d", sum);
unsigned j;
for (j = ; output[j] != '\0'; ++j) {
if (j != strlen(output) - )
printf("%s ", NUMBERS[output[j] - ]);
else
printf("%s", NUMBERS[output[j] - ]);
}
return ;
}

Note: 自己的电脑上调试无误,提交时运行错误!

C++ Version:

 #include <iostream>
using namespace std; int main() {
string digits;
string NUMBERS[] = {"zero", "one", "two", "three", "four", "five","six",
"seven", "eight", "nine", "ten"};
getline(cin, digits);
cout << digits << endl;
int sum = ;
for (int i = ; i < digits.size(); ++i) {
sum += int(digits[i]);
}
string output = to_string(sum);
for (int j = ; j < output.size(); ++j) {
if (j != output.size() - )
cout << NUMBERS[int(output[j])] << " ";
else
cout << NUMBERS[int(output[j])];
}
return ;
}

Java Version:

 import java.util.Scanner;

 /**
* Created by sheepcore on 2020-02-28
*/ public class P1005_Spell_It_Right {
public static void main(String[] args) {
String digits;
String NUMBERS[] = {"zero", "one", "two", "three", "four", "five","six",
"seven", "eight", "nine", "ten"};
Scanner scan = new Scanner(System.in);
digits = scan.nextLine();
int sum = 0;
for (int i = 0; i < digits.length(); ++i) {
sum += Integer.parseInt(digits.charAt(i) + "");
}
String output = sum + "";
int idx;
for (int j = 0; j < output.length(); ++j) {
if (j != output.length() - 1){
idx = Integer.parseInt(output.charAt(j) + "");
System.out.print(NUMBERS[idx] + " ");
}
else {
idx = Integer.parseInt(output.charAt(j) + "");
System.out.print(NUMBERS[idx]);
}
}
}
}

Python Version:

 """
created by sheepcore on 2020-2-28
""" if __name__ == "__main__":
digits = input()
NUMBERS = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven',
'eight', 'nine', 'ten']
i = 0
for ch in digits:
i += eval(ch)
sum = str(i)
output = ""
for num in sum:
output += " " + str(NUMBERS[eval(num)])
print(output.lstrip(), end='')

4.summary:

掌握C、C++、Java、Python中字符串与数字之间的转换方法。

水滴石穿,笨鸟先飞!

PAT-1005 Spell It Right 解答(C/C++/Java/python)的更多相关文章

  1. PAT 1005 Spell It Right

    1005 Spell It Right (20 分)   Given a non-negative integer N, your task is to compute the sum of all ...

  2. PAT 1005 Spell It Right 字符串处理

    Given a non-negative integer N, your task is to compute the sum of all the digits of N, and output e ...

  3. PAT (Advanced Level) Practice 1005 Spell It Right (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1005 Spell It Right (20 分) 凌宸1642 题目描述: Given a non-negative integer N ...

  4. 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 th ...

  5. PAT 甲级 1005 Spell It Right (20)(代码)

    1005 Spell It Right (20)(20 分) Given a non-negative integer N, your task is to compute the sum of al ...

  6. PAT甲1005 Spell it right【字符串】

    1005 Spell It Right (20 分) Given a non-negative integer N, your task is to compute the sum of all th ...

  7. PAT 甲 1005. Spell It Right (20) 2016-09-09 22:53 42人阅读 评论(0) 收藏

    1005. Spell It Right (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Given ...

  8. PAT 1005

    1005. Spell It Right (20) Given a non-negative integer N, your task is to compute the sum of all the ...

  9. 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 th ...

随机推荐

  1. Vmware Ubuntu18.04更换清华源

    一.安装Ubuntu18.04 省略 二.安装VmwareTool 1.选择机器右击安装2.打开文件,copy压缩文件到其它目录(理由: 内存不够解压)3.解压文件,运行./忘记名字了.pl文件4.注 ...

  2. JsonResponse和HttpResponse

    1.联系 JsonResponse继承HttpResponse 2.区别 JsonResponse 数据类型装自动换成json字符串并相应到前端,传到前端的是数据类型而非json字符串 HttpRes ...

  3. 编程基础--XML约束

    2020年新年第一天,不写一篇博客纪念一下都感觉对不起这个跨年 为什么会写一篇关于xml的博客呢?xml在编程中用的又不多,再多也用不着自己写约束文件,只要能看懂就行了不是吗?嗯,没别的原因,就是想研 ...

  4. 英语学习app——Alpha发布2

    英语学习app--Alpha发布1 这个作业属这个作业属于哪个课程 https://edu.cnblogs.com/campus/xnsy/GeographicInformationScience/ ...

  5. 初入计科,首次接触C的感受

    1 你对计算机科学与技术专业了解是怎样? 答:一开始我对这个专业并无了解,觉得无非是把电脑给学透.经过一周的学习后,我深刻地感觉自己对这个专业深深的误解. 通过翻阅书籍,上网浏览相关信息,我认为该专业 ...

  6. Deep server from scratch

    Deep server from scratch 1.install Ubuntu16.04 via flash2.wired Network by Ruijie3.install google4.S ...

  7. Day8-Python3基础-Socket网络编程

    目录: 1.Socket语法及相关 2.SocketServer实现多并发 Socket语法及相关 socket概念 socket本质上就是在2台网络互通的电脑之间,架设一个通道,两台电脑通过这个通道 ...

  8. mysql--->mysql慢查询

    简介 > 开启慢查询日志,可以让MySQL记录下查询超过指定时间的语句,通过定位分析性能的瓶颈,才能更好的优化数据库系统的性能 参数及命令说明 查看慢查询是否开启和日志存储地址 show var ...

  9. Git详解之基础使用

    前言 读完本章你就能上手使用 Git 了.本章将介绍几个最基本的,也是最常用的 Git 命令,以后绝大多数时间里用到的也就是这几个命令.读完本章,你就能初始化一个新的代码仓库,做一些适当配置:开始或停 ...

  10. 《ASP.NET Core 高性能系列》静态文件中间件

    一.概述 静态文件(如 HTML.CSS.图片和 JavaScript等文件)是 Web程序直接提供给客户端的直接加载的文件. 较比于程序动态交互的代码而言,其实原理都一样(走Http协议), ASP ...