甲1002 A+B for Polynomials
两个多项式A,B进行相加,每个多项式先输入此多项式有几项(K项),然后依次输入每一项的指数和系数
例如:
Note:
- 输出系数的结果保留一位小数,cout<<setiosflags(ios::fixed)<<setprecision(1)<<变量<<endl;在头文件<iomanip>中 io代表输入输出,manip是manipulator(操纵器)的缩写。setiosflags(ios::fixed)是指以固定的浮点显示,当setiosflags(ios::fixed)和setprecision(n)两个一起用时就标识保留n为小数输出。
- visual c++ 2010 在执行完程序后会不停留退出执行,需要加头文件<iostream> 程序末尾return前加system("pause")
原题:
1002 A+B for Polynomials (25分)
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<⋯<N2<N1≤1000.
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
法一:单链表实现:
如下图所示,为一元多项式 A(x)=7+3x+9x8+5x17 和 B(x)=8x+22x7-9x8 的链表表示图:
定义结构体 PLnode: 单链表一个结点表示多项式的一项;
函数void creatpolyn(PLinkList L, int k):创建多项式函数输入值为(头节点,项数k)
函数PLinkList Addpolyn(PLinkList La, PLinkList Lb):完成多项式相加,返回相加后的多项式链表头节点Lc
#include<cstdio>
#include<stdlib.h>
#include<iostream>
#include<iomanip>//控制精度头文件
using namespace std;
typedef struct PLnode{
int a;//指数
float b;//系数
struct PLnode *next;
}PLnode,*PLinkList;
//链表表示创建多项式,K项系数和指数(a系数0~1000,b指数1~10)
void creatpolyn(PLinkList L, int k){
L->next = NULL;
L->a = k;//头结点指数域记录多项书长度(K项)
L->b = -1;//头节点系数域设为-1
PLnode *p = L;//指针p作为遍历链表的指针
//创建K个节点并将其初始化,链接到L上
for(int i = 1; i <= k; i++){
PLnode *newk = (PLnode*)malloc(sizeof(PLnode));
cin >> newk->a >> newk->b;
p->next = newk;
p = newk;
p->next = NULL;
}
}
//完成多项式相加运算 Lc=La+Lb
PLinkList Addpolyn(PLinkList La, PLinkList Lb){
int Lc_k = 0;//Lc的项数,
PLinkList Lc = (PLnode*)malloc(sizeof(PLnode));
Lc->next = NULL;
PLnode *pa = La->next, *pb = Lb->next, *pc = Lc, *p;
Lb->next = NULL; free(Lb);
La->next = NULL; free(La);
while(pa && pb){
int x = pa->a - pb->a;//指数相减判断大小
if(x < 0){ //若pb所指项的指数大于pa所指项的指数,则将pb链接在Lc上
pc->next = pb;
pc = pb;
pb = pb->next;
pc->next = NULL;
++Lc_k;
}else if(x > 0){//若pb所指项的指数小于pa所指项的指数,则将pa链接在Lc上
pc->next = pa;
pc = pa;
pa = pa->next;
pc->next = NULL;
++Lc_k;
}else if(x == 0){//若指数相等
float n = pa->b + pb->b;//指数相等的项系数相加
if(n != 0.0){ //若系数和不为0,则创建系数等于n的结点并连接到Lc上
p = (PLnode*)malloc(sizeof(PLnode));
p->a = pa->a;
p->b = n;
pc->next = p;
pc = p;
pc->next = NULL;
++Lc_k;
}
//释放pa pb 所指节点
p = pa;//释放pa
pa = pa->next;
free(p);
p = pb;//释放pb
pb = pb ->next;
free(p);
}
}
while(pa){
pc->next = pa;
pc = pa;
pa = pa->next;
++Lc_k;
}
while(pb){
pc->next = pb;
pc = pb;
pb = pb->next;
++Lc_k;
}
pc->next = NULL;
Lc->a = Lc_k;
Lc->b = -1;
return Lc;
}
//输出Lc
void printpolyn(PLinkList Lc){
PLnode *p = Lc->next;
cout << Lc->a;
while(p){
cout << " "<<p->a ;
cout << setiosflags(ios::fixed)
<<setprecision(1)
<<" "
<< p->b;
p = p->next;
}
cout << endl;
}
int main(){
PLinkList La, Lb, Lc;
La = (PLinkList)malloc(sizeof(PLnode));
Lb = (PLinkList)malloc(sizeof(PLnode));
Lc = (PLinkList)malloc(sizeof(PLnode));
int La_k, Lb_k;
cin >> La_k;
creatpolyn(La,La_k);
cin >> Lb_k;
creatpolyn(Lb,Lb_k);
Lc = Addpolyn(La,Lb);
printpolyn(Lc);
system("pause");
return 0;
}
思路参考http://data.biancheng.net/view/90.html 中的讲述,改动趋向王道数据结构中经典单链表相加课后习题。
用此方法实现较好理解但实现要100多行。
法二:
以空间换时间的方式
创建一个大于题目要求的指数范围0~1000的数组a[1010]={}
用数组下标表示指数p,数组中存储系数k
#include<iostream>
#include<iomanip>
using namespace std;
int main(){
int k1, k2, i;
float a[1010]={};
float k;//系数
int p;//指数
cin >> k1;//输入第一个多项式的项数
for(i = 0; i < k1; i++){
cin >> p >>k;//依次输入每个项的“指数,系数”
a[p] = k;//指数为p的系数是k!!!!!!
}
cin >> k2;//输入第二个多项式的项数
for(i = 0; i < k2; i++){
cin >> p >> k;//依次输入每个项的“指数,系数”
a[p] = a[p] + k;//找到对应指数的系数相加!!!!!!
}
int n = 0;//统计相加后的多项式的长度
for(i = 0; i <= 1000; i++){
if(a[i] != 0) n++;
}
//输出结果
cout << n;
for(int i = 1000; i >= 0; i--){
if(a[i] != 0)
cout <<" "<<i<<" "
<<setiosflags(ios::fixed)
<<setprecision(1)
<<a[i];
}
return 0;
}
很巧妙的方法20多行可实现,代码参考https://blog.csdn.net/miao_599/article/details/83310906