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 ...
随机推荐
- restframework 解析器、渲染器、url控制组件
一.解析器 解析器的作用就是服务端接收客户端传过来的数据,把数据解析成自己可以处理的数据.本质就是对请求体中的数据进行解析. 1.分类 from rest_framework.parsers impo ...
- Ninject 初步 -Getting Started with Ninject 精通ASP-NET-MVC-5-弗瑞曼 Listing 6-10
- Xen+OpenQRM快速部署
一.选择系统平台 a) Ubuntu-11.10-server-amd64 二.Xen安装 a) Xen安装 # apt-get -y install xen-hypervisor-4.1-a ...
- Ubuntu下makefile的简单使用
在Windows下,只需要简单的点击以下make,rebuild即可.而在Linux下,这样的IDE环境并没有提供,难道必须每一步都执行一遍吗?比较ok的做法自然是能够利用批处理脚本来进行操作了,这样 ...
- JPA基本注解的使用
一:JPA基本注解 使用: 使用: 使用: 查看表: 二:用table来生成主键 使用: allocationSize:每次增加多少 tablel:指定使用那张表 执行两次main方法后查看表: jp ...
- HTML-01-HTML格式
<!DOCTYPE html><!--告诉浏览器这个文档是html5版本的文档声明--> <html> <!--html是我们网页中最大的标签--> & ...
- ImportError: Failed to import pydot. You must install pydot and graphviz for `pydotprint` to work.
用了pip install pydot; pip install graphviz都不行 去网上查了才发现window下要去https://graphviz.gitlab.io/下载windows版本 ...
- Centos与Ubuntu
共同点 1.两个系统都分别有桌面系统与服务器系统,不过ubuntu的桌面从外观上来看要比centos的漂亮 不同点 1.centos中新建的普通用户是没有sudo权限的,如果想让普通用户拥有sudo权 ...
- 深浅拷贝 集合(定义,方法) 函数(定义,参数,return,作用域) 初识
深浅拷贝 在python中浅拷贝 a=[1,2,3,4,]b=a.copy()b[0]='3333'print(a) #[1, 2, 3, 4] 浅拷贝一层并不会对a造成变化print(b) #[33 ...
- oracle问题之死锁 (一)
[前言] 遇到 oracle 异常 和 解决实践 系列文章 整理分享 杂症一.oracle死锁 一.症状: 执行SQL或程序时,程序没有响应或SQL执行一直处于执行状态,没有成功,也没有报错. 二.病 ...