PTA7-2 一元多项式的乘法与加法运算测试样例+代码详解

PTA7-2 一元多项式的乘法与加法运算

测试样例:

序号 输入 输出
0 4 3 4 -5 2 6 1 -2 0 15 24 -25 22 30 21 -10 20 -21 8 35 6 -33 5 14 4 -15 3 18 2 -6 1
3 5 20 -7 4 3 1 5 20 -4 4 -5 2 9 1 -2 0
1 2 1 2 1 0 1 4 -1 0
2 1 2 -1 0 2 2
2 2 -1000 1000 1000 0 -1000000 2000 2000000 1000 -1000000 0
2 1000 1000 -1000 0 0 0
3 0 0 0
1 999 1 999

代码C语言

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

#define ERROR 1e8
typedef int ElementType;
typedef struct Node *list;
struct Node//队列中每一个元素的基本结构
{
    int coef;//系数
    int idx;//指数
    struct Node *next;
};
list head1,head2;

 //void putin(list head,int sum)
  list putin(list head)//读入
 {
     list p,end;
     int sum;
     head=(list)malloc(sizeof(list));//一定要放到end=head前面
     end=head;//先分配空间,再赋值
     int i;
     scanf("%d",&sum);

     for(i=0;i<sum;i++)
     {
         p=(list)malloc(sizeof(list));
         scanf("%d %d",&p->coef,&p->idx);
         end->next=p;
         end=p;
     }
     end->next=NULL;
     return head;
 }
 list mul(list head1,list head2)
 {
     list head,node,end;
     head=(list)malloc(sizeof(list));
     end=head;
     //list temp;
    //temp=head2;
    list temp=head2;
     if(head1->next==NULL||head2->next==NULL)
     {
         node=(list)malloc(sizeof(list));
         node->coef=0;
         node->idx=0;
         head->next=node;
         node->next=NULL;
     }
     else
     {
         while(head1->next!=NULL)
         {
             head1=head1->next;
             while(head2->next!=NULL)
             {
                 head2=head2->next;
                 node=(list)malloc(sizeof(list));
                 node->coef=(head1->coef)*(head2->coef);
                 node->idx=(head1->idx)+(head2->idx);
                 end->next=node;
                 end=node;
                 end->next=NULL;
             }
             head2=temp;
             //head2->next=temp           
         }
     }
     return head;
       
 }
list combine(list head)//用数组排序合并同类项
{
    int a[10001],b[10001];
    a[0]=10000;
    int i=1;
    list comb,node,end;
    comb=(list)malloc(sizeof(list));
    end=comb;
    list temp,idx;
    temp=head;
    idx=head;
    while(head->next!=NULL)
    {
        head=head->next;
        a[i]=head->idx;
        i++;      
    }
    int j,k,t;
    for(j=1;j<i;j++)
    {
        for(k=j+1;k<i;k++)
        {
            if(a[k]>a[j])
            {
                t=a[k];
                a[k]=a[j];
                a[j]=t;
            }
        }
    }
    for(j=1;j<i;j++)
    {
        if(a[j-1]>a[j])
        {
            node=(list)malloc(sizeof(list));
            node->idx=a[j];
            node->coef=0;
            while(temp->next!=NULL)
            {
                temp=temp->next;
                if(temp->idx==a[j])
                {
                    node->coef=node->coef + temp->coef;
                }
            }
            temp=idx;

            end->next=node;
            end=node;
            end->next=NULL;
        }
    }
    return comb;
}
 list add(list head1,list head2)
 {
     list head,node,end;
     int tag=0;
     int i=0;
     int sum;
     head=(list)malloc(sizeof(list));
     end=head;
     if((head1->next!=NULL)&&(head2->next!=NULL))
        {
            tag=1;
            head1=head1->next;
            head2=head2->next;
            while((head1!=NULL)&&(head2!=NULL))
            {
                node=(list)malloc(sizeof(list));
                if(head1->idx>head2->idx)
                {
                    node->idx=head1->idx;
                    node->coef=head1->coef;
                    head1=head1->next;
                }
                else if(head1->idx<head2->idx)
                {
                    node->idx=head2->idx;
                    node->coef=head2->coef;
                    head2=head2->next;
                }
                else
                {
                    node->idx=head1->idx;
                    node->coef=head1->coef + head2->coef;
                    head1=head1->next;
                    head2=head2->next;
                }
                
                    end->next=node;
                    end=node;
                    end->next=NULL;
            }
        }
        //运行到这里有两种情况
        if(tag==1)// 第一种是if((head1->next!=NULL)&&(head2->next!=NULL))条件成立,while((head1!=NULL)&&(head2!=NULL))不成立后跳出
        {
             while(head1)
        {
            //head1=head1->next;
            node=(list)malloc(sizeof(list));
            node->coef=head1->coef;
            node->idx=head1->idx;
           head1=head1->next;
            end->next=node;
            end=node;
            end->next=NULL;
        }
        while(head2)
        {
            //head2=head2->next;
            node=(list)malloc(sizeof(list));
            node->coef=head2->coef;
            node->idx=head2->idx;
            head2=head2->next;
            end->next=node;
            end=node;
            end->next=NULL;
        
          }
        }
        else//第二种是if((head1->next!=NULL)&&(head2->next!=NULL))条件不成立
        {
            while(head1->next)
            {
                head1=head1->next;
                 node=(list)malloc(sizeof(list));
            node->coef=head1->coef;
            node->idx=head1->idx;
           //head1=head1->next;
            end->next=node;
            end=node;
            end->next=NULL;
            }
            while(head2->next)
        {
            head2=head2->next;
            node=(list)malloc(sizeof(list));
            node->coef=head2->coef;
            node->idx=head2->idx;
            //head2=head2->next;
            end->next=node;
            end=node;
            end->next=NULL;
        
          }
            
        }
        
           
        
        
        return head;
 }
 void display(list head)//注意控制格式
 {
     int flag=1;
     int tag=1;
     list temp=head;
     while(tag==1&&temp->next!=NULL)
     {
         temp=temp->next;
         if(temp->coef!=0)
         {
             tag=0;
         }
     }
     if(tag==1)
         {
             printf("0 0");
         }
    else
    {
        while(head->next!=NULL)
     {      
         if(flag==0&&head->next->next==NULL)
         {
             head=head->next;
             if(head->coef!=0)
               printf(" %d %d",head->coef,head->idx);
         }
          else if(flag==1)
         {
         head=head->next;
         if(head->coef!=0)
           printf("%d %d",head->coef,head->idx);
           flag=0;
         }
         else
         {
          head=head->next;
          if(head->coef!=0)
           printf(" %d %d",head->coef,head->idx);
         }      
     
       }

    }

     
 }
 int main()
 {
     int sum1,sum2;
     list head;
     //scanf("%d %d",&sum1,&sum2);
     head1=putin(head1);
     head2=putin(head2);
     //putin(head1,sum1);
     //putin(head2,sum2);
     head=mul(head1,head2);
     head=combine(head);
     display(head);
     printf("\n");
     head=add(head1,head2);
     display(head);
     //printf("\n");
     //head=mul(head1,head2);
     return 0;
 }

猜你喜欢

转载自blog.csdn.net/qq_45955041/article/details/106996514
今日推荐