首页 > 编程语言 >1002 A+B for Polynomials C++

1002 A+B for Polynomials C++

时间:2023-07-10 21:57:50浏览次数:43  
标签:std int C++ ++ num coe Polynomials exp 1002

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

Sample Output:

3 2 1.5 1 2.9 0 3.2

题意:

题目意思是计算两个多项式的加法。

分析:

水题。用水桶法,用数组下标表示指数,数组值表示系数,逆序输出,值为0则不输出。

代码:

//
// Created by yaodong on 2023/7/5.
//
#include "iostream"
#include "cstring"
int main() {
    double num[1001];
    memset(num, 0, sizeof(num));
    int k, exp;
    double coe;
    std::cin >> k;
    for (int i = 0; i < k; ++i) {
        std::cin >> exp >> coe;
        num[exp] += coe;
    }
    std::cin >> k;
    for (int i = 0; i < k; ++i) {
        std::cin >> exp >> coe;
        num[exp] += coe;
    }
    int len = 0;
    for (int i = 0; i < 1001; ++i) {
        if(num[i] != 0)
            len++;
    }
    std::cout << len;
    for (int i = 1000; i >= 0; --i) {
        if(num[i] != 0)
            printf(" %d %.1f", i, num[i]);
    }
}

 

标签:std,int,C++,++,num,coe,Polynomials,exp,1002
From: https://www.cnblogs.com/langweixianszu/p/17542448.html

相关文章

  • 如何使用C++11 STD::THREAD设置堆栈大小?
    本教程将介绍如何使用C++11std::thread设置线程的堆栈大小。C++11std::thread是一种轻量级的多线程实现,它的灵活性使得它成为一个流行的选择。但是,在某些情况下,您可能需要设置线程的堆栈大小来满足您的需求。在开始本教程之前,我们假设您已经熟悉了C++11std::thread的基础知识......
  • 遇到难题了,在线等大佬求解\C++
    intmain(){ characcounts[]={0}; charpassword[]={0}; inti=0; printf("请输入账号:>"); scanf("%s",accounts); (strcmp(accounts,"1234")==0); for(i=1;i<=3;i++) { printf("请输入密码:>"); ......
  • C++程序设计综合实验任选题目[2023-07-10]
    C++程序设计综合实验任选题目[2023-07-10]程序设计综合实验任选题目简单题目题目1模拟ATM机存取款管理系统设计1、问题描述模拟银行的自动取款及使用过程中的界面和用户交互过程。实现查询银行卡余额、取款、修改密码、退出系统等功能。2、功能要求(1)卡号、密码输入最多......
  • python3使用pip安装wordcloud报错error: Microsoft Visual C++ 14.0 or greater is re
    背景:使用的是Anaconda集成环境,python版本是:3.10,安装wordcloud包,使用的命令是:pipinstallwordcloud,出现报错:error:MicrosoftVisualC++14.0orgreaterisrequired.Getitwith"MicrosoftC++BuildTools":https://visualstudio.microsoft.com/visual-cpp-build-tools/......
  • 如何用C++11实现观察者模式
    观察者模式是一种设计模式,定义了对象之间的一对多关系。当一个对象状态发生改变时,它的依赖者都会收到通知并自动更新。在C++11中,可以通过以下方式实现观察者模式:首先,我们需要创建一个观察者接口,其中包含一个更新方法。这个接口可以被多个观察者类实现,从而实现多态。#include<iostr......
  • C++类模板实现工厂模式(优化if else/switch case)
    引自:https://blog.csdn.net/weixin_43795921/article/details/127224633template<typenameIdentifierType,classAbstractProduct,classProductCreator=AbstractProduct*(*)(),classMapContainer=std::map<IdentifierType,ProductCreato......
  • C++ 数据抽象
     数据抽象是指,只向外界提供关键信息,并隐藏其后台的实现细节,即只表现必要的信息而不呈现细节。数据抽象是一种依赖于接口和实现分离的编程(设计)技术。让我们举一个现实生活中的真实例子,比如一台电视机,您可以打开和关闭、切换频道、调整音量、添加外部组件(如喇叭、录像机、DVD播......
  • C++题解——格子游戏
    题目链接:一本通TFLSOJ思路:使用并查集给点连接,如果在连接过程中遇到已连接的点二次连接,就输出代码:#include<bits/stdc++.h>usingnamespacestd;structnode{ intx,y;};nodef[205][205];intn,m;nodefind(nodek){ if(f[k.x][k.y].x==k.x&&f[k.x][k.y].y==k.y)retur......
  • c/c++程序编译运行全过程
    一.预处理-gcc-E1.进行头文件的替换(执行预处理指令)2.删除注释3.不会检查语法二.编译-gcc-S1.会检查语法2.将C语言代码翻译成对应平台的汇编语言三.汇编-gcc-c将汇编语言翻译成二进制指令四.链接-gcc-o把用到的标准库的函数实现,系统启动代码一并链接到最后生成的可......
  • 华为工程师(王桂林)带你实战C++
    适合人群:有一定的C语言基础或是想提高C++水平的在职人员或是想要从事C、C++开发的绝大多数人你将学到:本课程我以实战为主,课上全部代码均为边讲边手敲,学会此套课程,可以达到一个C++中高级研发者的水平。课程简介:王桂林老师,曾供职于海尔,华为等世界500强企业。现在专职于C++教......