Day 006:PAT练习--1005 Spell It Right (20 分)
上星期一直在写报告乱七八糟的,从今天开始不刷乙级的了,还是多刷甲级进步来得快一点!

显而易见,该题的关键在于将输入之和的每一位从高到低输出,这里我们发现题意中的输入数的范围为0-10^100,显然我们可以使用字符串输入,之后分别遍历每一位,将其分别存入sum中。
因为是从高到低输出每一位,而不是从低到高:若是后者,则可以使用一个小函数不断取余再除以10即可全部输出;此时是前者,则可使用sstream头文件,将sum转换为字符串形式,再分别遍历每一位,输出每一位的值对应的字符数组分量,即可AC。
这里我们要注意,要不就使用字符串数组存储数字转换出的英文单词;否则若使用普通的字符数组的话,要在变量名前加※,以表示其为一个分量不限长度的字符数组,输出时也要以字符串形式输出。
代码如下:
#include<iostream>
#include<string>
#include<sstream>
#include<cstring>
#include<algorithm>
using namespace std;
char *num[10] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
void ans(string s){
for(int i = 0; i < s.length(); i++){
printf("%s", *(num + int(s[i] - 48)));
if(i != s.length() - 1){
printf(" ");
}
}
}
int main(){
string s;
getline(cin, s);
int len = s.length(), sum = 0;
for(int i = 0; i < len; i++){
sum += (s[i] - 48);
}
stringstream ss;
string temp;
ss << sum;
ss >> temp;
ans(temp);
return 0;
}
继续加油!争取能拿90+!
Day 006:PAT练习--1005 Spell It Right (20 分)的更多相关文章
- PAT Advanced 1005 Spell It Right (20 分)
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 ...
- 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 ...
- 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 (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. Spell It Right (20) 【字符串】
题目链接 https://www.patest.cn/contests/pat-a-practise/1005 思路 因为 n <= 10^100 所以 要用字符串读入 但是 100 * 100 ...
- PAT (Advanced Level) Practice 1005 Spell It Right (20 分) (switch)
Given a non-negative integer N, your task is to compute the sum of all the digits of N, and output e ...
- 【PAT甲级】1005 Spell It Right (20 分)
题意: 给出一个非零整数N(<=10^100),计算每位之和并用英文输出. AAAAAccepted code: #include<bits/stdc++.h> using name ...
- 1005 Spell It Right (20 分)
Given a non-negative integer N, your task is to compute the sum of all the digits of N, and output e ...
随机推荐
- K-good number Theory + 数学问题
这道题是我做CodeTon Round1时的D题,总的来看思路很重要,有几个比较明显的切入问题的角度,要选择到最优的那个: 先看题目: 我们可以发现,这道题的描述一目了然,就是说我们能不能找k个数的和 ...
- Qt自定义控件之可伸缩组合框(GroupBox)控件
摘要 本文基于QGroupBox扩展了一种可以伸缩的组合框,正常状态下,组合框处于收缩状态,内部的控件是隐藏的:需要的时候,可以将组合框进行伸展,并将内部控件显示出来. 正文 实现的代码比较简单,主要 ...
- Java Byte不能用equals
- 哪一个 bash 内置命令能够进行数学运算?
bash shell 的内置命令 let 可以进行整型数的数学运算. #! /bin/bash--let c=a+b--
- @Param注解和@Mapper注解
@Param 1.如果dao方法中只有一个参数,入参可以为#{0}或者#{任意单词},也可以使用@Param指定参数名称,sql中就只能#{指定名称}获取参数 public List<Regio ...
- 为什么要使用 rabbitmq?
1.在分布式系统下具备异步,削峰,负载均衡等一系列高级功能; 2.拥有持久化的机制,进程消息,队列中的信息也可以保存下来. 3.实现消费者和生产者之间的解耦. 4.对于高并发场景下,利用消息队列可以使 ...
- java-設計模式-生成器
生成器模式Bulider 使你能够分步骤创建复杂对象. 该模式允许你使用相同的创建代码生成不同类型和形式的对象. 将一个复杂对象的构造与它的表示分离,使同样的构建过程可以创建不同的表示. 将一个复杂的 ...
- Spring的@Enable*注解的工作原理
转自:https://blog.csdn.net/chengqiuming/article/details/81586948 一 列举几个@Enable*注解的功能 @EnableAspectJAut ...
- 如何设置出IDEA中VCS下的Enable Version Control Intergration
File–>settings–>Version Control
- 基于MPC算法的车辆多目标自适应巡航控制系统研究_荆亚杰