欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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

程序员文章站 2024-01-19 13:02:22
...

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

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

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 + "}";
		}
	}

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

public class Polynmial {

	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 + "}";
		}
	}
	
	private List<Polynmial.Item> elems = new ArrayList<Polynmial.Item>();
	
	public int size() {
		return elems.size();
	}
	
	public String toString() {
		StringBuffer buf = new StringBuffer();
		for(int i = 0; i < elems.size(); i++) {
			if(i == 0) {
				buf.append("{");
			}else {
				buf.append(", ");
			}
			buf.append(elems.get(i));
		}
		buf.append("}");
		return buf.toString();		
	}
	
	public Polynmial add(int exponential, int coefficient) {
		Item elem = new Item(exponential, coefficient);
		add(elem);
		return this;
	}
	
	public Polynmial remove(int exponential) {
		for(Item elem: elems) {
			if(elem.getExponential() == exponential) {
				elems.remove(elem);
				return this;
			}
		}
		return this;
	}
	
	private void add(Item elem) {
		if(elems.size() == 0) {
			elems.add(0, elem);
			return;
		}
		for(int i = 0; i < elems.size(); i++) {
			Item e = elems.get(i);
			if(elem.getExponential() < e.getExponential()) {
				elems.add(i, elem);
				return;
			}
		}
		elems.add(elem);
	}
	
	public Polynmial plus(Polynmial p) {
		int 
			idx1 = 0,
			idx2 = 0;
		while(idx1 < elems.size() && idx2 < p.elems.size()) {
			Item 
				e1 = elems.get(idx1),
				e2 = p.elems.get(idx2);
			if(e1.getExponential() < e2.getExponential()) {
				++idx1;
			}else if(e1.getExponential() == e2.getExponential()) {
				int coefficient = e1.getCoefficient() + e2.getCoefficient();
				if(coefficient == 0) {
					remove(idx1);
				}else {
					e1.setCoefficient(coefficient);
				}
				++idx1;
				++idx2;
			}else {
				add(e2);
				++idx2;
			}	
		}
		while(idx2 < p.elems.size()) {
			Item e2 = p.elems.get(idx2);
			add(e2);
			++idx2;
		}
		return this;
	}
	
	public Polynmial minus(Polynmial p) {
		int 
			idx1 = 0,
			idx2 = 0;
		while(idx1 < elems.size() && idx2 < p.elems.size()) {
			Item 
				e1 = elems.get(idx1),
				e2 = p.elems.get(idx2);
			if(e1.getExponential() < e2.getExponential()) {
				++idx1;
			}else if(e1.getExponential() == e2.getExponential()) {
				int coefficient = e1.getCoefficient() - e2.getCoefficient();
				if(coefficient == 0) {
					remove(idx1);
				}else {
					e1.setCoefficient(coefficient);
				}
				++idx1;
				++idx2;
			}else {
				e2.setCoefficient(-e2.getCoefficient());
				add(e2);
				++idx2;
			}	
		}
		while(idx2 < p.elems.size()) {
			Item e2 = p.elems.get(idx2);
			e2.setCoefficient(-e2.getCoefficient());
			add(e2);
			++idx2;
		}
		return this;
	}
}

 

 

            

相关标签: 算法 数据结构