博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
1002. A+B for Polynomials (25)
阅读量:5257 次
发布时间:2019-06-14

本文共 3285 字,大约阅读时间需要 10 分钟。

This time, you are supposed to find A+B where A and B are two polynomials.

Input

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

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.22 2 1.5 1 0.5

Sample Output

3 2 1.5 1 2.9 0 3.2
/* 这题多项式指数为0~1000 所以可以考虑数组进行存储。下标为指数    数组值为系数。*/#include "iostream"using namespace std;int main() {    float a[1002] = { 0 };    float b[1002] = { 0 };    int n, t;    cin >> n;    int exp;    float coef;    while (n--) {        cin >> exp >> coef;        a[exp] = coef;    }    cin >> t;    while (t--) {        cin >> exp >> coef;        b[exp] = coef;    }    int cnt = 0;    for (int i = 0; i <= 1000; i++) {        a[i] = a[i] + b[i];        if (a[i] != 0)            cnt++;    }    cout << cnt;    for (int i = 1000; i >= 0; i--)        if (a[i] != 0)            printf(" %d %.1f",i,a[i]);    cout << endl;    return 0;}

 

/*   多项式加法。用链表进行存储指数和系数。*/#include "iostream"using namespace std;struct Node {    int exp;    float coef;    Node* next;};typedef Node* ptrToNode;typedef ptrToNode* List; /* 指向指针的指针 */int len = 0;void attach(List l,int exp, float coef) {    ptrToNode p = (ptrToNode)malloc(sizeof(Node));    p->exp = exp;    p->coef = coef;    p->next = NULL;    (*l)->next = p;    (*l) = (*l)->next;}ptrToNode ReadPoly() {    ptrToNode l = (ptrToNode)malloc(sizeof(Node));    ptrToNode rear = l;    l->next = NULL;    int n;    cin >> n;    while (n--) {        int exp;        float coef;        cin >> exp >> coef;        attach(&rear, exp, coef);    }    ptrToNode p = l;    l = l->next;    free(p);    return l;}int compare(int a,int b) {    if (a < b)        return -1;    if (a == b)        return 0;    if (a > b)        return 1;}ptrToNode addPoly(ptrToNode l1,ptrToNode l2) {    ptrToNode l = (ptrToNode)malloc(sizeof(Node));    l->next = NULL;    ptrToNode rear = l;    while (l1 != NULL && l2 != NULL) {        switch (compare(l1->exp,l2->exp)) {        case -1: attach(&rear, l2->exp, l2->coef); l2 = l2->next; len++; break;        case 1: attach(&rear, l1->exp, l1->coef); l1 = l1->next; len++; break;        case 0:             if (l1->coef + l2->coef != 0) { attach(&rear, l1->exp, l1->coef + l2->coef); len++; }            l1 = l1->next; l2 = l2->next;  break;        }    }    while (l1 != NULL) { attach(&rear, l1->exp, l1->coef); l1 = l1->next; len++; }    while (l2 != NULL) { attach(&rear, l2->exp, l2->coef); l2 = l2->next; len++; }    ptrToNode p = l;    l = l->next;    free(p);    return l;}void disp(ptrToNode p) {    while (p != NULL) {        printf( " %d %.1f",p->exp,p->coef);        p = p->next;    }}int main() {    ptrToNode l1 = ReadPoly();    ptrToNode l2 = ReadPoly();    ptrToNode p = addPoly(l1, l2);    cout << len;    disp(p);    cout << endl;    return 0;}

 

转载于:https://www.cnblogs.com/minesweeper/p/6265163.html

你可能感兴趣的文章
shell脚本(管理守护进程)
查看>>
php 简单的对称加密
查看>>
Investment(完全背包)
查看>>
数组方法
查看>>
ROS Kinetic Install on Debian 9
查看>>
在linux下安装并运行scrapyd
查看>>
[PHP源码阅读]array_pop和array_shift函数
查看>>
宏定义 求结构体变量的偏移量
查看>>
Zend Framework相关
查看>>
迷宫问题
查看>>
英语配音片段
查看>>
[原创]前后端交互的方式整理
查看>>
css简介及常用语法
查看>>
git add 的一点说明
查看>>
时间类(时间戳的各种转换成)
查看>>
计算机的启动过程 <orang's 一个操作系统的实现>
查看>>
函数集成redis与Spring集成
查看>>
搜索中文Solr Analysis And Solr Query -- Solr分析以及查询
查看>>
core 文件生成设置详解
查看>>
一种数据展示方式,UI设计新颖,供大家参考(源码部分) (demo已经上传)
查看>>