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 ...
 
随机推荐
- Vue中图片的加载方式
			
一.前言 VUE项目中图片的加载是必须的,那么vue中图片的加载方式有哪些呢,今天博主就抽点时间来为大家大概地捋一捋. 二.图片的加载方法 1.在本地加载图片(静态加载) 图片存放assets文件夹中 ...
 - KVM虚拟机cpu资源限制和vcpu亲缘性绑定
			
前言 KVM中添加的实例存在资源分布不均的情况,这样如果有消耗资源的实例会影响到其他实例的服务正常运行,所以给kvm做资源限制是很有必要的,下面记录一下在centos7中KVM环境下使用cgroup限 ...
 - SpringBoot:自定义注解实现后台接收Json参数
			
0.需求 在实际的开发过程中,服务间调用一般使用Json传参的模式,SpringBoot项目无法使用@RequestParam接收Json传参 只有@RequestBody支持Json,但是每次为了一 ...
 - GC root & 使用MAT分析java堆
			
当我们的java程序遇到频繁full gc或者oom的时候,我们常常需要将当前的heap dump出来进行进一步的分析.MAT是用于分析heap dump的神器. 1 生成heap dump heap ...
 - 您对 Distributed Transaction 有何了解?
			
分布式事务是指单个事件导致两个或多个不能以原子方式提交的单独数据源的突 变的任何情况.在微服务的世界中,它变得更加复杂,因为每个服务都是一个工 作单元,并且大多数时候多个服务必须协同工作才能使业务成功 ...
 - 学习Jenkins(二)
			
一:持续集成的概念: 总体的概括 持续集成Continuous Integration 持续交付Continuous Delivery 持续部署Continuous Deployment 二:安装部署 ...
 - Leetcode1——两数之和 详细解析
			
Leetcode1--两数之和 题目分类:数组.哈希表的应用 1. 题目描述 给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 target 的那 两个 整数 ...
 - 阿里云、腾讯云、CentOS下的MySQL的安装与配置详解
			
一. 安装 查看是否已安装 # 查看MySQL版本 mysql --version # 查看MySQL相关文件 whereis mysql 若已安装,卸载方法如下 # 卸载MySQL yum remo ...
 - WordPress 网站开发“微信小程序“实战(三)
			
本文是"WordPress 开发微信小程序"系列的第三篇,本文记录的是开发"DeveWork+"小程序1.2 版本的过程.建议先看完第一篇.第二篇再来阅读本文. ...
 - 判断链表是否有环(Java实现)
			
判断给定的链表中是否有环.如果有环则返回true,否则返回false. 解题思路:设置两个指针,slow和fast,fast每次走两步,slow每次走一步,如果有环的话fast一定会追上slow,判断 ...