• 1009. Product of 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 product 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 up to 1 decimal place.

    Sample Input

    2 1 2.4 0 3.2
    2 2 1.5 1 0.5
    

    Sample Output

    3 3 3.6 2 6.0 1 1.6
    #include "iostream"
    using namespace std;
    typedef struct Node *ptrToNode;
    struct Node {
        int exp;
        float coef;
        ptrToNode next;
    };
    typedef ptrToNode List;
    void count(List l) {
        int k = 0;
        while (l) {
            k++;
            l = l->next;
        }
        cout << k << " ";
    }
    /* 打印链表中的所有元素
    */
    void dispList(List p) {
        int flag = 0;
        if (p == NULL)
            cout << "0 0";
        else {
            while (p) {
                if (flag == 0)
                    printf("%d %.1f", p->exp, p->coef);
                else
                    printf(" %d %.1f", p->exp, p->coef);
                p = p->next;
                flag++;
            }
        }
        cout << endl;
    }
    /* 将节点添加到链表尾端
    */
    void attach(float coef, int exp, List *rear) {
        List p = (List)malloc(sizeof(Node));
        p->coef = coef;
        p->exp = exp;
        p->next = NULL;
        (*rear)->next = p;
        *rear = p;
    }
    /* 读入多项式
    */
    List readPoly() {
        int n, exp;  // coef指数  exp系数
        float coef;
        List rear;
        cin >> n;
        List p = (List)malloc(sizeof(Node));
        p->next = NULL;
        rear = p; // 指向链表的末尾节点
        while (n--) {
            cin >> exp >> coef;
            attach(coef, exp, &rear);  //将指数,系数作为节点插入链表中
        }
        List t;
        t = p;
        p = p->next;
        free(t);
        return p;
    }
    /* 比较指数的大小
    */
    int compare(int exp1, int exp2) {
        if (exp1 > exp2) {
            return 1;
        }
        else if (exp1 == exp2) {
            return 0;
        }
        else {
            return -1;
        }
    }
    /* 实现多项式的相加操作
    */
    
    List addPoly(List l1, List l2) {
        List front, rear;
        rear = (List)malloc(sizeof(Node));  // 创建空的头结点
        front = rear;
        // 2个多项式的系数进行比较,如果相等 判断系数和是否为0 不为0 就插入
        while (l1 && l2) {
            int flag = compare(l1->exp, l2->exp);
            if (flag == 1) {
                attach(l1->coef, l1->exp, &rear);
                l1 = l1->next;
            }
            /* 系数相等 */
            else if (flag == 0) {
                if (l1->coef + l2->coef != 0) {
                    attach(l1->coef + l2->coef, l1->exp, &rear);
                }
                l1 = l1->next;
                l2 = l2->next;
            }
            else if (flag == -1) {
                attach(l2->coef, l2->exp, &rear);
                l2 = l2->next;
            }
        }
        for (; l1; l1 = l1->next) { attach(l1->coef, l1->exp, &rear);  }
        for (; l2; l2 = l2->next) { attach(l2->coef, l2->exp, &rear);  }
        rear->next = NULL;
        List s = front;
        front = front->next;
        free(s);
        return front;
    }
    /* 用L1的单元项 * l2的所有项 得到一个新链表
    */
    List multiOneItem(List l1, List l2) {
        List rear, p;
        rear = p = (List)malloc(sizeof(Node));
        while (l2) {
            List p = (List)malloc(sizeof(Node));
            p->coef = l1->coef * l2->coef;
            p->exp = l1->exp + l2->exp;
            rear->next = p;
            rear = rear->next;
            l2 = l2->next;
        }
        rear->next = NULL;
        p = p->next;
        return p;
    }
    /* 用mutiOneItem得到的新链表和之前的链表相加
    */
    List multiPoly(List l1, List l2) {
        List q = l1;
        List s = NULL;
        while (q) { //
            s = addPoly(s, multiOneItem(q, l2));
            q = q->next;
        }
        return s;
    }
    int  main() {
        List l1 = readPoly();  //读入一个多项式
        List l2 = readPoly();  //读入一个多项式
        List p1 = multiPoly(l1, l2); //多项式相乘
        count(p1);
        dispList(p1);
        return 0;
    }
  • 相关阅读:
    k8s之创建etcd集群
    完美解决微信video视频隐藏控件和内联播放问题
    JS工具库封装:Video转换成Canvas
    H5 video 标签 播放事件
    iSlider 如丝般高性能H5全屏滑动组件
    设计模式之 适配器模式
    c++ STL常用算法使用方法
    快速排序与二分查找
    CLOSE_WAIT TIME_WAIT
    erlang erl文件编译的三种脚本
  • 原文地址:https://www.cnblogs.com/minesweeper/p/6283308.html
Copyright © 2020-2023  润新知