人生第一个成功百行代码

#include<iostream>
using namespace std;

typedef struct Node{
    int coe;
    int index;
    Node *next;
}node,*nodeptr;

nodeptr add(nodeptr head1,nodeptr head2);
void show(nodeptr head3);
void del(nodeptr k);
void addall(nodeptr a[],int end);


int main(void)
{
    nodeptr head1,p;
    head1=new node;
    head1->next=NULL;
    p=head1;
    int M;
    cin >> M;
    for(int i=0;i<=M;i++){
        nodeptr q=new node;
        cout << "coe?" << endl;
        cin >> q->coe;
        cout << "index?" << endl;
        cin >> q->index;
        p->next=q;
        p=q;
    }
    p->next=NULL;
    
    
    nodeptr head2;
    head2=new node;
    head2->next=NULL;
    p=head2;
    int N;
    cin >> N;
    for(int i=0;i<=N;i++){
        nodeptr q=new node;
        cout << "coe?" << endl;
        cin >> q->coe;
        cout << "index?" << endl;
        cin >> q->index;
        p->next=q;
        p=q;
    }
    p->next=NULL;

    




    nodeptr a[M+1],h=head1->next;
    int i=0;
    while(h){
        nodeptr g=head2->next,temp;
        a[i]=new node;
        a[i]->next=NULL;
        temp=a[i];
        while(g){
            nodeptr k=new node;
            k->coe=g->coe * h->coe;
            k->index=g->index + h->index;
            g=g->next;
            temp->next=k;
            temp=k;
        }
        temp->next=NULL;
        h=h->next;
        i++;
    }


    addall(a,M);


    show(a[0]);
    for(int i=0;i<M+1;i++){
        del(a[i]);
    }

    del(head1);
    del(head2);




    

    return 0;
}



nodeptr add(nodeptr head1,nodeptr head2){
    nodeptr head3,p,q;
    head3=new node;
    p=head1->next;
    q=head2->next;
    nodeptr m=new node;
    head3->next=m;
    while(p!=NULL || q!=NULL){
        if(p==NULL){
            
                m->next=q->next;
                m->index=q->index;
                m->coe=q->coe;
                
            
            break;
        }
        if(q==NULL){
            
            m->next=p->next;
            m->index=p->index;
            m->coe=p->coe;
            
            
            break;
        }
        nodeptr temp=new node;
        m->next=temp;
        if(p->index==q->index){
            m->index=q->index;
            m->coe=q->coe+p->coe;
            m=m->next;
            p=p->next;
            q=q->next;
        }
        else    if(p->index>q->index){
                    m->index=q->index;
                    m->coe=q->coe;
                    m=m->next;
                    q=q->next;
                }
            else{
                    m->index=p->index;
                    m->coe=p->coe;
                    m=m->next;
                    p=p->next;
            }
         
    }

    return head3;
}



void show(nodeptr head3){
    nodeptr p=head3->next;
    while(p){
        cout << p->coe <<"x^" << p->index << "+";
        p=p->next;
    }
    return;
}




void del(nodeptr head2){
    nodeptr p=head2->next;
    while(p){
        nodeptr temp=p;
        p=p->next;
        delete temp;
    }
    delete head2;
}



void addall(nodeptr a[],int end){
    if(end==0)  return;
    if(end%2==1){
        for(int i=0;i<=end/2;i++){
            a[i]=add(a[i],a[end-i]);
        }
        addall(a,end/2);
    }
    else{
        for(int i=0;i<end/2;i++){
            a[i]=add(a[i],a[end-i]);
        }
        addall(a,end/2);
    }
    return;
}

快200行

ohhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值