zoukankan      html  css  js  c++  java
  • 线性结构2 一元多项式的乘法与加法运算 【STL】

    02-线性结构2 一元多项式的乘法与加法运算(20 分)

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

    输入分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

    思路
    先保存 两组数据
    可以用结构体 也可以直接用 pair

    #include <cstdio>
    #include <cstring>
    #include <ctype.h>
    #include <cstdlib>
    #include <cmath>
    #include <climits>
    #include <ctime>
    #include <iostream>
    #include <algorithm>
    #include <deque>
    #include <vector>
    #include <queue>
    #include <string>
    #include <map>
    #include <stack>
    #include <set>
    #include <numeric>
    #include <sstream>
    #include <iomanip>
    #include <limits>
    
    #define CLR(a) memset(a, 0, sizeof(a))
    
    using namespace std;
    typedef long long ll;
    typedef long double ld;
    typedef unsigned long long ull;
    typedef pair <int, int> pii;
    typedef pair <ll, ll> pll;
    typedef pair<string, int> psi;
    typedef pair<string, string> pss;
    
    const double PI = 3.14159265358979323846264338327;
    const double E = exp(1);
    const double eps = 1e-6;
    
    const int INF = 0x3f3f3f3f;
    const int maxn = 1e3 + 5;
    const int MOD = 1e9 + 7;
    
    pii a[maxn], b[maxn];
    
    int main()
    {
        int n, m;
        cin >> n;
        map <int, int> ans_add, ans_time;
        for (int i = 0; i < n; i++)
            scanf("%d%d", &a[i].first, &a[i].second);
        cin >> m;
        for (int i = 0; i < m; i++)
            scanf("%d%d", &b[i].first, &b[i].second);
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < m; j++)
            {
                ans_time[a[i].second + b[j].second] += a[i].first * b[j].first;
            }
        }
        for (int i = 0; i < n; i++)
            ans_add[a[i].second] += a[i].first;
        for (int i = 0; i < m; i++)
            ans_add[b[i].second] += b[i].first;
        int flag = 0;
        map <int, int>::iterator it;
        if (ans_time.size())
            for (it = ans_time.end(), it--; ; it--)
            {
                if (it->second)
                {
                    if (flag)
                        printf(" ");
                    else
                        flag = 1;
                    printf("%d %d", it->second, it->first);
                }
                if (it == ans_time.begin())
                    break;
            }
        if (flag == 0)
            printf("0 0");
        cout << endl;
        flag = 0;
        if (ans_add.size())
            for (it = ans_add.end(), it--; ; it--)
            {
                if (it->second)
                {
                    if (flag)
                        printf(" ");
                    else
                        flag = 1;
                    printf("%d %d", it->second, it->first);
                }
                if (it == ans_add.begin())
                    break;
            }
        if (flag == 0)
            printf("0 0");
        cout << endl;
    }
  • 相关阅读:
    selenium面试题
    Selenium(Webdriver)自动化测试常问问题
    v-on事件修饰符
    vue学习第二天:Vue跑马灯效果制作
    Vue错误汇总
    vue入门的第一天: v-on使用
    vue学习第一天:v-bind的使用(让属性绑定变量)
    vue入门的第一天:v-clock、v-text、v-html的使用
    git常用代码合集
    Area.js下载
  • 原文地址:https://www.cnblogs.com/Dup4/p/9433215.html
Copyright © 2011-2022 走看看