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;
}

  

时间: 2024-07-30 00:22:56

PAT 1002. A+B for Polynomials的相关文章

1002. A+B for Polynomials (25)——PAT (Advanced Level) Practise

题目信息: 1002. A+B for Polynomials (25) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue This time, you are supposed to find A+B where A and B are two polynomials. Input Each input file contains one test case. Each case occupies 2 lin

PAT 甲级1002 A+B for Polynomials (25)

1002. A+B for Polynomials (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue This time, you are supposed to find A+B where A and B are two polynomials. Input Each input file contains one test case. Each case occupies 2 lines, an

1002. A+B for Polynomials (25)

题目: 1002. A+B for Polynomials (25) #include<stdio.h> #define N 1000 int main() { double poly[N+1]={0}; int n,m,i; int exp; double coef; scanf("%d",&n); m=n; for(i=0;i<n;i++) { scanf(" %d %lf",&exp,&coef); poly[exp]

1002. A+B for Polynomials

1002. A+B for Polynomials (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue This time, you are supposed to find A+B where A and B are two polynomials. Input Each input file contains one test case. Each case occupies 2 lines, an

1002 A+B for Polynomials (25)(25 point(s))

problem 1002 A+B for Polynomials (25)(25 point(s)) This time, you are supposed to find A+B where A and B are two polynomials. Input Each input file contains one test case. Each case occupies 2 lines, and each line contains the information of a polyno

PAT A1002 A+B for Polynomials

PAT A1002 A+B for Polynomials 题目描述: 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 polyno

1002 A+B for Polynomials (PAT (Advanced Level) Practice)

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 N?1?? a?N?1???? N?2?? a?N?2?

PAT Advanced 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 N?1?? a?N?1???? N?2?? a?N?2?

PAT:1002. A+B for Polynomials (25) 部分错误

#include<stdio.h> #include<stdlib.h> #include<string.h> //[warning]double 输入%lf,输出%f struct arr { int tag; double data; }arr[1005]; int main() { memset(arr,0,sizeof(arr)); int t1,t2,tmp,maxI=0; //maxI记录最多多少项 double tdata; for(int I=0 ; I