首页 > 代码库 > PAT 1002. A+B for Polynomials

PAT 1002. A+B for Polynomials

#include<stdio.h>
#include<string.h>
double coefficient[1010];
int k1, k2,k3=0;
int nk;
int max_exponent1, max_exponent2;
int min_exponent1, min_exponent2;
int main(){
		memset(coefficient, 0, sizeof coefficient);
		scanf("%d", &k1);
		for (int i = 0; i < k1; i++){
			scanf("%d", &nk);
			scanf("%lf", &coefficient[nk]);
			if (i == 0)max_exponent1 = nk;
			if (i == k1 - 1)min_exponent1 = nk;
		}
		scanf("%d", &k2);
		double coeff;
		for (int i = 0; i < k2; i++){
			scanf("%d %lf", &nk, &coeff);
			coefficient[nk] += coeff;
			if (i == 0)max_exponent2 = nk;
			if (i == k2 - 1)min_exponent2 = nk;
		}
		int min_exponent = min_exponent1<min_exponent2 ? min_exponent1 : min_exponent2;
		int max_exponent = max_exponent1 > max_exponent2 ? max_exponent1 : max_exponent2;
		for (int i = max_exponent; i >= min_exponent; i--){
			if (coefficient[i] == 0)continue;
			k3++;
		}
		printf("%d", k3);
		for (int i = max_exponent; i >= min_exponent; i--){
			if (coefficient[i] == 0){
				continue;
			}
			printf(" %d %.1lf", i, coefficient[i]);
		}
	return 0;
}

  

PAT 1002. A+B for Polynomials