PAT甲级1005 Spell It Right

2024-02-14 15:18
文章标签 spell pat right 甲级 1005

本文主要是介绍PAT甲级1005 Spell It Right,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

题目描述:
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

题目大意:
给一串数字,求和,将该数的每个数字用英文打出。

代码如下:

#include <iostream>
using namespace std;
int main()
{string s,s2[]={"zero","one","two","three","four","five","six","seven","eight","nine"};cin>>s;int sum=0,k;for(int i=0;i<s.size();i++)sum+=s[i]-'0';string s3=to_string(sum);for(int i=0;i<s3.size();i++){cout<<s2[s3[i]-'0'];if(i!=s3.size()-1) cout<<" ";}return 0;
}

来总结一下:
1)数字的最大范围为10^100,所以只能用string 类来存储,求和完之后,将int转化为string类,然后对应输出相应的英文字母。

这篇关于PAT甲级1005 Spell It Right的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/708860

相关文章

PAT甲级-1044 Shopping in Mars

题目   题目大意 一串项链上有n个钻石,输入给出每个钻石的价格。用m元买一个连续的项链子串(子串长度可为1),如果不能恰好花掉m元,就要找到最小的大于m的子串,如果有重复就输出多个,按递增顺序输出子串的前端和后端索引。 原来的思路 取连续的子串使和恰等于m,没有恰等于就找最小的大于。可以将子串依次累加,使得每个位置都是起始位置到该位置的序列和,整个数组显递增顺序,就可以用右边减左边

Csting Left Mid Right

 CString Left( int nCount ) const;                   //从左边1开始获取前 nCount 个字符 CString Mid( int nFirst ) const;                      //从左边第 nCount+1 个字符开始,获取后面所有的字符 CString Mid( int nFirst, int nC

PAT (Advanced Level) Practice——1011,1012

1011:  链接: 1011 World Cup Betting - PAT (Advanced Level) Practice (pintia.cn) 题意及解题思路: 简单来说就是给你3行数字,每一行都是按照W,T,L的顺序给出相应的赔率。我们需要找到每一行的W,T,L当中最大的一个数,累乘的结果再乘以0.65,按照例子写出表达式即可。 同时还需要记录每一次选择的是W,T还是L

Python 中的 spell checker 库

Python 中的 spell checker 库 在日常生活中,我们经常遇到文本中的错误,如 misspelled words、typos 等。为了解决这些问题,我们可以使用 spell checker 库来检测和纠正文本中的错误。Python 提供了多种 spell checker 库,下面我们将介绍其中的一些库,并结合实例来演示它们的使用。 1. PyEnchant PyEnchant

PAT (Advanced Level) Practice

1001:  题目大意: 计算 a+b 的结果,并以标准格式输出——即每三个数字一组,组之间用逗号分隔(如果数字少于四位,则不需要逗号分隔)  解析: 我们知道相加右正有负,对于样例来说 Sample Input: -1000000 9 Sample Output: -999,991 如果是从左往右,算上负号的话输出应该是-99,999,1 从右往左:-,999,991离正确

join连接的五种方式的简单使用案例(Inner join,Left join,Right join,Full join,Cross join)

1.内连接Inner join 内连接是基于连接谓词将俩张表(如A和B)的列组合到一起产生新的结果表  ,在表中存在至少一个匹配时,INNER JOIN 关键字返回行。    下面是一个简单的使用案例  以下是运行代码及结果  2.左外连接Left join 左外连接Left join关键字会从左表那里返回所有的行,即使是在右表中没有匹配到的行    下面是一个简单的案例

HDU 1005(矩阵乘法)

A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7. Given A, B, and n, you are to calculate the value of f(n). 题意:给出一个递推公式,求第N项。 题解:开始试图找出一个能够直接算的递

代码随想录Day 28|题目:122.买卖股票的最佳时机Ⅱ、55.跳跃游戏、45.跳跃游戏Ⅱ、1005.K次取反后最大化的数组和

提示:DDU,供自己复习使用。欢迎大家前来讨论~ 文章目录 题目题目一:122.买卖股票的最佳时机 II贪心算法:动态规划 题目二:55.跳跃游戏解题思路: 题目三: 45.跳跃游戏 II解题思路方法一方法二 题目四:1005.K次取反后最大化的数组和解题思路 总结 贪心算法继续刷题 题目 题目一:122.买卖股票的最佳时机 II 122. 买卖股票的最佳时机 II 贪心

Leetcode211: Populating Next Right Pointers in Each Node II

Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tree could be any binary tree? Would your previous solution still work? Note: You may only use constant e

1050 String Subtraction——PAT甲级

Given two strings S1​ and S2​, S=S1​−S2​ is defined to be the remaining string after taking all the characters in S2​ from S1​. Your task is simply to calculate S1​−S2​ for any given strings. However,