您的位置:首页 > 其它

PAT_1002

2018-03-28 10:39 162 查看


1002. A+B for Polynomials (25)

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 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 < ... < N2 < N1 <=1000.

Output

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:输出按照输出格式进行输出-----从小到大的顺序输出(以Ni系数为基准排序)

2:输出的精度要求为1位小数,并且0必须是0;

3:如果两个多项式相互抵消了则不输出,而不是输出 ni 0;

贴一段我的错误代码:

#include<iostream>
#include<list>
#include<cstdio>
#include<iomanip>
using namespace std;

struct Polynomial
{
float Coefficient;
float exponent;                   //使用了一个多项式结构体
};

int main()
{
Polynomial a[25],b[11];
Polynomial temp;
int c,d,ans=0;
freopen("in.txt","r",stdin);
cin>>c;
for(int i=0;i<c;i++){
cin>>a[i].exponent;
cin>>a[i].Coefficient;
}
c--;
/*for(int i=0;i<c;i++){
cout<<a[i].exponent<<" ";
cout<<a[i].Coefficient<<endl;
}*/
getchar();
cin>>d;
for(int i=0;i<d;i++){

cin>>b[i].exponent;
cin>>b[i].Coefficient;
}

for(int i=0;i<c;i++){
for(int j=ans;j<d;j++)
{
if(a[i].exponent==b[j].exponent)
{
a[i].Coefficient+=b[j].Coefficient;            //进行了O(n²)相加
ans=j+1;
continue;
}
else if((a[i].exponent)<(b[j].exponent)){
c=c+1;
a[c].Coefficient=b[j].Coefficient;
a[c].exponent=b[j].exponent;
ans=j+1;
}

}
}
//cout<<(a[c].Coefficient)<<endl;
/*for(int i=0;i<c;i++)
{
for(int j=0;j<c-i;j++){
if((a[j].exponent)<(a[j+1].exponent))
{
//cout<<"出现一次"<<endl;
temp.Coefficient=a[j].Coefficient;       //完全可以用sort()进行排序,省去冗长的代码
temp.exponent=a[j].exponent;
a[j].Coefficient=a[j+1].Coefficient;
a[j].exponent=a[j+1].exponent;
a[j+1].Coefficient=temp.Coefficient;
a[j+1].exponent=temp.exponent;
}
}
}*/
cout<<c+1<<" ";
for(int i=0;i<=c;i++){
if((a[i].Coefficient==0))
{
cout.unsetf(ios::fixed);
cout<<(a[i].exponent)<<" ";              //用了麻烦的输出格式,printf更有优势于输出的数位控制
cout.setf(ios::fixed);
cout<<"0"<<" ";
}
else{
cout.unsetf(ios::fixed);
cout<<(a[i].exponent)<<" ";
cout.setf(ios::fixed);
cout<<setprecision(1)<<(a[i].Coefficient)<<" ";
}
}
return 0;



然后浏览了其他大佬的代码,感觉自己真的想岔了,完全可以用一个1000位数组存储位数的出现,在以一个数组记录系数。

这是其他帖子正确的代码:

# include <iostream>
# include <string>
# include <sstream>
# include <vector>
# include <algorithm>
#include  <cstdio>
# include <functional>
# include <iomanip>
# include <ctime>
# include <map>
# include <math.h>
# include <string.h>

using namespace std;

bool com1(int a, int b)
{
return a >= b;
}

int main(void)
{
int K1, K2;
int numOfX;
double num;

int i = 0;
int cnt;                    //出现过的指数个数
int aCnt;                    //实际输出个数

double numX[1001];            //指数范围[0,1000],所以用该数组记录
int headX[20];                //记录出现过的指数

memset(numX, 0, 1001*sizeof(double));

cin >> K1;
for (i = 0; i < K1; i++)
{
cin >> numOfX >> num;
headX[i] = numOfX;
numX[numOfX] += num;
}

cnt = i;
aCnt = i;
cin >> K2;
for (i = 0; i < K2; i++)
{
cin >> numOfX >> num;
if (numX[numOfX] == 0)            //若该数为0,说明该指数没有出现过
{
headX[cnt++] = numOfX;
aCnt++;
}
numX[numOfX] += num;            //计算过后,若该数为0,则该指数不会被输出
if (numX[numOfX] == 0)
aCnt--;
}

sort(headX, headX+cnt, com1);        //对出现过的指数排列
//    qSort(headX, 0, cnt - 1, com1);

/*                                    //原本认为当两式子抵消之后,输出会是
if (aCnt == 0)
7127
//这个样子的,但实际上,只需要输出aCnt就可以了。
cout << "0 0 0.0" << endl;        //这样输出最后1个测试点不能通过。
else
*/
{
cout << aCnt;
for (i = 0; i < cnt; i++)
{
if (numX[headX[i]] != 0)
printf(" %d %.1lf", headX[i], numX[headX[i]]);
}
cout << endl;
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  PAT 1002