码迷,mamicode.com
首页 > 其他好文 > 详细

PAT 1002. A+B for Polynomials

时间:2017-01-21 07:42:45      阅读:195      评论:0      收藏:0      [点我收藏+]

标签:double   print   als   class   scanf   exp   pre   can   scan   

#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

标签:double   print   als   class   scanf   exp   pre   can   scan   

原文地址:http://www.cnblogs.com/Edward-zy/p/6336405.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!