1002 数组

1.数组初始化
2.保留小数

#include
#include
using namespace std;
int main() {
    int m, n;
    float a[1001] = { 0.0 };
    float coe;
    int exp;
    cin >> m;
    for (int i = 0;i < m;i++) {
        cin >> exp >> coe;
        a[exp] += coe;
    }
    cin >> n;
    for (int i = 0;i < n;i++) {
        cin >> exp >> coe;
        a[exp] += coe;
    }
    int res = 0;
    for (int i = 0;i < 1001;i++) {
        if (a[i] != 0.0)
            res++;
    }
    cout << res;
    for (int i = 1000;i >= 0;i--) {
        if (a[i] != 0.0)
            cout << " " << i << " " << setprecision(1) << std::fixed << a[i];
    }
    return 0;
}

你可能感兴趣的:(pat甲级)