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 ...
随机推荐
- slf4j NoSuchMethodError 错误 ---- 版本冲突
java.lang.NoSuchMethodError: org.slf4j.spi.LocationAwareLogger.log(Lorg/slf4j/Marker;Ljava/lang/Stri ...
- HTML笔记01
HTML语法规范 <!DOCTYPE html>//HTML5规范 用于注释<!-- HTML文件主要包含头部分和体部分 <!-title> 指定网站标题 指定浏览器打开 ...
- 吴裕雄--天生自然Django框架开发笔记:Django Admin 管理工具
Django 提供了基于 web 的管理工具. Django 自动管理工具是 django.contrib 的一部分.可以在项目的 settings.py 中的 INSTALLED_APPS 看到它: ...
- Codeforces 1296D - Fight with Monsters
题目大意: n 只怪兽,每只的血量为 h[i] ,你的攻击力为 a ,你的对手攻击力为 b 打每只怪兽时,都是你先出手,然后你的对手出手,这样轮流攻击 如果是你给予了怪兽最后一击,你就能得到一分 你还 ...
- POJ - 3660 Cow Contest(flod)
题意:有N头牛,M个关系,每个关系A B表示编号为A的牛比编号为B的牛强,问若想将N头牛按能力排名,有多少头牛的名次是确定的. 分析: 1.a[u][v]=1表示牛u比牛v强,flod扫一遍,可以将所 ...
- 专为前端开发者准备的15款优秀的Sublime Text插件
Sublime Text 已成为了目前最流行的代码编辑器之一.它的反应速度.简单易用性以及丰富的插件生态,让众多前端开发者们为之倾倒. 为了帮助开发者们更便捷地使用 Sublime Text ,我们决 ...
- Codeforces 997A Convert to Ones(思维)
https://codeforces.com/problemset/problem/997/A 题目大意: 给定一串0-1序列,定义两种操作: 操作一:选取一连续串倒置. 操作二:选取一连续串把进行0 ...
- POJ 1141 经典DP 轨迹打印
又几天没写博客了,大二的生活实在好忙碌啊,开了五门专业课,每周都是实验啊实验啊实验啊....我说要本月刷够60题,但好像完不成了,也就每天1题的样子.如今写动规还是挺有条理的,包括这道需要打印轨迹,其 ...
- ubuntu下查询网络的常用命令
1.查看无线路由器(网关)的IP地址 在terminal中输入如下命令,即可查询到当前使用的路由器的IP地址(我电脑是连着一个无线网络还有一个DSL连接): route|grep -i default ...
- 用eclipse运行算法第四版的BinarySearch
import java.util.Arrays; import edu.princeton.cs.algs4.In; import edu.princeton.cs.algs4.StdIn; impo ...