Addition of a sparse Polynomial

Source: Internet
Author: User

Description:

Design a one-dimensional sparse polynomial addition generator to complete the addition of polynomials A and B, and establish a polynomial A + B.

Input description:

A group of input data, all of which are integers. 1st behavior 2 positive integers n, m, where n represents the number of items in the first polynomial, m represents the number of items in the second polynomial, and 2nd rows contain 2n integers, each two integers represents the coefficients and exponent of each of the first polynomials, and the second row contains 2 m integers, each representing the coefficients and exponent of each of the second polynomials. (Note: The sequence is sorted in exponential Ascending Order)

Output description:

Output results in a row in polynomial-like form, and the index is in the ascending order. Note that the output form of a non-zero entry with a value of 1 is omitted from the coefficient 1. For example, the output form of 1x ^ 2 is x ^ 2, the output format of-1 x ^ 2 is-x ^ 2.

Input example:

6 2

1 0 1 1 1 2 1 3 1 4 2 5

-1 3-2 4

Output example:

1 + x ^ 2-x ^ 4 + 2x ^ 5

 

The code I wrote is as follows:

1 # include <stdio. h> 2 # include <string. h> 3 int main () 4 {5 Int A1 [1000]; 6 int A2 [1000]; 7 memset (A1, 0, sizeof (A1 )); // Initialize all elements of the A1 array to 0 8 memset (A2, 0, sizeof (A2); // Initialize all elements of the A2 array to 0 9 int n, m; 10 int T1, T2; // take two numbers each time, T1 is the coefficient, T2 is the corresponding index 11 scanf ("% d", & N, & M ); 12 For (INT I = 0; I <n; I ++) // Save the first formula in A1. Each element of A1 represents an item of the formula, 13 {// The element value is the coefficient of the item, and the element subscript is the index of the item 14 scanf ("% d", & T1, & T2 ); 15 A1 [T2] = T1; 16} 17 for (INT I = 0; I <m; I ++) // The second sub-statement is saved in A2 18 {19 scanf ("% d", & T1, & T2); 20 A2 [T2] = T1; 21} 22 bool flag = true; // flag indicates whether the current item is the first item of the "sum, when the flag is true, it indicates that the current item is the first item of the "sum" 23 for (INT I = 0; I <1000; I ++) // I indicates the index of the item, scan 24 {25 int K = A1 [I] + A2 [I] from 0; // K is the sum of the coefficients of the items whose exponent is I in the two formulas, and 26 if (K! = 0) // The sum contains 27 items with an index of I {28 if (I = 0) // if the index of this item is 029 printf ("% d ", k); 30 else if (I = 1) // if the index of this item is 131 {32 If (k =-1) // if the index of this item is 1, and the coefficient is-133 printf ("-X"); 34 else if (k = 1) // if the index of this item is 1, and the coefficient is 135 flag? Printf ("X"): printf ("+ X"); 36 else // if the index of this item is 1, the coefficient is not-1 or 137! Flag & K> 0? Printf ("+ % dx", k): printf ("% dx", k ); 38} 39 else // if the index of this item is greater than 140 {41 if (k =-1) // if the index of this item is greater than 1, and the coefficient is-142 printf ("-x ^ % d", I); 43 else if (k = 1) // if the index of this item is greater than 1, and the coefficient is 144 flag? Printf ("x ^ % d", I): printf ("+ x ^ % d", I); 45 else // if the index of this item is greater than 1, and the coefficient is not-1 or 146! Flag & K> 0? Printf ("+ % DX ^ % d", K, I): printf ("% DX ^ % d", K, I); 47} 48 flag = false; 49} 50} 51 printf ("\ n"); 52 return 0; 53}

******

Addition of a sparse Polynomial

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.