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 ...
随机推荐
- HZNU-ACM寒假集训Day9小结 倍增
LCA 倍增法求最近公共祖先 首先对于每个结点先进行dfs预处理它的深度,再记录下它们往父亲方向走2的0次,1次...k次步所到达的结点.在这里2的k次大于整棵树的最大深度. 预处理完后,需要查询两个 ...
- PAT Advanced 1018 Public Bike Management (30) [Dijkstra算法 + DFS]
题目 There is a public bike service in Hangzhou City which provides great convenience to the tourists ...
- Django项目同步到码云
本篇博客主要记录下将刚刚初始化后的Django项目部署到码云中,首先我们需要到码云中注册一个账号,下面会讲解下如何在码云中建立一个仓库,再将其克隆到本地.最后将本地的项目推送到码云的仓库中. 码云内初 ...
- Java学生管理系统(IO版)
图解: cade: student.java /* * 这是我的学生类 */ public class Student { //学号 private String id; //姓名 private S ...
- java.io.tmpdir在哪里?
查找所在目录的方式如下: System.out.println(System.getProperty(“java.io.tmpdir”)); System.getProperty(),还可以获取更多其 ...
- ssh到ubuntu没颜色
ssh远程到ubuntu系统, 没有颜色. 原因是 .bashrc 配置没生效. $ echo '. $HOME/.bashrc' > ~/.profile
- LeetCode No.166,167,168
No.166 FractionToDecimal 分数到小数 题目 给定两个整数,分别表示分数的分子 numerator 和分母 denominator,以字符串形式返回小数. 如果小数部分为循环小数 ...
- ftp主动和被动模式区别
转载自:http://www.west999.com/cms/wiki/server/2018-11-16/49417.html FTP是基于TCP的服务的,FTP不同之处在于FTP使用两个端口,一个 ...
- 移动端— Touch事件轮播图
虽然 以前也写过手机端页面 .当时用的jquery moblie 框架.啥也不懂 就知道复制粘贴出效果 不敢改内部样式.现在呢 了解手机端原理 一些基本的概念 视口 缩放 后 .再去想以前写的页面 ...
- Scanner方式输入小写字母转换成大写字母
import java.util.Scanner; /** * 小写字母转换成大写字母 * @author zzu119 * */ public class letterTransfe ...