标签:
注意输入输出的:
1,输入小数的语句 scanf("%lf", &a)
2,输出一位小数的语句 printf("%.1f", count);
This time, you are supposed to find A+B where A and B are two polynomials.
Input
Each input file contains one test case. Each case occupies 2 lines, and each line contains the information of a polynomial: K N1 aN1 N2 aN2 ... NK aNK, where K is the number of nonzero terms in the polynomial, Ni and aNi (i=1, 2, ..., K) are the exponents and coefficients, respectively. It is given that 1 <= K <= 10,0 <= NK < ... < N2 < N1 <=1000.
Output
For each test case you should output the sum of A and B in one line, with the same format as the input. Notice that there must be NO extra space at the end of each line. Please be accurate to 1 decimal place.
Sample Input2 1 2.4 0 3.2 2 2 1.5 1 0.5Sample Output
3 2 1.5 1 2.9 0 3.2
#pragma warning(disable:4996)
/**/
#include <stdio.h>
using namespace std;
int main(void){
int k1 = 0, k2 = 0;
int ak1[1001] = { 0 }, ak2[1001] = { 0 };//项数
double nk1[1001] = { 0.0 }, nk2[1001] = { 0.0 };//系数
scanf("%d", &k1);
for (int i = 0; i < k1; i++){
scanf("%d", &ak1[i]);
scanf("%lf", &nk1[i]);
}
scanf("%d", &k2);
for (int j = 0; j < k2; j++){
scanf("%d", &ak2[j]);
scanf("%lf", &nk2[j]);
}
double c[1001] = {0};
for (int i = 0; i < k1; i++){
c[ak1[i]] += nk1[i];
}
for (int j = 0; j < k2; j++){
c[ak2[j]] += nk2[j];
}
int count = 0;
for (int k = 0; k <= 1000; k++){
if (c[k] != 0)
count++;
}
printf("%d", count);
for (int q = 1000; q >= 0; q--){
if (c[q] != 0)
printf(" %d %.1f", q, c[q]);
}
return 0;
}
#pragma warning(disable:4996)
#include <stdio.h>
using namespace std;
int main(void) {
int k1 = 0, k2 = 0;
k2 = 0;
int n1[1001] = { 0 }, n2[1001] = { 0 };
double a1[1001] = { 0.0 }, a2[1001] = { 0.0 };
int i = 0;
scanf("%d", &k1);
for (i = 0; i < k1; i++) {
scanf("%d", &n1[i]);
scanf("%lf", &a1[i]);////not number 1
}
i = 0;
scanf("%d", &k2);
for (i = 0; i < k2; i++) {
scanf("%d", &n2[i]);
scanf("%lf", &a2[i]);
}
double c[1001] = { 0 };
for (i = 0; i < k1; i++) {
c[n1[i]] += a1[i];
}
for (i = 0; i < k2; i++) {
c[n2[i]] += a2[i];
}
int count = 0;
for (i = 0; i < 1001; i++) {
if (c[i] != 0)
count++;
}
printf("%d", count);
for (i = 1000; i >= 0; i--) {
if (c[i] != 0) //****
printf(" %d %.1f", i, c[i]);
}
return 0;
}
1002. A+B for Polynomials (25)
标签:
原文地址:http://www.cnblogs.com/zzandliz/p/5023027.html