字符串解析
问题描述:
代码实现:
1 package xiaoMiBishi_2015; 2 3 import java.util.*; 4 5 public class xiaoMi_2015 { 6 7 public static void main(String[] args){ 8 int[] A,B,Key; String t_source; 9 int max1,max2; //记录x幂的最大值 10 max1=0; max2=0; 11 Scanner b=new Scanner(System.in); 12 t_source=b.nextLine(); 13 b.close(); 14 A=new int[100]; 15 B=new int[100]; 16 Key=new int[100]; 17 for( int i=0;i<100;i++) 18 {A[i]=0;B[i]=0;Key[i]=0;} 19 String[] T=t_source.split(" "); 20 String[] t=T[0].split(","); 21 String[] u=T[1].split(","); 22 23 for(int i=0;i<u.length;i=i+2){ //取得每个i次幂的值放入A[i] 24 u[i]=u[i].substring(1, u[i].length()); 25 u[i+1]=u[i+1].substring(0, u[i+1].length()-1); 26 int n=Integer.parseInt(u[i]); 27 B[n]=Integer.parseInt(u[i+1]); 28 if(n>=max2) 29 max2=n; 30 } 31 32 for(int i=0;i<t.length;i=i+2){ //取得每个i次幂的值放入B[i] 33 t[i]=t[i].substring(1, t[i].length()); 34 t[i+1]=t[i+1].substring(0, t[i+1].length()-1); 35 int n=Integer.parseInt(t[i]); 36 A[n]=Integer.parseInt(t[i+1]); 37 if(n>=max1) 38 max1=n; 39 } 40 41 //System.out.print(max1); 42 //System.out.print(max2); 43 44 for(int p=0;p<=max1+max2;p++){ //两者想乘整合 45 46 for(int q=0;q<=p;q++){ 47 48 Key[p]=Key[p]+A[q]*B[p-q]; 49 50 } 51 52 System.out.println(Key[p]); 53 } 54 55 System.out.println("结果是:"); //输出结果 56 int n=0; 57 for(int i=max1+max2;i>=0;i--){ 58 59 if(Key[i]!=0){ 60 if(n!=0 && Key[i]>0) 61 System.out.print("+"); 62 System.out.print(Key[i]); 63 System.out.print("x^"); 64 System.out.print(i); 65 n++; 66 } 67 68 } 69 } 70 71 }
运行效果: