PAT A1002 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 polynomial:
K N​1 aN1​​ N2​ aN2… NK aNK

where K is the number of nonzero terms in the polynomial, N​i and aN​i(i=1,2,⋯,K) are the exponents and coefficients, respectively. It is given that 1≤K≤10,0≤NK<⋯<N2<N1≤1000.

Output Specification:

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

思路:

对相应指数的项的系数相加即可,a[i]表示指数为i的项的系数

参考题解:

#include<cstdio>
const int max_len=1001;
int main(){
	int k,m,count=0;
	double a[max_len],n;
	memset(a,0,sizeof(a));
	for(int i=0;i<2;i++){
		scanf("%d",&k);
		while(k--){
		scanf("%d%lf",&m,&n);
		a[m]+=n;
		}
	}
	for(int i=0;i<max_len;i++){
		if(a[i]!=0){
			count++;
		}
	} 
	printf("%d",count);
	for(int i=max_len-1;i>=0;i--){
		if(a[i]!=0){
			printf(" %d %.1f",i,a[i]);
		}
	}
	return 0;
}

后记

独揽月下萤火,照亮一纸寂寞;
追忆那些什么,你说的爱我;
花开后花又落 ,轮回也没结果;
苔上雪告诉我, 你没归来过。

《南山忆》——许嵩

全部评论

相关推荐

03-03 19:02
已编辑
东华理工大学 Node.js
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
正在热议
更多
# 春招至今,你的战绩如何? #
11108次浏览 95人参与
# 你的实习产出是真实的还是包装的? #
1960次浏览 42人参与
# MiniMax求职进展汇总 #
24125次浏览 309人参与
# 军工所铁饭碗 vs 互联网高薪资,你会选谁 #
7644次浏览 43人参与
# 简历第一个项目做什么 #
31750次浏览 341人参与
# 重来一次,我还会选择这个专业吗 #
433557次浏览 3926人参与
# 米连集团26产品管培生项目 #
6042次浏览 216人参与
# 当下环境,你会继续卷互联网,还是看其他行业机会 #
187217次浏览 1122人参与
# 牛客AI文生图 #
21452次浏览 238人参与
# 不考虑薪资和职业,你最想做什么工作呢? #
152461次浏览 888人参与
# 研究所笔面经互助 #
118967次浏览 577人参与
# 简历中的项目经历要怎么写? #
310376次浏览 4219人参与
# AI时代,哪些岗位最容易被淘汰 #
63853次浏览 828人参与
# 面试紧张时你会有什么表现? #
30516次浏览 188人参与
# 你今年的平均薪资是多少? #
213147次浏览 1039人参与
# 你怎么看待AI面试 #
180154次浏览 1258人参与
# 高学历就一定能找到好工作吗? #
64334次浏览 620人参与
# 你最满意的offer薪资是哪家公司? #
76547次浏览 374人参与
# 我的求职精神状态 #
448145次浏览 3129人参与
# 正在春招的你,也参与了去年秋招吗? #
363530次浏览 2638人参与
# 腾讯音乐求职进展汇总 #
160683次浏览 1112人参与
# 校招笔试 #
471238次浏览 2964人参与
牛客网
牛客网在线编程
牛客网题解
牛客企业服务