PAT 1002 a+b for polynomials 翻譯 分析 代碼

題目:1002 a+b for polynomials (25)(25 分)【多項式的a+b形式

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

【計算兩個多項式A+B的和】

Input

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~ ... NK a~NK~, where K is the number of nonzero terms in the polynomial, Ni and a~Ni~ (i=1, 2, ..., K) are the exponents and coefficients, respectively. It is given that 1 <= K <= 10,0 <= NK < ... < N2 < N1 <=1000.

【每個輸入文件都包括一個測試例。每個例子包括兩行,每行都包括一個多項式的信息:K N1 a1 N2 a2 ……Ni ai,K是多項式裏面有K個項,其中1<=K<=10,冪次N最大爲1000】

Output

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.

【對每個測試案例你都應該輸出A+B的和,並且以同樣的格式輸入樣例,注意!在每行的結尾都要求不準出現“空格”.精確到小數點後1位,輸出以冪次由大到小輸出

【注】:以下分析所在爲代碼註釋,通過註釋可以分析得到此題答案。

【注】:此代碼是由Dev c++ 5.11 所寫,運用c++語言。  其他方法例如c++,後續會另寫微博給出方式。驚訝

/****************************************************************************************************************************/

#include<cstdio>

int main()
{
	int m=1111,k,count=0,i,n;		//由題意得,0<N<=1000,所以m必須大於1000 
	double a;				//係數a必須是double型 
	double p[m]={};
	
		scanf("%d",&k);		        //第一行輸入具體數值 ; 
		for(i=0;i<k;i++)
    	    {
		scanf("%d%lf",&n,&a);
		p[n]=a;				//把係數A存入P[]數組 ; 
	    }
		
		scanf("%d",&k);		        //第二行輸入具體數值 ; 
		for(i=0;i<k;i++)
	    {
		scanf("%d%lf",&n,&a);
		p[n]+=a;			//冪次相同的二項式 使其係數相加; 
	    }
		
		    for(i=0;i<m;i++)
		    {
			if(p[i]!=0)
			{
				count++;	//t計數,係數相加後,如果不等於零,則爲確實存在的二項式,記錄共有多少二項式; 
			}
		    }
		
		printf("%d",count);
	        while(m--)
	    {
		if(p[m]!=0)			//輸出係數不等於0的切實存在的二項式; 
		printf(" %d %.1f",m,p[m]);	//倒序輸出,輸出冪次n和係數a,a保留一位小數; 注意輸出格式; 
	     } 
	return 0;
}//唱晚,愛你喲~ 2018年7月3日10:56:17 

/******************************************************************************************************************/

測試點

2 1 2.4 0 3.2
2 2 1.5 1 0.5

測試結果

3 2 1.5 1 2.9 0 3.2


發佈了36 篇原創文章 · 獲贊 7 · 訪問量 4959
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章