PAT甲级练习题(1) A+B Format Spell It Right (20)

原题: A+B Format (20)

[A+B Format Spell It Right (20

Calculate a + b and output the sum in standard format – that is, the digits must be separated into groups of three by commas (unless there are less than four digits).

输入描述:

Each input file contains one test case. Each case contains a pair of integers a and b where -1000000 <= a, b <= 1000000. The numbers are separated by a space.

输出描述:

For each test case, you should output the sum of a and b in one line. The sum must be written in the standard format.

示例1
输入

-1000000 9

输出

-999,991

翻译:

计算a + b并以标准格式输出总和-也就是说,必须用逗号将数字分成三组(除非少于四位)。

输入描述:

每个输入文件包含一个测试用例。 每个案例都包含一对整数a和b,其中-1000000 <= a,b <=1000000。数字之间用空格分隔。

输出描述:

对于每个测试用例,应在一行中输出a和b的总和。 总和必须以标准格式书写。

示例1
输入

-1000000 9

输出

-999,991

思路:

就是两数之和按照每三个数有个 , 的格式输出。

代码:


#include <iostream>
#include <string>
#include <cmath>
#include <stack>
using namespace std;
void init() {}
int main()
{
	int a = 0, b = 0,res=0;
	cin >> a >> b;
	res = a + b;
	int num = 0;
	int r = res;
	//while (r!=0)
	//{
	//	r = r / 10;
	//	num++;
	//}
	//cout << num << endl;
	stack<int > sta;
	bool falg = false;
	if (res >=1000 || res <= -1000)
	{
		if (1)
		{
			if (res < 0)
			{
				falg = true;
				res = -res;
			}
			int temp = 0;
			int n = 0;
			while (res!=0)
			{
				if (n%3==0&&n!=0)
				{
					sta.push(-1);
				}
				temp = res % 10;
				sta.push(temp);
				res = res / 10;
				n++;
			}
			while (!sta.empty())
			{
				int  a = sta.top();
				if (falg==true)
				{
					cout << "-";
					falg = false;
				}
			    if (a==-1)
				{
					cout << ",";
					sta.pop();
				}
				else
				{
					cout << a;
					sta.pop();
				}
			}
			cout << endl;
		}
		else
		{
			

		}
	}
	else
	{
		if (res < 0)
		{
			cout << "-"<<res<<endl;
		}
		else
			cout << res << endl;
	}
	return 0;
}

原题: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 every digit of the sum in English.

输入描述:

Each input file contains one test case. Each case occupies one line which contains an N (<= 10100).

输出描述:

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.

输入例子:

12345

输出例子:

one five

翻译:

译文描述

给定一个非负整数N,您的任务是计算N的所有数字的总和,并以英语输出总和的每个数字。

输入描述:

每个输入文件包含一个测试用例。 每个案例占用一行包含N(<= 10^100)的行。

输出描述:

对于每个测试用例,用英语单词在一行中输出总和的数字。 两个连续的单词之间必须有一个空格,但在行尾不能有多余的空格。

输入例子:

12345

输出示例:

一五

思路:

数字非常大,直接字符串,然后相加,取每一位数,然后放到栈里,逆序输出

代码:


#include <iostream>
#include <string>
#include <cmath>
#include <stack>
using namespace std;
void init() {}
string a[10] = { "zero","one","two","three","four","five","six","seven","eight","nine" };
int main()
{
	int n;
	string s;
	cin >> s;
	int res = 0;
	for (int i = 0; i < s.length(); i++)
	{
		res = res + (int)(s[i] - '0');
	}
	//cout << res<<endl;
	stack<int> sta ;
	int temp = 0 ;
	while (res!=0)
	{
		temp = res % 10;
		sta.push(temp);
		res = res/10;
	}
	int size = sta.size();
	//cout <<size<< endl;
	//while (!sta.empty())
	//{
	//	int a = sta.top();
	//	cout <<a << endl;
	//	sta.pop();
	//}
	int index = 0 ;
	while (size--)
	{
		index = sta.top();
		//cout << index << endl;
		if (size==0)
		{
			cout << a[index]<<endl;
			sta.pop();
		}
		else
		{
			cout << a[index]<<" ";
			sta.pop();
		}
	}
	return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值