算法趣谈--二项式展开

package TUZI;

import java.io.IOException;
import java.util.Scanner;

public class Yanghuisanjiao {
	
	static int a;
	static int b;
	static int power;
	
	static class Term{
		
		int power_a;
		int power_b;
		int ratio_term;
		
		public Term(int power_a, int power_b, int ratio_term) {
			super();
			this.power_a = power_a;
			this.power_b = power_b;
			this.ratio_term = ratio_term;
		}
				
	}
	
	@SuppressWarnings("null")
	public static void main(String[] args) throws IOException {
		
		System.out.println("请输入a+b的次方数:");
		Scanner scanner  = new Scanner(System.in);
		power = scanner.nextInt();
		
		Term[] answer = new Term[power+1];
		answer[0] = new Term(0,0,1);
		
		for(int i=1; i <= power;i++){
			answer[0] = new Term(0, i, 1);
			answer[i] = new Term(i, 0,1);
			System.out.print("(a+b)^ "+i+" = a^"+answer[0].power_a+"*b^"+answer[0].power_b+" + ");
			//首&未不用算,系数
			for(int j=i-1;j>0;j--){
			   answer[j].ratio_term =  answer[j-1].ratio_term + answer[j].ratio_term;
			   answer[j].power_a = j;
			   answer[j].power_b = i -j;
			   System.out.print( " "+answer[j].ratio_term+"a^"+answer[j].power_a+"*b^"+answer[j].power_b+" + ");
			}
			System.out.println("a^"+answer[i].power_a+"*b^"+answer[i].power_b);
		}
	}

}

 

你可能感兴趣的:(ACM趣谈)