题目链接:https://www.patest.cn/contests/pat-a-practise/1002

原题如下:

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
__________________________________________________________________________________________________________________________________________________
  这道题我感觉就是一元多项式的加法,因此用了之前文章中的方法,集用两个链表分别存储两行元素,每次比较然后相加,可是有三个测试点超时了……代码如下:
 #include<stdio.h>
#include<stdlib.h> typedef struct Node{
struct Node *Next;
int expon;
float coef;
}PNode; void Insert(PNode *P,PNode **PtrRear)
{
//printf("er");
PNode *tmp=(PNode *)malloc(sizeof(struct Node));
tmp->expon=P->expon;tmp->coef=P->coef;tmp->Next=NULL;
(*PtrRear)->Next=tmp;
*PtrRear=tmp;
//printf("sd");
return ;
} PNode * ReadP(int K)
{
int i,e;
float c;
PNode *P1=(PNode *)malloc(sizeof (struct Node)); P1->Next=NULL;
PNode *tmp=(PNode *)malloc(sizeof (struct Node));tmp->Next=NULL;
tmp=P1;
for (i=;i<K;i++)
{
scanf("%d %f",&e, &c);
PNode *P=(PNode*)malloc(sizeof (struct Node));
P->expon =e;P->coef=c;P->Next=NULL;
P1->Next=P;
P1=P;
}
tmp=tmp->Next; return tmp;
} int main()
{
int K1,K2,i;
PNode *rear,*front,*tmp;
rear=(PNode *)malloc(sizeof (struct Node));front=rear;
PNode *P1=(PNode *)malloc(sizeof (struct Node));
PNode *P2=(PNode *)malloc(sizeof (struct Node)); scanf("%d",&K1);P1=ReadP(K1);
scanf("%d",&K2);P2=ReadP(K2); //printf("\n%d %.1lf %d %.1lf",P1->expon,P1->coef,P1->Next->expon,P1->Next->coef); printf("\n%d %.1lf %d %.1lf\n",P2->expon,P2->coef,P2->Next->expon,P2->Next->coef); int cnt=;
while (P1 && P2)
{
if (P1->expon>P2->expon)
{
Insert(P1,&rear);
P1=P1->Next;
cnt++;
}
else if (P1->expon<P2->expon)
{
Insert(P2,&rear);
P2=P2->Next;
cnt++;
}
else if (P1->expon==P2->expon)
{
if (P1->coef+P2->coef)
{
PNode *tmp=(PNode *)malloc(sizeof (struct Node));
tmp->expon=P1->expon;tmp->coef=P1->coef+P2->coef;
tmp->Next=NULL;
Insert(tmp,&rear);
P1=P1->Next;P2=P2->Next;
cnt++;
}
}
}
while (P1){Insert(P1,&rear);P1=P1->Next;cnt++;}
while (P2){Insert(P2,&rear);P2=P2->Next;cnt++;} int flag=;
tmp=front;
front=front->Next;
while (front)
{
if (!flag)
{printf("%d %d %.1f",cnt,front->expon,front->coef);front=front->Next;flag=;}
else
{
printf(" %d %.1f",front->expon,front->coef);front=front->Next;
}
}
free(tmp);
return ;
}

希望哪位朋友能帮忙看下问题在哪……

在网上看了其他人的代码,真是简单精妙啊,直接开一个数组,指数即为数值下标,每个数组的值即为相应的每个指数对应系数的值,代码如下:

#include<stdio.h>
#define MaxN 1001 int main()
{
int K,i,e;
int line=;
int cnt=;
int Maxe=;
double Input[MaxN]={};
double c;
while (line)
{scanf("%d",&K);
for (i=;i<K;i++)
{
scanf("%d %lf",&e,&c);
Input[e]+=c;
if (e>Maxe)Maxe=e;
}
line--;
} for (i=Maxe;i>=;i--)
{
if (Input[i]!=)cnt++;
} printf("%d",cnt);
for (i=Maxe;i>=;i--)
{
if (Input[i]!=)printf(" %d %.1lf",i,Input[i]);
}
return ;
}

1002. A+B for Polynomials (25)的更多相关文章

  1. PAT 1002. A+B for Polynomials (25) 简单模拟

    1002. A+B for Polynomials (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue T ...

  2. PAT甲 1002. A+B for Polynomials (25) 2016-09-09 22:50 64人阅读 评论(0) 收藏

    1002. A+B for Polynomials (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue T ...

  3. 1002 A+B for Polynomials (25)(25 point(s))

    problem 1002 A+B for Polynomials (25)(25 point(s)) This time, you are supposed to find A+B where A a ...

  4. 【PAT】1002. A+B for Polynomials (25)

    1002. A+B for Polynomials (25) This time, you are supposed to find A+B where A and B are two polynom ...

  5. PAT甲级 1002 A+B for Polynomials (25)(25 分)

    1002 A+B for Polynomials (25)(25 分) This time, you are supposed to find A+B where A and B are two po ...

  6. PAT 甲级1002 A+B for Polynomials (25)

    1002. A+B for Polynomials (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue T ...

  7. PAT (Advanced Level) Practice 1002 A+B for Polynomials (25 分) 凌宸1642

    PAT (Advanced Level) Practice 1002 A+B for Polynomials (25 分) 凌宸1642 题目描述: This time, you are suppos ...

  8. 甲级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 fi ...

  9. PAT 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 con ...

随机推荐

  1. Asp.net 解决下载乱码问题,支持火狐、IE、谷歌等主流浏览器

    public static void DownFileStream(MemoryStream ms, string fileName) { if (ms !=Stream.Null) { ) { fi ...

  2. mysql workbench连接不上远程数据库,xshell无法连接远程主机的问题

    1.先说xshell无法连接的问题 最近使用virtualbox装了个ubuntu-16.04,然后在win7上使用xshell连接,首先确认win7能ping通虚拟机ip.然后确认是否安装了open ...

  3. oracleDBA-D3

    1.静态参数文件 PFILE:使用操作系统提供的VIM进行编辑,包含多个参数文件 参数文件名是:init.ora(如: D:\app\Administrator\admin\orcl\pfile) 其 ...

  4. OC中的__attribute__的使用

    简介: 在IOS9.2官方文档中Attributes的描述如下,简单明了: Attributes provide more information about a declaration or typ ...

  5. vs2016x64&&qt5.7.1编译osg3.4.0&&osgEarth2.7

    此文仅备忘: 1.安装VS2013_Cn_Ult 2.安装qt-opensource-windows-x86-msvc2013_64-5.7.1 设置环境变量QTDIR,并将其bin加入到path中. ...

  6. WinForm任务栏最小化

    在C#编写的WinForm里,在FormBorderStyle设为None的时候,任务栏点击程序图标,不会自动最小化.在主窗口WinForm.cs里加入如下代码后,即可恢复该功能. protected ...

  7. 两种方式testng dataprovider结合csv做测试驱动

    方式一: 第一.读取csv数据源码 import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream ...

  8. 【JavaScript】ES6 新语法

    function* 声明 function* 声明(function关键字后跟一个星号)定义一个generator(生成器)函数,返回一个Generator对象. 生成器是一种可以从中退出并在之后重新 ...

  9. 【Java EE 学习 75 上】【数据采集系统第七天】【二进制运算实现权限管理】【权限分析和设计】

    一.权限计算相关分析 1.如何存储权限 首先说一下权限保存的问题,一个系统中最多有多少权限呢?一个大的系统中可能有成百上千个权限需要管理.怎么保存这么多的权限?首先,我们使用一个数字中的一位保存一种权 ...

  10. UVA 11768 Lattice Point or Not(扩展欧几里德)

    将直线转化为ax + by = c的形式,然后扩展欧几里得求在[x1, x2]之间的解 对直线与坐标轴平行的特判 调试了好长时间,注意: 1 正负数转化为整型的处理 2 注意判断有无解 #includ ...