一元多项式的存储和其算法运算

关键字: 线性结构

      线性结构可以存储一元多项式。一元多项式是由n + 1 个系数唯一确定,那么我可以将系数存放在线性表中,指数隐含在线性表的序号中。即线性表第一个元素存放指数0 的系数,线性表第二个元素存放指数1 的系数。。。以此类推线性表第n 个元素存放指数n - 1的系数。然而这样的存储方式存在一个空间浪费的问题,当指数有20 000 时但是多项式却只有三项,但我们却需要存储20 001项数据。

      那么我们该如何进行改进呢?我们可以通过两张线性表进行存储一元多项式即可。即一张线性表存放指数,另一张线性表存放系数。其实这很像Java Map容器,它是以键值对存储的,我们可以看做指数是键值,系数是值。由于指数和系数都是整型如果我们使用Map 作为存储使用起来并不方便,因此我们可以创建一个Item 类,代码如下:

Java代码 复制代码  收藏代码
  1. public class Item {   
  2.            
  3.         private int exponential;   
  4.            
  5.         private int coefficient;   
  6.   
  7.         public Item(int exponential, int coefficient) {   
  8.             this.exponential = exponential;   
  9.             this.coefficient = coefficient;   
  10.         }   
  11.            
  12.         public int getCoefficient() {   
  13.             return coefficient;   
  14.         }   
  15.   
  16.         public void setCoefficient(int coefficient) {   
  17.             this.coefficient = coefficient;   
  18.         }   
  19.   
  20.         public int getExponential() {   
  21.             return exponential;   
  22.         }   
  23.   
  24.         public void setExponential(int exponential) {   
  25.             this.exponential = exponential;   
  26.         }   
  27.            
  28.         public String toString() {   
  29.             return "{" + exponential + ", " + coefficient + "}";   
  30.         }   
  31.     }  
public class Item {
		
		private int exponential;
		
		private int coefficient;

		public Item(int exponential, int coefficient) {
			this.exponential = exponential;
			this.coefficient = coefficient;
		}
		
		public int getCoefficient() {
			return coefficient;
		}

		public void setCoefficient(int coefficient) {
			this.coefficient = coefficient;
		}

		public int getExponential() {
			return exponential;
		}

		public void setExponential(int exponential) {
			this.exponential = exponential;
		}
		
		public String toString() {
			return "{" + exponential + ", " + coefficient + "}";
		}
	}

这样在使用起来就比较方便了。接下来我们需要解决的是一元多项式的算法问题,由于算法比较简单这里不做解释,直接将代码附上,请自行理解:)。代码如下:

Java代码 复制代码  收藏代码
  1. public class Polynmial {   
  2.   
  3.     public class Item {   
  4.            
  5.         private int exponential;   
  6.            
  7.         private int coefficient;   
  8.   
  9.         public Item(int exponential, int coefficient) {   
  10.             this.exponential = exponential;   
  11.             this.coefficient = coefficient;   
  12.         }   
  13.            
  14.         public int getCoefficient() {   
  15.             return coefficient;   
  16.         }   
  17.   
  18.         public void setCoefficient(int coefficient) {   
  19.             this.coefficient = coefficient;   
  20.         }   
  21.   
  22.         public int getExponential() {   
  23.             return exponential;   
  24.         }   
  25.   
  26.         public void setExponential(int exponential) {   
  27.             this.exponential = exponential;   
  28.         }   
  29.            
  30.         public String toString() {   
  31.             return "{" + exponential + ", " + coefficient + "}";   
  32.         }   
  33.     }   
  34.        
  35.     private List<Polynmial.Item> elems = new ArrayList<Polynmial.Item>();   
  36.        
  37.     public int size() {   
  38.         return elems.size();   
  39.     }   
  40.        
  41.     public String toString() {   
  42.         StringBuffer buf = new StringBuffer();   
  43.         for(int i = 0; i < elems.size(); i++) {   
  44.             if(i == 0) {   
  45.                 buf.append("{");   
  46.             }else {   
  47.                 buf.append(", ");   
  48.             }   
  49.             buf.append(elems.get(i));   
  50.         }   
  51.         buf.append("}");   
  52.         return buf.toString();         
  53.     }   
  54.        
  55.     public Polynmial add(int exponential, int coefficient) {   
  56.         Item elem = new Item(exponential, coefficient);   
  57.         add(elem);   
  58.         return this;   
  59.     }   
  60.        
  61.     public Polynmial remove(int exponential) {   
  62.         for(Item elem: elems) {   
  63.             if(elem.getExponential() == exponential) {   
  64.                 elems.remove(elem);   
  65.                 return this;   
  66.             }   
  67.         }   
  68.         return this;   
  69.     }   
  70.        
  71.     private void add(Item elem) {   
  72.         if(elems.size() == 0) {   
  73.             elems.add(0, elem);   
  74.             return;   
  75.         }   
  76.         for(int i = 0; i < elems.size(); i++) {   
  77.             Item e = elems.get(i);   
  78.             if(elem.getExponential() < e.getExponential()) {   
  79.                 elems.add(i, elem);   
  80.                 return;   
  81.             }   
  82.         }   
  83.         elems.add(elem);   
  84.     }   
  85.        
  86.     public Polynmial plus(Polynmial p) {   
  87.         int    
  88.             idx1 = 0,   
  89.             idx2 = 0;   
  90.         while(idx1 < elems.size() && idx2 < p.elems.size()) {   
  91.             Item    
  92.                 e1 = elems.get(idx1),   
  93.                 e2 = p.elems.get(idx2);   
  94.             if(e1.getExponential() < e2.getExponential()) {   
  95.                 ++idx1;   
  96.             }else if(e1.getExponential() == e2.getExponential()) {   
  97.                 int coefficient = e1.getCoefficient() + e2.getCoefficient();   
  98.                 if(coefficient == 0) {   
  99.                     remove(idx1);   
  100.                 }else {   
  101.                     e1.setCoefficient(coefficient);   
  102.                 }   
  103.                 ++idx1;   
  104.                 ++idx2;   
  105.             }else {   
  106.                 add(e2);   
  107.                 ++idx2;   
  108.             }      
  109.         }   
  110.         while(idx2 < p.elems.size()) {   
  111.             Item e2 = p.elems.get(idx2);   
  112.             add(e2);   
  113.             ++idx2;   
  114.         }   
  115.         return this;   
  116.     }   
  117.        
  118.     public Polynmial minus(Polynmial p) {   
  119.         int    
  120.             idx1 = 0,   
  121.             idx2 = 0;   
  122.         while(idx1 < elems.size() && idx2 < p.elems.size()) {   
  123.             Item    
  124.                 e1 = elems.get(idx1),   
  125.                 e2 = p.elems.get(idx2);   
  126.             if(e1.getExponential() < e2.getExponential()) {   
  127.                 ++idx1;   
  128.             }else if(e1.getExponential() == e2.getExponential()) {   
  129.                 int coefficient = e1.getCoefficient() - e2.getCoefficient();   
  130.                 if(coefficient == 0) {   
  131.                     remove(idx1);   
  132.                 }else {   
  133.                     e1.setCoefficient(coefficient);   
  134.                 }   
  135.                 ++idx1;   
  136.                 ++idx2;   
  137.             }else {   
  138.                 e2.setCoefficient(-e2.getCoefficient());   
  139.                 add(e2);   
  140.                 ++idx2;   
  141.             }      
  142.         }   
  143.         while(idx2 < p.elems.size()) {   
  144.             Item e2 = p.elems.get(idx2);   
  145.             e2.setCoefficient(-e2.getCoefficient());   
  146.             add(e2);   
  147.             ++idx2;   
  148.         }   
  149.         return this;   
  150.     }   
  151. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值