码迷,mamicode.com
首页 > 其他好文 > 详细

一元多项式的乘法和加法

时间:2019-01-15 20:31:40      阅读:200      评论:0      收藏:0      [点我收藏+]

标签:多项式   close   out   oid   node   java   而不是   while   index   

//一元多项式的乘法和加法

import java.util.*;

class Node{
 int index;
 int coef;
 Node next = null;
    public Node(int coef, int index) {
     this.index = index;
     this.coef = coef;
    }
}

class Link{
 Node head = null;
 Node tmp = null;
 
 public void addnode(int coef,int index) {
  Node n = new Node(coef,index);
  if(head == null) {
   head = n;
   tmp = head;
   return;
  }
  tmp.next = n;
   while(tmp.next != null) {
    tmp = tmp.next;
   }
 }
 
 public Link pluslink(Link a,Link b) {
  Link c = new Link();
  a.tmp = a.head;
  b.tmp = b.head;

  while(a.tmp != null && b.tmp !=null) {
   if(a.tmp.index == b.tmp.index) {
    if(a.tmp.coef+b.tmp.coef!=0) {
    c.addnode(a.tmp.coef+b.tmp.coef,a.tmp.index);}
    a.tmp = a.tmp.next;
    b.tmp = b.tmp.next;
    
   }
   else if(a.tmp.index > b.tmp.index) {
    c.addnode( a.tmp.coef,a.tmp.index);
    a.tmp = a.tmp.next;
   }
   else if(a.tmp.index < b.tmp.index) {
    c.addnode(b.tmp.coef,b.tmp.index);
    b.tmp = b.tmp.next;
   }
  }
  if(a.tmp == null) {
   while(b.tmp != null) {
    c.addnode(b.tmp.coef,b.tmp.index);
    b.tmp = b.tmp.next;
   }
  }
  else if(b.tmp == null) {
   while(a.tmp != null) {
    c.addnode(a.tmp.coef,a.tmp.index);
    a.tmp = a.tmp.next;
   }
  }
  return c;
  
 }
 
 public Link multiplelink(Link a,Link b) {
  a.tmp = a.head;
  b.tmp = b.head;
  Link m = new Link();
  while(a.tmp != null) {
   Link l = new Link();
   while(b.tmp !=null) {
   l.addnode(a.tmp.coef*b.tmp.coef, a.tmp.index+b.tmp.index);
   b.tmp = b.tmp.next;
   }
   m=pluslink(m, l);
   a.tmp = a.tmp.next;
   b.tmp = b.head;
     
  }
  return m;
 }
 
 public void printlink(Link a) {
  a.tmp = a.head;
  int n = 0;
  if(a.tmp == null)
  {System.out.print("0 0");}
  else{
   while(a.tmp != null) {
    if(a.tmp.coef != 0) {
     System.out.print(a.tmp.coef +" "+a.tmp.index);
     n++;
        a.tmp = a.tmp.next;
        {while(a.tmp != null) {
         if(a.tmp.coef != 0) {
         System.out.print(" "+a.tmp.coef +" "+a.tmp.index);
         n++;
         }
         a.tmp = a.tmp.next;
         
        }break;
        }
       
    }
    else {a.head = a.tmp;}
    a.tmp = a.tmp.next;
   }
     if(n==0) {
      System.out.print("0 0");
     }
  }
  
 }
}
public class Main {

 public static void main(String[] args) {
  Scanner in = new Scanner(System.in);
  Link s1 = new Link();
  int N1=in.nextInt();
  for(int n = N1; n>0; n--) {
   s1.addnode(in.nextInt(), in.nextInt());
  }
  Link s2 = new Link();
  int N2=in.nextInt();
  for(int n =N2; n>0; n--) {
   s2.addnode(in.nextInt(), in.nextInt());
  }
  Link s3 = new Link();
  
  
  if(N1>=N2){
  s3.printlink(s3.multiplelink(s2, s1));
  }
  else {
  s3.printlink(s3.multiplelink(s1, s2)); 
  }
  
  System.out.println();
  s3.printlink(s3.pluslink(s1, s2));

  in.close();

 }

}

1.判断是否是零多项式可以在加法时判断,我是在输出时判断的,应该在加法处判断比较简单;

2.输入零多项式和常数多项式中的零多项式就是不输入(。。)而不是输入0 0。

 

一元多项式的乘法和加法

标签:多项式   close   out   oid   node   java   而不是   while   index   

原文地址:https://www.cnblogs.com/dyq19/p/10274042.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!