题目链接: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

  1. 2 1 2.4 0 3.2
  2. 2 2 1.5 1 0.5

Sample Output

  1. 3 2 1.5 1 2.9 0 3.2
    __________________________________________________________________________________________________________________________________________________
      这道题我感觉就是一元多项式的加法,因此用了之前文章中的方法,集用两个链表分别存储两行元素,每次比较然后相加,可是有三个测试点超时了……代码如下:
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. typedef struct Node{
  5. struct Node *Next;
  6. int expon;
  7. float coef;
  8. }PNode;
  9.  
  10. void Insert(PNode *P,PNode **PtrRear)
  11. {
  12. //printf("er");
  13. PNode *tmp=(PNode *)malloc(sizeof(struct Node));
  14. tmp->expon=P->expon;tmp->coef=P->coef;tmp->Next=NULL;
  15. (*PtrRear)->Next=tmp;
  16. *PtrRear=tmp;
  17. //printf("sd");
  18. return ;
  19. }
  20.  
  21. PNode * ReadP(int K)
  22. {
  23. int i,e;
  24. float c;
  25. PNode *P1=(PNode *)malloc(sizeof (struct Node)); P1->Next=NULL;
  26. PNode *tmp=(PNode *)malloc(sizeof (struct Node));tmp->Next=NULL;
  27. tmp=P1;
  28. for (i=;i<K;i++)
  29. {
  30. scanf("%d %f",&e, &c);
  31. PNode *P=(PNode*)malloc(sizeof (struct Node));
  32. P->expon =e;P->coef=c;P->Next=NULL;
  33. P1->Next=P;
  34. P1=P;
  35. }
  36. tmp=tmp->Next;
  37.  
  38. return tmp;
  39. }
  40.  
  41. int main()
  42. {
  43. int K1,K2,i;
  44. PNode *rear,*front,*tmp;
  45. rear=(PNode *)malloc(sizeof (struct Node));front=rear;
  46. PNode *P1=(PNode *)malloc(sizeof (struct Node));
  47. PNode *P2=(PNode *)malloc(sizeof (struct Node));
  48.  
  49. scanf("%d",&K1);P1=ReadP(K1);
  50. 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);
  51.  
  52. int cnt=;
  53. while (P1 && P2)
  54. {
  55. if (P1->expon>P2->expon)
  56. {
  57. Insert(P1,&rear);
  58. P1=P1->Next;
  59. cnt++;
  60. }
  61. else if (P1->expon<P2->expon)
  62. {
  63. Insert(P2,&rear);
  64. P2=P2->Next;
  65. cnt++;
  66. }
  67. else if (P1->expon==P2->expon)
  68. {
  69. if (P1->coef+P2->coef)
  70. {
  71. PNode *tmp=(PNode *)malloc(sizeof (struct Node));
  72. tmp->expon=P1->expon;tmp->coef=P1->coef+P2->coef;
  73. tmp->Next=NULL;
  74. Insert(tmp,&rear);
  75. P1=P1->Next;P2=P2->Next;
  76. cnt++;
  77. }
  78. }
  79. }
  80. while (P1){Insert(P1,&rear);P1=P1->Next;cnt++;}
  81. while (P2){Insert(P2,&rear);P2=P2->Next;cnt++;}
  82.  
  83. int flag=;
  84. tmp=front;
  85. front=front->Next;
  86. while (front)
  87. {
  88. if (!flag)
  89. {printf("%d %d %.1f",cnt,front->expon,front->coef);front=front->Next;flag=;}
  90. else
  91. {
  92. printf(" %d %.1f",front->expon,front->coef);front=front->Next;
  93. }
  94. }
  95. free(tmp);
  96. return ;
  97. }

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

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

  1. #include<stdio.h>
  2. #define MaxN 1001
  3.  
  4. int main()
  5. {
  6. int K,i,e;
  7. int line=;
  8. int cnt=;
  9. int Maxe=;
  10. double Input[MaxN]={};
  11. double c;
  12. while (line)
  13. {scanf("%d",&K);
  14. for (i=;i<K;i++)
  15. {
  16. scanf("%d %lf",&e,&c);
  17. Input[e]+=c;
  18. if (e>Maxe)Maxe=e;
  19. }
  20. line--;
  21. }
  22.  
  23. for (i=Maxe;i>=;i--)
  24. {
  25. if (Input[i]!=)cnt++;
  26. }
  27.  
  28. printf("%d",cnt);
  29. for (i=Maxe;i>=;i--)
  30. {
  31. if (Input[i]!=)printf(" %d %.1lf",i,Input[i]);
  32. }
  33. return ;
  34. }
  1.  

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. MongoDB 学习笔记一: 配置

    下载MongoDB 下载地址:https://www.mongodb.com/download-center?jmp=nav#community 这里是在windows平台下安装MongoDB, 下载 ...

  2. 同一行多个div宽度自适应布局

    主要运用到的是:布局神器display:table-cell 元素两端对齐 第一个案例是让两个元素分别向左和向右对齐,如果是过去,我一定会用float来实现,但其实用table可以这么做: 自动平均划 ...

  3. javase-排序

    public class sort { public static void main(String[] args) { String[] arr = {"aa","bb ...

  4. kaggle入门2——改进特征

    1:改进我们的特征 在上一个任务中,我们完成了我们在Kaggle上一个机器学习比赛的第一个比赛提交泰坦尼克号:灾难中的机器学习. 可是我们提交的分数并不是非常高.有三种主要的方法可以让我们能够提高他: ...

  5. ORACLE中常见SET指令

    1         SET TIMING ON 说明:显示SQL语句的运行时间.默认值为OFF. 在SQLPLUS中使用,时间精确到0.01秒.也就是10毫秒. 在PL/SQL DEVELOPER 中 ...

  6. QT操作EXCEL

    介绍一下最基本的QT对EXCEL的读写操作. 声明:转载于:http://blog.csdn.net/czyt1988/article/details/52121360 在使用QT的操作数据库的时候, ...

  7. Qtablevies获取内容

    首先是向tableview中添加内容 model=new QStandardItemModel(); model->setHorizontalHeaderItem(, new QStandard ...

  8. Qt中 QString 和int, char等的“相互”转换

    转载:http://blog.csdn.net/ei__nino/article/details/7297791 Qt中 int ,float ,double转换为QString 有两种方法 1.使用 ...

  9. C# 单例模式

    饿汉, 懒汉模式就不说了,下面是IODH模式 static void Main(string[] args) { Singleton s1, s2; s1 = Singleton.getInstanc ...

  10. 对《分享一下自己用c++写的小地图》一文的补充

    在写完上一篇文章后,发现了一个问题: 那就是编写的插件无法实时预览. 在学习了Slate之后,我找到了方法: 重写SynchronizeProperties函数 头文件中添加: #if WITH_ED ...