PAT甲级——1005.SpellItRight(20分)
Given a non-negative integer N, your task is to compute the sum of all the digits of N, and output every digit of the sum in English.
Input Specification:
Each input file contains one test case. Each case occupies one line which contains an N (≤10
100
).
Output Specification:
For each test case, output in one line the digits of the sum in English words. There must be one space between two consecutive words, but no extra space at the end of a line.
Sample Input:
12345
Sample Output:
one five
个人最初的题解思路是设定字符串常量,求和得出数值后使用if else计算出各位的数字对应输出:
#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
int sum = 0;
char str[101];
const char* a[]={"zero","one","two","three","four","five","six","seven","eight","nine"};
cin>>str;
int len=strlen(str);
for(int i=0;i<len;++i)
{
sum = sum+(str[i]-'0');
}
//cout<<sum<<endl;
if(sum>=0&&sum<10)
{
printf("%s",a[sum%10]);
}
else if(sum>10&&sum<100)
{
printf("%s %s",a[sum/10],a[sum%10]);
}
else if(sum>100)
{
printf("%s %s %s",a[sum/100],a[(sum/10)%10],a[sum%10]);
}
return 0;
}
更优的方法是:
#include <iostream>
using namespace std;
int main() {
string a;
cin >> a;
int sum = 0;
for (int i = 0; i < a.length(); i++)
sum += (a[i] - '0');
string s = to_string(sum);
string arr[10] = {"zero", "one", "two", "three", "four", "five", "six",
"seven", "eight", "nine"};
cout << arr[s[0] - '0'];
for (int i = 1; i < s.length(); i++)
cout << " " << arr[s[i] - '0'];
return 0;
}
to_string函数将数字转为字符串完美解决了各个位置上的数值输出问题
PAT甲级——1005.SpellItRight(20分)的更多相关文章
- PAT 甲级 1035 Password (20 分)(简单题)
1035 Password (20 分) To prepare for PAT, the judge sometimes has to generate random passwords for ...
- PAT 甲级 1077 Kuchiguse (20 分)(简单,找最大相同后缀)
1077 Kuchiguse (20 分) The Japanese language is notorious for its sentence ending particles. Person ...
- PAT 甲级 1061 Dating (20 分)(位置也要相同,题目看不懂)
1061 Dating (20 分) Sherlock Holmes received a note with some strange strings: Let's date! 3485djDk ...
- PAT甲级——1035 Password (20分)
To prepare for PAT, the judge sometimes has to generate random passwords for the users. The problem ...
- PAT甲级——1061 Dating (20分)
Sherlock Holmes received a note with some strange strings: Let's date! 3485djDkxh4hhGE 2984akDfkkkkg ...
- PAT甲级——1077.Kuchiguse(20分)
The Japanese language is notorious for its sentence ending particles. Personal preference of such pa ...
- 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 1035 Password(20 分)
1035 Password(20 分) To prepare for PAT, the judge sometimes has to generate random passwords for the ...
随机推荐
- [转自官方文档] Django——render()
每个视图都需要做2件事,返回一个包含被请求页面内容的HttpResponse对象或者一个404 快捷函数 render( 请求, 模板, 内容) 载入模板,填充上下文,再返回它生成的HttpResp ...
- MVC学生管理系统-阶段V(模糊查询)
项目源码 :https://download.csdn.net/download/weixin_44718300/11091042 此处省略一段话.去上一篇查看 NO01:修改list.jsp < ...
- java集合对象区别一
Vector和ArrayList 1.vector是线程同步的,所以他也是线程安全的,而ArrayList是线程异步的,是不安全的.如果不考虑到线程的安全因素,一般用ArrayList效率较高. 2. ...
- DispatcherServlet(2)_HandlerMapping
HandlerMapping_xmind SpringMVC默认提供的HandlerMapping BeanNameUrlHandlerMapping SimpleUrlHandlerMapping ...
- Linux 压缩解压操作
Linux 压缩解压操作 Linux解压文件到指定目录 tar在Linux上是常用的打包.压缩.加压缩工具,他的参数很多,折里仅仅列举常用的压缩与解压缩参数 参数:-c :create 建立压缩档案的 ...
- 51Nod1085 0-1背包(一维和二维数组实现)
背包是典型的动态规划问题,关于背包问题的详解,推荐博客:点击打开链接(这篇博客有点错误,代码for循环里错了,不过讲解 的很详细) 题目如下: 在N件物品取出若干件放在容量为W的背包里,每件物品的体积 ...
- js保留的关键字
js保留的关键字 break else new var case finally return void catch for switch while continue function this w ...
- MySQL的DDL和DML
SQL语句:结构化查询语句,使用SQL与数据库“沟通”,完成相应的数据库操作. 语句分类 DDL(Data Definition Languages)语句:即数据库定义语句,用来创建数据库中的表.索引 ...
- UVA 11019 二维匹配 AC自动机
这个题目要求在一个大矩阵里面匹配一个小矩阵,是AC自动机的灵活应用 思路是逐行按普通AC自动机匹配,用过counts[i][j]记录一下T字符矩阵以i行j列为开头的与P等大的矩阵区域 有多少行已经匹配 ...
- KL散度与JS散度
1.KL散度 KL散度( Kullback–Leibler divergence)是描述两个概率分布P和Q差异的一种测度.对于两个概率分布P.Q,二者越相似,KL散度越小. KL散度的性质:P表示真实 ...