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 ...
随机推荐
- 如何使用docker制作开发集成环境
1. 编写最基本的Dockerfile 内容:touch 一个Dockerfile FROM ubuntu 2. 创建基本的docker镜像 docker build -t ubuntu:v1 . ...
- 什么是SpringCloudConfig?
在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件.在Spring Cloud中,有分布式配置中心组件spring cloud config ,它支持 ...
- Hystrix相关注解?
@EnableHystrix:开启熔断 @HystrixCommand(fallbackMethod="XXX"):声明一个失败回滚处理函数XXX,当被注解的方法执行超时(默认是 ...
- request表示HttpServletRequest对象?
request表示HttpServletRequest对象.它包含了有关浏览器请求的信息,并且提供了几个用于获取cookie, header, 和session数据的有用的方法. response表示 ...
- 使用 Spring 框架的好处是什么?
轻量:Spring 是轻量的,基本的版本大约 2MB.控制反转:Spring 通过控制反转实现了松散耦合,对象们给出它们的依 赖,而不是创建或查找依赖的对象们.面向切面的编程(AOP):Spring ...
- Grep 命令有什么用? 如何忽略大小写? 如何查找不含 该串的行?
是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹 配的行打印 出来. grep [stringSTRING] filename grep [^string] filename
- 《剑指offer》面试题4:替换空格
面试题4:替换空格 题目:请实现一个函数,把字符串中的每个空格替换成"%20",例如输入"We are happy.",则输出"we%20are%20 ...
- 搞半天,全国34个省份包含湾湾\香港\澳门的高德poi兴趣点23类数据终于爬完事了
1.技术架构: python+阿里云数据库mongodb5.0+高德地图rest api 2.成本: 阿里云数据库mongodb5.0一个月话费1k多 2.遇到的问题 1)两个阿里云账号下 mongo ...
- CSS: 给表格的第一列和最后一列不同的样式
table td:first-child { width:160px; height:20px; border:solid 1px Black; padding:5px; text-align:cen ...
- Head标签里面的dns-prefetch,preconnect,prefetch和prerender
开始 今天突然心血来潮想起前端性能优化的问题,这基本是老生常谈的事情了,面试随便都能说上几个,但是还是有点疑问:就是Head标签了,记忆中Head可是藏龙卧虎,各种技能都有,当然这些不可能都一一记住, ...