设计函数分别求两个一元多项式的乘积与和。

输入格式:

输入分2行,每行分别先给出多项式非零项的个数,再以指数递降方式输入一个多项式非零项系数和指数(绝对值均为不超过1000的整数)。数字间以空格分隔。

输出格式:

输出分2行,分别以指数递降方式输出乘积多项式以及和多项式非零项的系数和指数。数字间以空格分隔,但结尾不能有多余空格。零多项式应输出0 0

输入样例:

4 3 4 -5 2  6 1  -2 0
3 5 20  -7 4  3 1

输出样例:

15 24 -25 22 30 21 -10 20 -21 8 35 6 -33 5 14 4 -15 3 18 2 -6 1
5 20 -4 4 -5 2 9 1 -2 0

第一次写的,修修改改花了快6个小时

/*

设计函数分别求两个一元多项式的乘积与和。

输入格式:

输入分2行,每行分别先给出多项式非零项的个数,再以指数递降方式输入一个多项式非零项系数和指数(绝对值均为不超过1000的整数)。数字间以空格分隔。

输出格式:

输出分2行,分别以指数递降方式输出乘积多项式以及和多项式非零项的系数和指数。数字间以空格分隔,但结尾不能有多余空格。零多项式应输出0 0。

*/

#include <stdio.h>

#include <stdlib.h>

typedef int ElementType;

typedef struct node *Stacknode;

struct node{

int coef;

int expon;

Stacknode next;

};

typedef Stacknode Polynomial;

void PrintPoly(Polynomial P)

{

int flag = 0;

if(!P)

{

printf("0 0\n");

return;

}

P = P->next;

while(P)

{

if(!flag)

{

flag = 1;

}

else

{

printf(" ");

}

printf("%d %d",P->coef,P->expon);

P = P->next;

}

printf("\n");

}

Polynomial Read( int n )

{

int i;

Polynomial Head = (Polynomial)malloc(sizeof(struct node));

Polynomial Rear = Head;

Head->next =NULL;

for(i=0;i<n;i++)

{

Polynomial tmp = (Polynomial)malloc(sizeof(struct node));

scanf("%d",&tmp->coef);

scanf("%d",&tmp->expon);

tmp->next = NULL;

Rear->next = tmp;

Rear = tmp;

}

return Head;

}

/*void Attach( int c, int e, Polynomial Rear)

{

Polynomial P;

P = (Polynomial)malloc(sizeof(struct node));

P->coef = c;

P->expon = e;

P->next = NULL;

Rear->next = P;

Rear = P;

}

*/

Polynomial AddPoly( Polynomial P1,Polynomial P2 )

{

Polynomial PP1,PP2,tmp,Head,Rear;

Rear = (Polynomial)malloc(sizeof(struct node));

Rear->next = NULL;

Head = Rear;

PP1 = P1->next;

PP2 = P2->next;

if(!PP1)

{

Rear->next = PP2;

return Head;

}

if(!PP2)

{

Rear->next = PP1;

return Head;

}

while( PP1 && PP2 )

{

if( PP1->expon > PP2->expon )

{

Polynomial temp = (Polynomial)malloc(sizeof(struct node));

temp->coef = PP1->coef;

temp->expon = PP1->expon;

temp->next = NULL;

Rear->next = temp;

Rear = temp;

PP1 = PP1->next;

}

else if(PP1->expon < PP2->expon )

{

Polynomial temp = (Polynomial)malloc(sizeof(struct node));

temp->coef = PP2->coef;

temp->expon = PP2->expon;

temp->next = NULL;

Rear->next = temp;

Rear = temp;

PP2 = PP2->next;

}

else if( PP1->expon == PP2->expon )

{

if( PP1->coef + PP2->coef == 0 )

{

PP1 = PP1->next;

PP2 = PP2->next;

}

else

{

Polynomial temp = (Polynomial)malloc(sizeof(struct node));

temp->coef = PP1->coef + PP2->coef;

temp->expon = PP1->expon;

temp->next = NULL;

Rear->next = temp;

Rear = temp;

PP1 = PP1->next;

PP2 = PP2->next;

}

}

}

while(PP1)

{

Polynomial temp = (Polynomial)malloc(sizeof(struct node));

temp->coef = PP1->coef;

temp->expon = PP1->expon;

temp->next = NULL;

Rear->next = temp;

Rear = temp;

PP1 = PP1->next;

}

while(PP2)

{

Polynomial temp = (Polynomial)malloc(sizeof(struct node));

temp->coef = PP2->coef;

temp->expon = PP2->expon;

temp->next = NULL;

Rear->next = temp;

Rear = temp;

PP2 = PP2->next;

}

return Head;

}

Polynomial MultPoly( Polynomial P1,Polynomial P2 )

{

Polynomial PP1,PP2,Head,Rear,temp;

int c,e;

if( !P1 || !P2 )

{

return NULL;

}

PP1 = P1->next;

PP2 = P2->next;

Head = (Polynomial)malloc(sizeof(struct node));

Head->next = NULL;

Rear = Head;

while(PP2)

{

Polynomial temp = (Polynomial)malloc(sizeof(struct node));

temp->coef = PP1->coef * PP2->coef;

temp->expon = PP1->expon + PP2->expon;

temp->next = NULL;

Rear->next = temp;

Rear = temp;

PP2 = PP2->next;

}

PP1 = PP1->next;

while(PP1)

{

PP2 = P2->next;

Rear = Head;

while(PP2)

{

e = PP1->expon + PP2->expon;

c = PP1->coef * PP2->coef;

while(Rear->next && Rear->next->expon > e )

{

Rear = Rear->next;

}

if(Rear->next && Rear->next->expon == e )

{

if(Rear->next->coef + c)

{

Rear->next->coef += c;

}

else

{

temp = Rear->next;

Rear->next = temp->next;

free(temp);

}

}

else

{

temp = (Polynomial)malloc(sizeof(struct node));

temp->coef = c;

temp->expon = e;

temp->next = Rear->next;

Rear->next = temp;

Rear = Rear->next;

}

PP2 = PP2->next;

}

PP1 = PP1->next;

}

return Head;

}

int main()

{

int m,n;

Polynomial P1,P2,PS1,PS2;

scanf("%d",&m);

P1 = Read(m);

scanf("%d", &n);

P2 = Read(n);

PS1 = AddPoly(P1,P2);

PS2 = MultPoly(P1,P2);

PrintPoly(PS2);

PrintPoly(PS1);

}

结果是这样的:

我心态崩了,好不容易调试出来又有bug,于是乎只能继续;

/*
设计函数分别求两个一元多项式的乘积与和。
输入格式:

输入分2行,每行分别先给出多项式非零项的个数,再以指数递降方式输入一个多项式非零项系数和指数(绝对值均为不超过1000的整数)。数字间以空格分隔。
输出格式:

输出分2行,分别以指数递降方式输出乘积多项式以及和多项式非零项的系数和指数。数字间以空格分隔,但结尾不能有多余空格。零多项式应输出0 0。
*/

#include <stdio.h>
#include <stdlib.h>

typedef int ElementType;
typedef struct node *Stacknode;
struct node{
    int coef;
    int expon;
    Stacknode next;
};

typedef Stacknode Polynomial;
void PrintPoly(Polynomial P)
{
    int flag = 0;

if(!P->next)
    {
        printf("0 0\n");
        return;
    }
    P = P->next;
    while(P)
    {
        if(!flag)
        {
            flag = 1;
        }
        else
        {
            printf(" ");
        }
        
        printf("%d %d",P->coef,P->expon);
        P = P->next;
        
    }

printf("\n");
}

Polynomial Read( int n )
{
    int i;
    Polynomial Head = (Polynomial)malloc(sizeof(struct node));
    Polynomial Rear = Head;
    Head->next =NULL;
    for(i=0;i<n;i++)
    {
        Polynomial tmp  = (Polynomial)malloc(sizeof(struct node));
        scanf("%d",&tmp->coef);
        scanf("%d",&tmp->expon);
        tmp->next = NULL;
        Rear->next = tmp;
        Rear = tmp;
    }
    return Head;
}
/*void Attach( int c, int e, Polynomial Rear)
{
    Polynomial P;
    P = (Polynomial)malloc(sizeof(struct node));
    P->coef = c;
    P->expon = e;
    P->next = NULL;
    Rear->next = P;
    Rear = P;
}
*/

Polynomial AddPoly( Polynomial P1,Polynomial P2 )
{
    Polynomial PP1,PP2,tmp,Head,Rear;
    Rear = (Polynomial)malloc(sizeof(struct node));
    Rear->next = NULL;
    Head = Rear;
    PP1 = P1->next;
    PP2 = P2->next;
    if(!PP1)
    {
        Rear->next = PP2;
        return Head;
    }
    if(!PP2)
    {
        Rear->next = PP1;
        return Head;
    }
    while( PP1 && PP2 )
    {
        if( PP1->expon > PP2->expon )
        {
            Polynomial temp = (Polynomial)malloc(sizeof(struct node));
            temp->coef  = PP1->coef;
            temp->expon = PP1->expon;
            temp->next = NULL;
            Rear->next = temp;
            Rear = temp;
            PP1 = PP1->next;
            
        }
        else if(PP1->expon < PP2->expon )
        {
            Polynomial temp = (Polynomial)malloc(sizeof(struct node));
            temp->coef  = PP2->coef;
            temp->expon = PP2->expon;
            temp->next = NULL;
            Rear->next = temp;
            Rear = temp;
            PP2 = PP2->next;
        }
        else if( PP1->expon == PP2->expon )
        {
            if( PP1->coef + PP2->coef == 0 )
            {
                PP1 = PP1->next;
                PP2 = PP2->next;
            }
            else
            {
                Polynomial temp = (Polynomial)malloc(sizeof(struct node));
                temp->coef = PP1->coef + PP2->coef;
                temp->expon = PP1->expon;
                temp->next = NULL;
                Rear->next = temp;
                Rear = temp;
                PP1 = PP1->next;
                PP2 = PP2->next;
            }
        }
    }
    while(PP1)
    {
            Polynomial temp = (Polynomial)malloc(sizeof(struct node));
            temp->coef  = PP1->coef;
            temp->expon = PP1->expon;
            temp->next = NULL;
            Rear->next = temp;
            Rear = temp;
            PP1 = PP1->next;
    }
    while(PP2)
    {
            Polynomial temp = (Polynomial)malloc(sizeof(struct node));
            temp->coef  = PP2->coef;
            temp->expon = PP2->expon;
            temp->next = NULL;
            Rear->next = temp;
            Rear = temp;
            PP2 = PP2->next;
    }
    return Head;
    
}

Polynomial MultPoly( Polynomial P1,Polynomial P2 )
{
    Polynomial PP1,PP2,Head,Rear,temp;
    int c,e;
    if( !P1 || !P2 )
    {
        return NULL;
    }
    PP1 = P1->next;
    PP2 = P2->next;

Head = (Polynomial)malloc(sizeof(struct node));
    Head->next = NULL;
    Rear = Head;
    while(PP2)
    {
        Polynomial temp = (Polynomial)malloc(sizeof(struct node));
        temp->coef = PP1->coef * PP2->coef;
        temp->expon = PP1->expon + PP2->expon;
        temp->next = NULL;
        Rear->next = temp;
        Rear = temp;
        PP2 = PP2->next;
    }
    PP1 = PP1->next;
    while(PP1)
    {
        PP2 = P2->next;
        Rear = Head;
        while(PP2)
        {
            e = PP1->expon + PP2->expon;
            c = PP1->coef * PP2->coef;
            while(Rear->next && Rear->next->expon > e )
            {
                Rear = Rear->next;
            }
            if(Rear->next && Rear->next->expon == e )
            {
                if(Rear->next->coef + c)
                {
                    Rear->next->coef += c;
                }
                else
                {
                    temp = Rear->next;
                    Rear->next = temp->next;
                    free(temp);
                }
            }
            else
            {
                temp = (Polynomial)malloc(sizeof(struct node));
                temp->coef = c;
                temp->expon = e;
                temp->next = Rear->next;
                Rear->next = temp;
                Rear = Rear->next;
            }
            PP2 = PP2->next;
        }
        PP1 = PP1->next;
    }
    return Head;

}

int main()
{
    int m,n;
    Polynomial P1,P2,PS1,PS2;
    scanf("%d",&m);
    P1 = Read(m);
    scanf("%d", &n);
    P2 = Read(n);
    PS1 = AddPoly(P1,P2);
    PS2 = MultPoly(P1,P2);
    PrintPoly(PS2);
    PrintPoly(PS1);

}

mmp还是有一个测试点过不了,沃日,想想这个尿题就来气。

浙江大学 PTA习题3.6 一元多项式的乘法与加法运算 (20分)相关推荐

  1. 习题3.6 一元多项式的乘法与加法运算 (20 分)(有测试点具体数据)c语言链表版本

    习题3.6 一元多项式的乘法与加法运算 (20 分) 设计函数分别求两个一元多项式的乘积与和. 输入格式: 输入分2行,每行分别先给出多项式非零项的个数,再以指数递降方式输入一个多项式非零项系数和指数 ...

  2. 7-2 一元多项式的乘法与加法运算 (20 分)

    7-2 一元多项式的乘法与加法运算 (20 分) 设计函数分别求两个一元多项式的乘积与和. 输入格式: 输入分2行,每行分别先给出多项式非零项的个数,再以指数递降方式输入一个多项式非零项系数和指数(绝 ...

  3. PTA 7-1 一元多项式的乘法与加法运算 (20 分)

    设计函数分别求两个一元多项式的乘积与和. 输入格式: 输入分2行,每行分别先给出多项式非零项的个数,再以指数递降方式输入一个多项式非零项系数和指数(绝对值均为不超过1000的整数).数字间以空格分隔. ...

  4. 7-2 一元多项式的乘法与加法运算 (20 分)(思路加详解+map做法)map真香啊 各个测试点的用例子 来吧宝贝!

    一:题目 设计函数分别求两个一元多项式的乘积与和. 输入格式: 输入分2行,每行分别先给出多项式非零项的个数,再以指数递降方式输入一个多项式非零项系数和指数(绝对值均为不超过1000的整数).数字间以 ...

  5. 7-2 一元多项式的乘法与加法运算 (20分)

    设计函数分别求两个一元多项式的乘积与和. 输入格式: 输入分2行,每行分别先给出多项式非零项的个数,再以指数递降方式输入一个多项式非零项系数和指数(绝对值均为不超过1000的整数).数字间以空格分隔. ...

  6. 7-2 一元多项式的乘法与加法运算 (20分) 设计函数分别求两个一元多项式的乘积与和。

    输入格式: 输入分2行,每行分别先给出多项式非零项的个数,再以指数递降方式输入一个多项式非零项系数和指数(绝对值均为不超过1000的整数).数字间以空格分隔. 输出格式: 输出分2行,分别以指数递降方 ...

  7. PTA:编程题:7-1 一元多项式的乘法与加法运算 (20 分)

    大一下半期数据结构 数据结构题目集 一元多项式的乘法与加法运算 设计函数分别求两个一元多项式的乘积与和. 输入格式: 输入分2行,每行分别先给出多项式非零项的个数,再以指数递降方式输入一个多项式非零项 ...

  8. 7-2一元多项式的乘法与加法运算

    title: "7-2一元多项式的乘法与加法运算(20" date: 2018-06-14T01:09:46+08:00 tags: [""] categori ...

  9. 浙大数据结构题集02-线性结构2 一元多项式的乘法与加法运算python版

    浙大数据结构题集02-线性结构2 一元多项式的乘法与加法运算python版 设计函数分别求两个一元多项式的乘积与和. 本文用链表做的 输入格式: 输入分2行,每行分别先给出多项式非零项的个数,再以指数 ...

最新文章

  1. Atitit.java jna  调用c  c++ dll的原理与实践  总结  v2  q27
  2. 大一c语言大作业课题大全,昆明理工大学大一C语言大作业题目.doc
  3. c++语言static作用,详解c++中的 static 关键字及作用
  4. 【BZOJ4236】JOIOJI [DP]
  5. leetcode word break java,Word Break leetcode java
  6. 简单快速的用SpringBoot访问静态资源(图片、html)
  7. 佳能打印机ip90 64位系统的驱动_佳能打印机如何安装 佳能打印机漏墨如何解决【详解】...
  8. NOIP模拟测试25
  9. highslide图片查看特效
  10. Euraka配置详解
  11. 对LNode*与LinkLinst等价却不等用的理解
  12. MIMO-OFDM学习笔记(传播与衰落)
  13. 让硬盘灯不再狂闪,调整Win7系统绝技(转)
  14. nginx之30分钟搞定nginx反向代理和负载均衡
  15. HTML创建表格及合并单元格
  16. UEFI 基础教程 (十四) - 设置默认启动项为UEFI Shell
  17. 将打开网页以网页 仅HTML,网页保存的不同方法
  18. java实现国密加解密
  19. 数电课程设计之7段显示器8421BCD码转换器
  20. STM32 IIC通信简介+PCF8563时钟芯片示例

热门文章

  1. 吉他谱——寂寞是因为思念谁
  2. CAFFE(FAQ.2):Ubuntu 配置caffe 框架之数据库读取,错误解决:ImportError: No module named leveldb解决办法...
  3. CentOS平滑更新nginx版本
  4. Java 面试高频提问知识点一
  5. 了解MyBatis框架
  6. 玩转Linux系统用户管理
  7. Python pow() 函数
  8. 01-09 Linux三剑客-sed
  9. matlab 限幅,限幅是什么意思
  10. Web前端前景、最新技术、学习路线?