PAT-1005 Spell It Right 解答(C/C++/Java/python)
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)的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- PAT 1005
1005. Spell It Right (20) Given a non-negative integer N, your task is to compute the sum of all the ...
- 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 ...
随机推荐
- .net Core 2.*使用autofac注入
创建项目 1.创建一个.net core 项目 2.创建一个类库 2.1创建interface文件夹 2.2创建Service文件夹 好了给大家看项目目录 对的.我创建了一个IUserService和 ...
- java8新特性Lambda和Stream
Java8出来已经4年,但还是有很多人用上了jdk8,但并没用到里面的新东西,那不就等于没用?jdk8有许多的新特性,详细可看下面脑图 我只讲两个最重要的特性Lambda和Stram,配合起来用可以极 ...
- Collections.synchronizedList 、CopyOnWriteArrayList、Vector介绍、源码浅析与性能对比
## ArrayList线程安全问题 众所周知,`ArrayList`不是线程安全的,在并发场景使用`ArrayList`可能会导致add内容为null,迭代时并发修改list内容抛`Concurre ...
- JVM 面试题汇总
JVM 面试题汇总 1.什么是 JVM?它有什么作用? 答:JVM 是 Java Virtual Machine(Java 虚拟机)的缩写,顾名思义它是一个虚拟计算机,也是 Java 程序能够实现跨平 ...
- 《ASP.NET Core 高性能系列》静态文件中间件
一.概述 静态文件(如 HTML.CSS.图片和 JavaScript等文件)是 Web程序直接提供给客户端的直接加载的文件. 较比于程序动态交互的代码而言,其实原理都一样(走Http协议), ASP ...
- java代码之美(14)---Java8 函数式接口
Java8 函数式接口 之前写了有关JDK8的Lambda表达式:java代码之美(1)---Java8 Lambda 函数式接口可以理解就是为Lambda服务的,它们组合在一起可以让你的代码看去更加 ...
- ros中launch启动文件的使用方法
launch文件:通过XML文件实现多节点的配置和启动(可自动启动ROS Master) launch文件中包含很多标签和属性 *launch文件语法 <launch> <node ...
- Python报错:PermissionError: [Errno 13] Permission denied
问题分析: 错误产生的原因是文件无法打开,可能产生的原因是文件找不到,或者被占用,或者无权限访问,或者打开的不是文件,而是一个目录. 问题解决: 1.检查对应路径下的文件是否存在,且被占用.如果文件不 ...
- 二、通过工厂方法来配置bean
调用静态工厂方法创建 Bean是将对象创建的过程封装到静态方法中. 当客户端需要对象时, 只需要简单地调用静态方法, 而不同关心创建对象的细节. 要声明通过静态方法创建的 Bean, 需要在 Bean ...
- MSVC下快速Unicode I/O
http://blog.kingsamchen.com/archives/863 如果需要往console输出包含非ASCII字符的宽字符串,一个比较快速的方法是使用WriteConsoleW这个AP ...