Home > Back-end >  Polynomial addition China university mooc WengKai "zero based learning Java language" week
Polynomial addition China university mooc WengKai "zero based learning Java language" week

Time:09-24

Package weqeeq;
import java.util.Scanner;

Public class Practice1 {

Static int k, h, f;

Public static void main (String [] args) {

Scanner sc=new Scanner(System.in);

Int [] ans=new int [101].//the subscript corresponds to the exponential, content corresponding coefficient

Int [] [] ploy1=new int [101] [2].//in the first round of the input to zero for globe
Int [] [] ploy2=new int [101] [2].//the second input to 0 for globe

Int [] cunm1=new int [102].//to store all the exponential type
Int [] cunm2=new int [102].//will all power from big to small store

int times;//before each factor
Int pow.//the exponential

Int cnt_1=0;
While (true) {
Pow=sc. NextInt ();
Times=sc. NextInt ();
Ploy1 [cnt_1] [0]=pow;
Ploy1 [cnt_1] [1]=times;
Cnt_1 + +;
If (pow==0)
break;
}
Int cnt_2=0;
While (true) {
Pow=sc. NextInt ();
Times=sc. NextInt ();
Ploy2 [cnt_2] [0]=pow;
Ploy2 [cnt_2] [1]=times;
Cnt_2 + +;
If (pow==0)
break;
}
For (int I=0; I & lt; Cnt_1; I++) {
Ans [ploy1 [I] [0]] +=ploy1 [I] [1];
Cunm1 [I]=ploy1 [I] [0];
K=I;
}
For (int I=0; I & lt; Cnt_2; I++) {

for(int j=0; jAns [ploy2 [I] [0]] +=ploy2 [I] [1];
}
Cunm2 [0]=cunm1 [0];
H=0;
F=1;
TT:
for(; H
For (int j=h + 1; j> 0; J -) {
If (cunm2 [j]==cunm1 [h]) continue TT;
}
Cunm2 [f]=cunm1 [h];
F++;
}

for(int j=0; j<101; J++)
for(int i=1; i<=cunm2. Length - 2; I++) {
If (cunm2 [I] Int temp=cunm2 [I];
Cunm2 [I]=cunm2 [I + 1);
Cunm2 [I + 1)=temp;
}
}

String resurt="";

for(int i=1; i<=cunm2. Length - 2; I++) {
If (ans [cunm2 [I]].=0)
If (cunm2 [I]!=0) {
If (cunm2 [I]!=1) {

If (ans [cunm2 [I]]==1) {

String temp="x" + cunm2 [I];

If (ans [cunm2 + 1] [I] <0);
Else if (ans [cunm2 + 1] [I] & gt; 0) temp=temp + "+";
Resurt=resurt + temp; }

Else if (ans [cunm2 [I]]==1) {
String temp="x" + cunm2 [I];

If (ans [cunm2 + 1] [I] <0);
Else if (ans [cunm2 + 1] [I] & gt; 0) temp=temp + "+";
Resurt=resurt + temp;
}
The else {
String temp=ans [cunm2 [I]] + "x" + cunm2 [I];
If (ans [cunm2 + 1] [I] <0);
Else if (ans [cunm2 + 1] [I] & gt; 0) temp=temp + "+";
Resurt=resurt + temp;
}
}
}
}

If (ans [1].=0) if (ans [0] & gt;=0)
System. The out. Println (resurt=resurt + ans [1] + x + "" + ans [0]).
The else System. Out. Println (resurt=resurt + ans [1] + "x" + ans [0]).
Else if (ans [0]==0 & amp; & Resurt!="") System. The out. Println (resurt=resurt +" + "+ ans [0]).
The else System. Out. Println (resurt=resurt + ans [0]).
}
}
  • Related