c++根据输入数据的小数位数输出数据

本题是多项式的相加,m,n分别代表两个多项式的项数。
This time, you are supposed to find A+B where A and B are two polynomials.
输入格式:
输入数据
输入样例:
2 1 2.4 0 3.2
2 2 1.5 1 0.5
输出样例:
3 2 1.5 1 2.9 0 3.2

#include <iostream>
#include <cstdio>
#include <string>
#include <iomanip>

using namespace std;

int main()
{
    int m,n;
    int t;float temp;
    string s;
    double exp[1001];
    int ans = 0;
    int x, y;
    for(int i = 0;i < 1001;i++){
        exp[i] = 0;
        }
    //
    scanf("%d", &m);
    cin >> t >> s;
    temp = 0;
    x = 0;
    y = 0;
    for(unsigned int i = 0;i < s.length();i++){
        if(s[i] == '.'){
            x = 1;
            }
        if(x == 1)
            y++;
        }
    y--;
    //获得小数点后的位数
    temp = atof(s.c_str());
    exp[t] += temp;
    m--;
    while(m != 0){
        scanf("%d %f", &t, &temp);
        m--;
        exp[t] += temp;
        }
    //cout << y;
    //
    scanf("%d", &n);
    while(n != 0){
        scanf("%d %f", &t, &temp);
        n--;
        exp[t] += temp;
        }
    for(int i = 0;i < 1001;i ++){
        if(exp[i] != 0)
            ans ++;
        }
    cout << ans;
    //
    for(int i = 1000;i >= 0 ;i --){
        if(exp[i] != 0){
            cout << " " << i << " " << fixed << setprecision(y) << exp[i];
            //输出时设置小数点后位数
            }
        }
    return 0;
}

转载请备注来源,谢谢!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值