天天看点

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

本题是多项式的相加,m,n分别代表两个多项式的项数。

This time, you are supposed to find A+B where A and B are two polynomials.

输入格式:

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

输入样例:

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;
}
           

转载请备注来源,谢谢!

继续阅读