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 ...
随机推荐
- 关于Hive中case when不准使用子查询的解决方法
在公司用Hive实现个规则的时候,遇到了要查询某个字段是否在另一张表中,大概情况就是 A表: id value1 value2 1 100 0 2 101 1 3 102 1 B表: value1 1 ...
- C++封装的基于WinSock2的TCP服务端、客户端
无聊研究Winsock套接字编程,用原生的C语言接口写出来的代码看着难受,于是自己简单用C++封装一下,把思路过程理清,方便自己后续翻看和新手学习. 只写好了TCP通信服务端,有空把客户端流程也封装一 ...
- RestTemplate---Spring提供的轻量Http Rest 风格API调用工具
前言 今天在学习Spring Cloud的过程中无意发现了 RestTemplate 这个Spring 提供的Http Rest风格接口之间调用的模板工具类,感觉比Apache提供的HttpClien ...
- Shoot the Bullet(有源汇带上下界最大流)
有源汇带上下界最大流 在原图基础上连一条汇点到源点流量为inf的边,将有源汇网络流转化为无源汇网络流用相同方法判断是否满流,如果满流再跑一边源点到汇点的最大流就是答案 例题:Shoot the Bul ...
- 【学习笔记】Linux基础(二):Linux的基本操作
二.Linux的基本操作 0.正确的开关机操作 开机和登陆: 安全起见,一般不使用最高权限的root账户登入系统,光立系统时再使用 登录时为login程序提供账户名和密码即可,密码不会被显示,登陆后显 ...
- yield 伪并发例子
import timedef custumer(name): print('%s 准备吃饺子了'%name) while True: curry = yield print('饺子%s来了 ,被%s吃 ...
- Idea | Load error: undefined path variables
Load error: undefined path variables 案例 今天打开idea项目,突然间出现如下异常: Load error: undefined path variables 类 ...
- artTemplate--使用artTemplate时,由于json对象属性有数字命名格式 导致调用报错 syntax error
案例 今天在使用artTemplate做开发时,遇到一个比较奇葩的问题,就是使用json对象去获取值得时候,报如下错误: Template Error <temp> function an ...
- win10下安装Anaconda3
1.官方下载:https://www.anaconda.com/distribution/#download-section (最新版直接下即可) 或者国内镜像下载:https://mirrors. ...
- Hexo+coding实现自动化部署
前言 昨天写了一篇利于云环境写博客,但是让群里大佬们看了下.评论道:"写的不错,但还是觉得这个云环境太繁琐了,没有CI/CD自动化部署方便".于是我便百度查了下,网上文章大部分是通 ...