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 ...
随机推荐
- OO第一单元
OO第一单元总结 目录 OO第一单元总结 前言 第一次作业 HW1基本思路 UML类图 代码规模 复杂度分析 方法复杂度 分析 类复杂度 分析 优化策略 第二次作业 HW2基本思路 UML类图 代码规 ...
- List 操作add 报错
操作List报java.lang.UnsupportedOperationException 2018.03.12 16:52:01字数 230阅读 1683 问题描述 今天在项目中调用List的ad ...
- 如何处理异形屏iphone X?
safe area: 默认放置在安全区域以避免遮挡, 但会压缩 在meta中添加viewport-fit=cover: 告诉浏览器要讲整个页面渲染到浏览器中,不管设备是圆角与否,这个时候会造成页面的元 ...
- spring学习一:spring入门及相关概念介绍
1:Spring的概念:(03年兴起) (1) 开源的轻量级的框架(无需复杂的环境,不依赖其他) (2) 一站式框架(Spring在javaee的三层结构中,对每一层都提供不同的解决技术: ...
- java使用正则表达式在文档里找匹配
public static void main(String[] args) { String str = "123我是456张三789的学生"; String regex2 = ...
- mac终端所有命令不能用
最近一次在用终端敲命令的时候发现命令总是不执行(只有cd命令可以正常执行),返回命令未识别的错误-bash: source: command not found 相信很多朋友也会遇到类似的问题. 解决 ...
- 如果一个表有一列定义为 TIMESTAMP,将发生什么?
每当行被更改时,时间戳字段将获取当前时间戳. 列设置为 AUTO INCREMENT 时,如果在表中达到最大值,会发生什么情况? 它会停止递增,任何进一步的插入都将产生错误,因为密钥已被使用. 怎样才 ...
- 列举 spring 支持的事务管理类型?
Spring 支持两种类型的事务管理: 1. 程序化事务管理:在此过程中,在编程的帮助下管理事务.它为您提供极大 的灵活性,但维护起来非常困难. 2. 声明式事务管理:在此,事务管理与业务代码分离.仅 ...
- ROS的安装-> rosdep init /update报错2022.02.24实测有效
ROS的安装-> rosdep init /update报错2022.02.24实测有效 一. 解决rosdep_init问题 正常执行sudo rosdep init会报错,如下: ERR ...
- python中类变量和实例变量的区别
类变量:可在类的所有实例之间共享的值(也就是说,它们不是单独分配给每个实例的).实例变量:实例化之后,每个实例单独拥有的变量. class student(): age = 0 name = 'stu ...