1. 程式人生 > 實用技巧 >PAT1002 A+B for Polynomials

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