当前位置:网站首页>一元多项式的乘法与加法运算 (20 分)
一元多项式的乘法与加法运算 (20 分)
2022-06-21 07:47:00 【Crer_lu】
一元多项式的乘法与加法运算 (20 分)
题目链接
一、算法分析
用结构题读入,然后用两个数组分别记录加法和乘法的结果。
加法可以在读入时就计算好,乘法可以在读入后跑一次两层循环。
二、测试点2、3:注意0多项式的情况
三、代码实现
#include<bits/stdc++.h>
using namespace std;
#define N 2005
struct tp{
int coe;
int index;
}poly1[N], poly2[N];
int ans_plus[N], ans_mult[N];
int main(){
int n, m;
scanf("%d", &n);
for(int i = 1; i <= n; ++ i){
scanf("%d%d", &poly1[i].coe, &poly1[i].index);
ans_plus[poly1[i].index] += poly1[i].coe;
}
scanf("%d", &m);
for(int i = 1; i <= m; ++ i){
scanf("%d%d", &poly2[i].coe, &poly2[i].index);
ans_plus[poly2[i].index] += poly2[i].coe;
}
for(int i = 1; i <= n; ++ i)
for(int j = 1; j <= m; ++ j)
ans_mult[poly1[i].index + poly2[j].index] += poly1[i].coe * poly2[j].coe;
bool flag = 1;
for(int i = poly1[1].index + poly2[1].index; i >= 0; -- i)
if(ans_mult[i]){
if(!flag) printf(" ");
flag = 0;
printf("%d %d", ans_mult[i], i);
}
if(flag) printf("0 0");
printf("\n");
flag = 1;
for(int i = max(poly1[1].index, poly2[1].index); i >= 0; -- i){
if(ans_plus[i]){
if(!flag) printf(" ");
flag = 0;
printf("%d %d", ans_plus[i], i);
}
}
if(flag) printf("0 0");
return 0;
}
边栏推荐
- Yyds dry goods inventory junit5 learning 3: assertions class
- Random random number class
- mysql中执行存储过程的语句怎么写
- [UML modeling] (4) sequence diagram of UML modeling
- JVM内存模型概念
- Leetcode topic [array] -40- combined sum II
- Ansa secondary development - external programs use socket to communicate with ansa
- Use js to switch between clickable and non clickable page buttons
- How to view the MySQL installation path
- Golang Sync. Use and principle of waitgroup
猜你喜欢

图解 Google V8 # 16:V8是怎么通过内联缓存来提升函数执行效率的?

dried food! Neuron competitive initialization strategy based on information bottleneck theory

RPA (shadow knife) does not need to write code to capture the product information of an East

解决Jenkins升级后不能保存配置的问题

JVM内存模型概念

mysql如何关闭事务

Rdkit | molecular similarity based on molecular fingerprint

为什呢代码没报错但是数据库里边的数据显示不出来

. Net 4.5 asynchronous programming pilot (async and await)

Wanzi detailed data warehouse, data lake, data middle platform and lake warehouse are integrated
随机推荐
mysql不是内部命令如何解决
China inorganic fiber market trend report, technical innovation and market forecast
You can't use typescript generics after reading it. Come to me for yyds dry inventory
mysql的安装路径如何查看
Golang Sync. Use and principle of waitgroup
RDKit | 基于Murcko骨架聚类化合物库
Fault analysis | case analysis of master-slave synchronization error reporting after MySQL slave restart
Rdkit | fragment decomposition of drug molecules
MATLAB 三维图(非常规)
What are the differences between SQL and MySQL
Using XAML only to realize the effect of ground glass background panel
Sword finger offer (2nd Edition) brush questions | 04 Find in 2D array
Talk about MySQL's locking rule "hard hitting MySQL series 15"
2021-06-18 STM32F103 DMA 与 DMA串口代码 使用固件库
Research Report on anhydrous trisodium phosphate industry - market status analysis and development prospect forecast
dried food! Neuron competitive initialization strategy based on information bottleneck theory
Software download method
Learning Tai Chi maker esp8226 (IX) JSON data communication III
2021-06-17 STM32F103 USART serial port code using firmware library
如何使用lerna进行多包(package)管理