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;
}
发布了96 篇原创文章 · 获赞 11 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_41852212/article/details/102678021
今日推荐