本題是多項式的相加,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;
}
轉載請備注來源,謝謝!