题目介绍
代码思路
这是一个简单的多项式相加然后按规定格式输出的一个题目,我们可以利用vector向量来实现,指数就是数组下标,系数就是下标对应的元素。
代码实现
#include<iostream>
#include<cstdio>
#include<vector>
#include<cstring>
#include<sstream>
#include<algorithm>
using namespace std;
int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("1.txt", "r", stdin);
#endif
vector<float> v(1001);
int a;
int b;
float num;
cin >> a;
for(int i=0;i<a;++i)
{
cin>>b>>num;
v[b]+=num;
}
cin>>a;
for(int i=0;i<a;++i)
{
cin>>b>>num;
v[b]+=num;
}
int count=0;
for(int i=0;i<1001;i++)
{
if(v[i]!=0)
count++;
}
cout<<count;
for(int i=1000;i>=0;i--)
{
if(v[i]!=0)
printf(" %d %.1f", i, v[i]);
}
return 0;
}
Comments | 2 条评论
博客作者 Denise Menken
The article is well-written and provides practical advice for readers. Thanks for sharing your insights with us.
博客作者 Dot Dorosky
We loved reading your post from start to finish. You have kept us interested. Thanks for sharing your thoughts.