1. 程式人生 > >PAT 甲級 1002 A+B for Polynomials

PAT 甲級 1002 A+B for Polynomials

Description

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

Input Each input file contains multiple test case. Each case occupies 2 lines, and each line contains the information of a polynomial: K N1 aN1 N2 aN2 … NK aNK, where K is the number of nonzero terms in the polynomial, Ni and aNi (i=1, 2, …, K) are the exponents and coefficients, respectively. It is given that 1 <= K <= 10,0 <= NK < … < N2 < N1 <=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.

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

我的解法,有一些小錯誤,一旦係數小於0.05保留一位小數程式設計了0。

#include <stdio.h>
struct dxs{
	int mi;
	double xishu;
};
int main(){
	int n1;
	while(scanf("%d",&n1)==1){
		struct dxs s1[n1];
		int i=0;
		for(i=0;i<n1;i++){
			scanf("%d %lf",&s1[i].mi,&s1[i].xishu);
		}
		int n2;
		scanf("%d",&n2);
		struct dxs s2[n2];
		for(i=0;i<n2;i++){
			scanf("%d %lf",&s2[i].mi,&s2[i].xishu);
		}
		int n3=n1+n2;
		struct dxs s3[n3]; 
		int xiangshu=0;
		int j=0;
		int kz=0;
		for(i=0;i<n1;i++){
			for(j=0;j<n2;j++){
				if(s1[i].mi==s2[j].mi){
					s3[xiangshu].mi=s1[i].mi;
					s3[xiangshu].xishu=s1[i].xishu+s2[j].xishu;
					xiangshu++;
					kz=1;
				}
			}
			if(kz==0){
				s3[xiangshu].mi=s1[i].mi;
				s3[xiangshu].xishu=s1[i].xishu;
				xiangshu++;
			}
			else kz=0;
		}
		int temp=xiangshu;
		for(j=0;j<n2;j++){
			for(i=0;i<temp;i++){
				if(s2[j].mi==s3[i].mi){
					kz=1;
				}
			}
			if(kz==0){
				s3[xiangshu].mi=s2[j].mi;
				s3[xiangshu].xishu=s2[j].xishu;
				xiangshu++;
			}
			else kz=0;
		}
		for(j=0;j<xiangshu;j++){
			for(i=0;i<xiangshu-j-1;i++){
				if(s3[i].mi<s3[i+1].mi){
					double t1=s3[i+1].xishu;
					int t2=s3[i+1].mi;
					s3[i+1].mi=s3[i].mi;
					s3[i+1].xishu=s3[i].xishu;
					s3[i].mi=t2;
					s3[i].xishu=t1;
				}
			}
		}
		printf("%d ",xiangshu);
		for(i=0;i<xiangshu-1;i++){
			printf("%d %.1f ",s3[i].mi,s3[i].xishu);
		}
		printf("%d %.1f\n",s3[xiangshu-1].mi,s3[xiangshu-1].xishu);
	}
	return 0;
} 

同學用C++的解法,模擬法

#include <iostream>
#include <cstdio>
using namespace std;
#define MAX 1001

int main()
{
    int k1,k2;
    while(cin>>k1)
    {
      double a[MAX]={0},b[MAX]={0},c[MAX]={0};
      double xishu;
      int mi;
      for(int i=0;i<k1;i++)
      {
          cin>>mi>>xishu;
          a[mi]=xishu;
      }
      cin>>k2;
       for(int i=0;i<k2;i++)
      {
          cin>>mi>>xishu;
          b[mi]=xishu;
      }
      int flag=0;
      for(int i=0;i<=1000;i++)
      {
         if(a[i]+b[i]>=0.05)
         {
             c[i]=a[i]+b[i];
             flag++;
         }
      }
      cout<<flag<<" ";
      for(int i=1000;i>=0;i--)
      {
          if(c[i]>=0.5)
          {
              if(flag==1)
               {
                   printf("%d %.1lf",i,c[i]);
               }
              else
              {
                   printf("%d %.1lf ",i,c[i]);
               }
              flag--;
          }
      }
    cout<<endl;
    }
    return 0;
}