(C++)A+B for Polynomials(構想と実現)
タイトルの説明
説明を入力:
出力の説明:
入力例:
出力例:
考え方:
実はこの問題は主に中の思想を理解する必要があって、配列を利用してこの多項式の問題を解決して、問題の中で要求する2つの多項式の和を求めます.を入力します.
まず入力から見ると、この要求入力は最初の整数で、最初の多項式が全部で何項あるかを示し、その後、各項の指数と係数を入力し、次に2番目の多項式も同じように入力します.このとき、私たちが求めているのは2つの多項式の和であることを知っておく必要があります.では、1つの配列だけではいけません.まず1つのkで最初の多項式の項数を保存し、その後、このkを巡回して最初の多項式の各値を入力し、この配列に保存し、配列の指数は配列の下付きであり、係数は配列の値である.では、2番目の多項式を入力すると、最初の配列に加算することができます.そうすれば.
最後に入力するには2つの複数の項目を出力する必要があります.加算された項目数と各項目の指数と係数です.だから、私は加算をするときに加算を行う必要があります.そして、加算するときは、この2つの値を加算して判断するしかありません.そしてこの配列をループします.
実装:
This time, you are supposed to find A+B where A and B are two polynomials.
説明を入力:
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.
出力の説明:
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.
入力例:
2 1 2.4 0 3.2
2 2 1.5 1 0.5
出力例:
3 2 1.5 1 2.9 0 3.2
考え方:
実はこの問題は主に中の思想を理解する必要があって、配列を利用してこの多項式の問題を解決して、問題の中で要求する2つの多項式の和を求めます.を入力します.
まず入力から見ると、この要求入力は最初の整数で、最初の多項式が全部で何項あるかを示し、その後、各項の指数と係数を入力し、次に2番目の多項式も同じように入力します.このとき、私たちが求めているのは2つの多項式の和であることを知っておく必要があります.では、1つの配列だけではいけません.まず1つのkで最初の多項式の項数を保存し、その後、このkを巡回して最初の多項式の各値を入力し、この配列に保存し、配列の指数は配列の下付きであり、係数は配列の値である.では、2番目の多項式を入力すると、最初の配列に加算することができます.そうすれば.
最後に入力するには2つの複数の項目を出力する必要があります.加算された項目数と各項目の指数と係数です.だから、私は加算をするときに加算を行う必要があります.そして、加算するときは、この2つの値を加算して判断するしかありません.そしてこの配列をループします.
実装:
#include
const int max_n = 1111;
double a[max_n] = {};
int main(){
int k, n, count = 0;
double e;
scanf("%d", &k);
for(int i = 0; i < k; i ++){
scanf("%d%lf", &n, &e);
a[n] = e;
}
scanf("%d", &k);// k , k 。
for(int i = 0; i < k; i ++){
scanf("%d%lf", &n, &e);
a[n] += e;// ,
}
for(int i = 0; i < max_n; i ++){
if(a[i] != 0){
count ++;
}
}
printf("%d", count);
for(int i = max_n - 1; i >=0; i --){
if(a[i] != 0) printf(" %d %.1f", i, a[i]);
}
return 0;
}