NBUT The Sum of F(x) and G(x)

  • 问题描述
  • When Deathmoon played MC game, he faced a math problem. When he found a ancient tomb and came in, he found two polynomials f(x) and g(x) no the wall, only did he calculate f(x) + g(x) correctly he could come in, can you help him?
    For example:
    f(x) = 2*x^5 + 3*x^3 + 7 + x^(-1),
    g(x) = 3*x^4 + 2*x^3 + x - x^(-1).
    Then,
    f(x) + g(x) = 2*x^5 + 3*x^4 + 5*x^3 + x + 7

  • 输入
  • Input end of EOF.
    First I will give you two positive integers N and M(0 < N, M < 10) means the number of items in polynomials.
    Then N lines follow, each line contains two integers c(-100 < c < 100) and p(-10 <= p <= 10), c means the coefficients and p means the power.
    Then M lines follow, each line contains two integers c(-100 < c < 100) and p(-10 <= p <= 10), c means the coefficients and p means the power.
    And for each test, every polynomial' s power is descending, it means pi > pj when i < j.
    c != 0 && p != 0.
  • 输出
  • You should output (f(x) + g(x))' s expression for coefficients and powers in descending.
  • 样例输入
  • 4 4
    2 5
    3 3
    7 0
    1 -1
    3 4
    2 3
    1 1
    -1 -1
    
  • 样例输出
  • 2 5
    3 4
    5 3
    1 1
    7 0
    
  • 提示
  • f(x) = coefficients * x ^ power;
    The sample input: 
    f(x) = 2*x^5 + 3*x^3 + 7 + x^(-1),
    g(x) = 3*x^4 + 2*x^3 + x - x^(-1).
    The sample output: 
    f(x) + g(x) = 2*x^5 + 3*x^4 + 5*x^3 + x + 7
    

题意:显而易见

思路:纯模拟,让下标确定为正数就是了

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
const int MAXN = 1000;

int n,m;
int arr[MAXN];
int vis[MAXN];

int main(){
	while (scanf("%d%d", &n, &m) != EOF){
		int c,p;
		memset(vis,0,sizeof(vis));
		for (int i = 0; i < n; i++){
			scanf("%d%d", &c, &p);
			arr[p+10] = c;
			vis[p+10] = 1;  //确保正数	
		}
		for (int i = 0; i < m; i++){
			scanf("%d%d", &c, &p);
			if (vis[p+10]){
				arr[p+10] += c;
				if (arr[p+10] == 0)
					vis[p+10] = 0;
			}
			else {
				vis[p+10] = 1;
				arr[p+10] = c;
			}
		}
		for (int i = 20; i >= 0; i--)
			if (vis[i])
				printf("%d %d\n", arr[i], i-10);
	}
	return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值