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 ...
随机推荐
- (7)opencv图片内部的基本处理
就是,给定我们一张图片,我们可以对图片的每一个像素的色彩进行处理 比如,我们的原图是这个样子 然后我首先将他变成灰度图(灰度图的行道是1,就是chanaual是1) 然后,我又将灰色图片的黑白进行颠倒 ...
- hadoop搭建一:虚拟机网络配置和基础(未完成)
基于VMware 15+CentOS 7+Hadoop 2.6,hadoop的搭建主要用于个人学习,水平有限. hadoop搭建一:虚拟机网络配置和基础 hadoop搭建二:hadoop全分布搭建 h ...
- 用Python在00:00给微信好友发元旦祝福语
2019年的元旦即将来临,这里用Python撸一串简单的代码来实现定点给微信里的所有小伙伴发祝福语 环境说明 Python版本: 不限 第三方库: itchat, schedule 注:所有祝福语来源 ...
- Linux(CENTOS7) NodeJs安装
1.下载NodeJs 官网下载地址:http://nodejs.cn/download/ 2.上传到linux系统 我这里上传到/disk/nodejs目录下面的,上传工具使用的xftp. 3 ...
- SpringCloud学习之Stream消息驱动【默认通道】(十)
在实际开发过程中,服务与服务之间通信经常会使用到消息中间件,而以往使用了中间件比如RabbitMQ,那么该中间件和系统的耦合性就会非常高,如果我们要替换为Kafka那么变动会比较大,这时我们可以使用S ...
- servlet-api api文档获取请求参数
1.假如有个get请求后面带有的参数如下: a=b&a2=b2&a3=b3&a4=b4. 如果想获取所有的key,value.这个时候可以根据request的getQueryS ...
- VUE- 访问服务器端数据 Vue-resource
VUE- 访问服务器端数据 Vue-resource 1. 安装 vue-resource cnpm install vue-resource --save 安装完毕后,在main.js中导入,如下所 ...
- Git 报错:Updates were rejected because the tip of your current branch is behind
刚开始学习 git 命令,发现会出现很多的错误,所以就总结下出现的错误,以此来加深理解和掌握吧! 环境:在本地库操作了一系列的 add 和 commit 操作后,想把本地仓库推送到远端,但是发生以下错 ...
- JavaSE--for each
参考:http://blog.csdn.net/yasi_xi/article/details/25482173 学习多线程的时候实例化线程数组而挖掘出来的一直以来的理解误区 之前一直以为for ea ...
- JavaScript—面向对象小例子
什么是面向对象 要是以前别人问我.随口道来,封装继承多态,万物皆对象...一大推.说的自己都以为自己掌握了面向对象.呵呵一笑.确实掌握了 只是不会用..... 什么是面向对象编程 以前 学.Net 虽 ...