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 ...
随机推荐
- 史上最简约的vi教程,复制和粘贴
上一篇博客,讲了"新手"如何"入门"vi,解决了"两眼一抹黑"的情况.知道在vi下如何进行基本的操作,如部署在Linux下的项目,修改配置文 ...
- Java入门 - 语言基础 - 18.正则表达式
原文地址:http://www.work100.net/training/java-regular-expression.html 更多教程:光束云 - 免费课程 正则表达式 序号 文内章节 视频 1 ...
- hbase伪分布式安装以及实例演示
参考指路:https://www.cnblogs.com/wang-jx/p/9672072.html (包含实例演示,这里就不copy人家的心血了) 1.下载对应安装包解压 1.1下载 同样建议选择 ...
- Win10如何关闭最近使用文件夹
如果默认开启了最近使用文件夹功能的话,最近浏览的文件就会被记录下来.所以看完各种教学资料后,并不是关掉或者重启别人就不知道你看了什么. 方法一: Win10怎么关闭最近使用文件夹 如何清理使用痕迹 从 ...
- Python里使用转义字符\r时遇到的问题
在Pycharm里使用转义字符\r和在IDLE里使用\r产生的结果是不一样的. 例子如下: print("你好!\r我是Python!") 输出结果为: 我是Python! 前面的 ...
- Jutil 单元测试
eclipse 自身集成了jtuil,右击项目,点击bulidpath,addjar,如下所示添加jutil 新建一个Junit的测试类用来测试上面的测试方法,新增Junit的测试类方法如下: // ...
- maven jar 包问题
1. Failure to transfer... 这种错误基本是所需要的 jar 包不存在,或者下载不完整,可去本地仓库查看相关 jar 文件的完整性 解决方案: 删除对应 jar 包,重新下载(删 ...
- 10个很多人不知道的Redis使用技巧
前言 Redis 在当前的技术社区里是非常热门的.从来自 Antirez 一个小小的个人项目到成为内存数据存储行业的标准,Redis已经走过了很长的一段路.随之而来的一系列最佳实践,使得大多数人可以正 ...
- 利用Atomic, ThreadLocal, 模仿AQS, ReentrantLock
/** * @description 队列同步器,利用原子整形模仿AQS,非公平锁(简单自适应自旋) * @since 2020/2/4 */ public class QueueSynchroniz ...
- 视觉slam十四讲第8章课后习题3+稀疏直接法程序注释
版权声明:本文为博主原创文章,转载请注明出处: http://www.cnblogs.com/newneul/p/8571653.html 3.题目回顾:在稀疏直接法中,假设单个像素周围小块的光度也不 ...