栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

PTA甲级-1002 A+B for Polynomials c++

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

PTA甲级-1002 A+B for Polynomials c++

文章目录
  • 题目内容
      • Input Specification:
      • Output Specification:
      • Sample Input:
      • Sample Output:
  • 一、题干大意
  • 二、题解要点
  • 三、具体实现
  • 总结

题目内容

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 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<⋯ 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

一、题干大意

这道题的意思就是说给定两组数,输出它们的和。给定的输入格式是:

1.这组数有几个项 2.指数 3.系数 2.指数 3.系数 。。。。

二、题解要点
  • 总体思路就是把指数当作下标。相同指数的系数累加。
  • 输出的时候只要是系数不为0的都进行输出。

三、具体实现
#include
using namespace std;

int main(){
	float xishu[1001]={0};
	int n1,n2;
	
		
	cin>>n1;
	for(int i=0;i
		int tempZhishu;
		float tempXishu;
		cin>>tempZhishu>>tempXishu;
		xishu[tempZhishu] += tempXishu;//用指数做下标 在数组中累加系数 
		
	}
	cin>>n2;
	for(int i=0;i
		int tempZhishu;
		float tempXishu;
		cin>>tempZhishu>>tempXishu;
		xishu[tempZhishu] += tempXishu;
		
	}
	
	
	int count=0;
	for(int i=0;i<=1000;i++){
		if(xishu[i]!=0){
			count++;
		}
	}
	cout<=0;i--)
    {
        if(xishu[i]!=0)
            printf(" %d %.1f",i,xishu[i]);
    }
	
	return 0;
}

总结

最后输出的时候一定要把所有系数不为0的全都进行输出,不能按照count的值进行循环输出,因为count记录的是一共有几个不为0的项,而这些项中间可能会有系数为0的项。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/993958.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号