java算法:基于应用ADT例子

java算法:基于应用ADT例子

例1:多项式ADT接口

Java代码 复制代码
  1. classPoly{
  2. Poly(int,int)
  3. doubleeval(double)
  4. voidadd(Poly)
  5. voidmult(Poly)
  6. publicStringtoString()
  7. }
class Poly{
	Poly(int,int)
	double eval(double)
	void add(Poly)
	void mult(Poly)
	public String toString()
}

例2:多项式客户程序

Java代码 复制代码
  1. publicclassBinomial{
  2. publicstaticvoidmain(Stringargs[]){
  3. intN=100;
  4. doublep=1.1;
  5. Polyy=newPoly(1,0);
  6. Polyt=newPoly(1,0);
  7. t.add(newPoly(1,1));
  8. for(inti=0;i<N;i++){
  9. y.mult(t);
  10. System.out.println(y+"");
  11. }
  12. System.out.println("value:"+y.eval(p));
  13. }
  14. }
public class Binomial{
	public static void main(String args[]){
		int N = 100;
		double p = 1.1;
		Poly y = new Poly(1,0);
		Poly t = new Poly(1,0);
		t.add(new Poly(1,1));
		for(int i = 0; i < N; i++){
			y.mult(t);
			System.out.println(y + " ");
		}
		System.out.println("value: " + y.eval(p));
	}
}

例3:多项式ADT的数组实现

Java代码 复制代码
  1. classPoly{
  2. privateintn;
  3. privateint[]a;
  4. Poly(intc,intN){
  5. a=newint[N+1];
  6. n=N+1;
  7. a[N]=c;
  8. for(inti=0;i<N;i++){
  9. a[i]=0;
  10. }
  11. }
  12. doubleeval(doubled){
  13. doublet=0.0;
  14. for(inti=n-1;i>=0;i--){
  15. t=t*d+(double)a[i];
  16. }
  17. returnt;
  18. }
  19. voidadd(Polyp){
  20. int[]t=newint[(p.n>n)?p.n:n];
  21. for(inti=0;i<p.n;i++){
  22. t[i]=p.a[i];
  23. }
  24. for(intj=0;j<n;j++){
  25. t[j]+=a[j];
  26. }
  27. a=t;
  28. n=t.length;
  29. }
  30. voidmult(Polyp){
  31. int[]t=newint[p.n+n-1];
  32. for(inti=0;i<p.n;i++){
  33. for(intij=0;j<n;j++){
  34. t[i+j]+=p.a[i]*a[j];
  35. }
  36. }
  37. a=t;
  38. n=t.length;
  39. }
  40. publicStringtoString(){
  41. Strings="";
  42. for(inti=0;i<n;i++){
  43. s+=a[i]+"";
  44. }
  45. returns;
  46. }
  47. }
class Poly{
	private int n;
	private int[] a;
	Poly(int c,int N){
		a = new int[N+1]; 
		n = N + 1; 
		a[N] = c;
		for(int i = 0; i < N; i++){
			a[i] = 0;
		}
	}
	double eval(double d){
		double t = 0.0;
		for(int i = n - 1; i >= 0; i--){
			t = t * d + (double)a[i];
		}
		return t;
	}
	void add(Poly p){
		int[] t = new int[(p.n > n) ? p.n : n];
		for(int i = 0; i < p.n; i++){
			t[i] = p.a[i];
		}
		for(int j = 0; j < n; j++){
			t[j] += a[j];
		}
		a = t;
		n = t.length;
	}
	void mult(Poly p){
		int[] t = new int[p.n + n -1];
		for(int i = 0; i < p.n; i++){
			for(inti j = 0; j < n; j++){
				t[i+j] += p.a[i] * a[j];
			}
		}
		a = t;
		n = t.length;
	}
	public String toString(){
		String s = "";
		for(int i = 0; i < n; i++){
			s += a[i] + " ";
		}
		return s;
	}
}

注意:Horner算法:a4x4 + a3x3 + a2x2 + a1x + a0 = (((a4x + a3)x + a2)x + a1)x + a0

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值