Polynomial addition Onge Teacher C Programming Cap 7th Chapter programming problem

Source: Internet
Author: User

Topic content:

A polynomial can be expressed as the sum of the powers of x multiplied by the coefficients, for example:

2x6+3x5+12x3+6x+20

Now, your program reads in two polynomial, then outputs the sum of the two polynomial, which adds the coefficients of the corresponding powers and outputs them.

The maximum power to be processed by the program is 100.

Input format:

To enter a total of two polynomial, the input format for each polynomial is as follows:

Enter two numbers per line, the first represents a power, the second represents the power factor, and all the coefficients are integers. The first line must be the highest power, and the last line must be power 0 times.

Note that the first and last lines are not necessarily in the order of power reduction, if the coefficient of a power is 0, it does not appear in the input data, and the coefficient of 0 power is 0 o'clock or it appears in the input data.

Output format:

Starting at the highest power, descending to a power of 0, such as:

2x6+3x5+12x3-6x+20

Note that x is a lowercase x, and there are no spaces between all the symbols, and there is no need for a power with a factor of 0.

Input Sample:

6 2

5 3

3 12

1 6

0 20

6 2

5 3

2 12

1 6

0 20

Sample output:

4x6+6x5+12x3+12x2+12x+40

time limit: 500ms memory limit: 32000kb
#include <stdio.h>inta[ the],b[ the];intMain () {intx, y;
while(~SCANF ("%d%d",&x,&y)) {A[x]=y; if(x==0) Break; } while(~SCANF ("%d%d",&x,&y)) {B[x]=y; if(x==0) Break; }
for(intI=0; i<= -; i++) A[i]=a[i]+B[i];
intflag=0;
for(intI= -;i>1; i--){ if(A[i]) {if(a[i]>0){ if(a[i]==1){ if(flag==0) {printf ("x%d", i); Flag=1; } Elseprintf"+x%d", i); } Else{ if(flag==0) {printf ("%dx%d", A[i],i); Flag=1; } Elseprintf"+%dx%d", A[i],i); } } Else{ if(a[i]==-1) printf ("-x%d", i); Elseprintf"%dx%d", A[i],i); } } }
if(a[1]){ if(a[1]>0){ if(a[1]==1){ if(flag==0) {printf ("x"); Flag=1; } Elseprintf"+x"); } Else{ if(flag==0) {printf ("%DX", a[1]); Flag=1; } Elseprintf"+%DX", a[1]); } } Else{ if(a[1]==-1) printf ("- x"); Elseprintf ("%DX", a[1]); } }
if(a[0]>0){ if(flag==0) {printf ("%d", a[0]); Flag=1; } Elseprintf ("+%d", a[0]); } if(a[0]<=0) {printf ("%d", a[0]); }
printf ("\ n"); return 0;}

Polynomial addition Onge Teacher C Programming Cap 7th Chapter programming problem

Related Article

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.