PAT1002 A+B for Polynomials

题目描述

1002 A+B for Polynomials (25分)

This time, you are supposed to find A+B where A and B are two polynomials.

Input Specification:

Each input file contains one test case. Each case occupies 2 lines, and each line contains the information of a polynomial:

K N1 a**N1 N2 a**N2 ... N**K aNK

where K is the number of nonzero terms in the polynomial, N**i and aNi (i=1,2,⋯,K) are the exponents and coefficients, respectively. It is given that 1≤K≤10,0≤N**K<⋯<N2<N1≤1000.

Output Specification:

For each test case you should output the sum of A and B in one line, with the same format as the input. Notice that there must be NO extra space at the end of each line. Please be accurate to 1 decimal place.

Sample Input:

2 1 2.4 0 3.2
2 2 1.5 1 0.5

Sample Output:

3 2 1.5 1 2.9 0 3.2

作者

CHEN, Yue

单位

浙江大学

代码长度限制

16 KB

时间限制

400 ms

内存限制

64 MB

题目分析

整体分析:

根据题目可知每一行要输入的第一个数据就是你要输入的数据的个数,即第一个数据是一个整型,且输入的值在1-9之间,即每行最少输入一个数据(这里的一个数据是指一个整型+一个浮点型),最多可输入九个数据。要输入的数据(整型+浮点型)之中的整型范围在0-1000。输出时也有格式要求,题目要求必须在同一行输出,从所给的例子中也不难看出输出时数据之间要加空格处理,并且要注意当输出最后一个数据时,后面是不能跟空格的,否则会出现格式错误。

如何进行输出?

考虑使用数组进行输出,要输入的数据(整型+浮点型)之中的整型可以视为数组下标,同时将要输入的数据(整型+浮点型)之中的浮点型存储在数组中,这样通过第一行和第二行的输入可以得到两个数组,然后再创建一个数组用来存储这两个数组进行相加之后的结果,用来进行输出数据。

题目解答

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
	//先设置数组a和b,用来存储输入的数据
	double arrayA[1001];
	double arrayB[1001];

	//数据c用来存储相加的数据
	double arrayOut[1001];

	//初始化数组
	for (int i = 0; i < 1001; i++)
	{
		arrayA[i] = 0;
	}
	for (int i = 0; i < 1001; i++)
	{
		arrayB[i] = 0;
	}
	for (int i = 0; i < 1001; i++)
	{
		arrayOut[i] = 0;
	}

	//开始输入第一行
	int ret;//用来记录每一行的第一个数据,即记录要输入的数据个数
	int ref;//第二行第一个数据,同样是要输入的数据个数
	int digit;//数据下标
	double data;//输入的数据
	int num = 0;//用来记录输出数组中非零数据的个数

	//第一行进行输入
	cin >> ret;
	for (int i = 0; i < ret; i++)
	{
		cin >> digit;
		cin >> data;
		arrayA[digit] = data;
	}

	//第二行进行输入
	cin >> ref;
	for (int i = 0; i < ref; i++)
	{
		cin >> digit;
		cin >> data;
		arrayB[digit] = data;
	}

	//数组值进行相加操作
	for (int i = 0; i < 1001; i++)
	{
		arrayOut[i] = arrayA[i] + arrayB[i];
	}

	//计算输出数组中非零元素个数
	for (int i = 0; i < 1001; i++)
	{
		if (arrayOut[i] != 0)
		{
			num++;
		}
	}
	//开始输出,注意格式要求
	cout << num ;

	for (int i = 1000; i >= 0; i--)
	{
		if (arrayOut[i] != 0)
		{
			//先输出数组下标
			cout << " " << i;
			//再输出该数组下标中的数据元素
			//setprecision(1)代表着指定输出一位小数
			cout.setf(ios::fixed);
			cout << " " << setprecision(1) << arrayOut[i];
		}
	}

	system("pause");

	return 0;
}

检测结果:

思考总结:

在C++的编程中,总会遇到浮点数的处理,有的时候,我们只需要保留2位小数作为输出的结果,这时候,问题来了,怎样才能让cout输出指定的小数点后保留位数呢?

在C语言的编程中,我们可以这样实现它:

printf("%.2f", sample);

在C++中,是没有格式符的,我们可以通过使用setprecision()函数来实现这个需求。
想要使用setprecision()函数,必须包含头文件#include 。使用方式如下:

cout << "a=" << setprecision(2) << a <<endl;

这时候,我们会发现,如果a的值为0.20001,输出的结果为a=0.2,后面第二位的0被省略了。
如果我们想要让它自动补0,需要在cout之前进行补0的定义。代码如下:

cout.setf(ios::fixed);

cout << "a=" <<fixed<< setprecision(2) << a <<endl; //输出a=0.20

这样,我们就可以得到0.20了。当然,如果想要关闭掉补0,只需要对fixed进行取消设置操作。

cout.unsetf(ios::fixed);

cout << "a=" << setprecision(2) << a <<endl; //输出a=0.2

参考代码:

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
    float a = 0.20001;

    cout.setf(ios::fixed);

    cout << "a=" <<fixed<< setprecision(2) << a <<endl; //输出结果为a=0.20

    cout.unsetf(ios::fixed);

    cout << "a=" << setprecision(2) << a <<endl; //输出结果为a=0.2
    
    system("pause");

    return 0;

}

double a = 17;
int b = 3;

cout.setf(ios::fixed);
cout << setprecision(1) << a / b;

输出:5.7

double a = 17;
int b = 3;
cout << setprecision(1) << a / b;

输出:6

double a = 17;
int b = 3;

cout.setf(ios::fixed);
cout << setprecision(2) << a / b;

输出:5.67

double a = 17;
int b = 3;
cout << setprecision(2) << a / b;

输出:5.7

吾生也有涯,而知也无涯
原文地址:https://www.cnblogs.com/daimasanjiaomao/p/13798296.html