⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 triangle.java

📁 用java实现几个算法和应用程序,有一定的参考价值
💻 JAVA
字号:

package SunFa;
import java.awt.*;

import TriFrame.TriFrame;

public class Triangle{
	protected int count; //三解形的行娄
	protected int[][] triangle; //保存三角形的内容
	protected  int[][] child; //记录被选择的路径,只需要记录其子结点即可
	protected int sum;  //
	protected TriFrame tf;
	protected int SLEEPTIME;
	//TriFrame tf = new TriFrame();
	
	public Triangle(int num,int[][] arr,int sleepTime,TriFrame tf){
		this.tf = tf;
		this.count = num;
		triangle = new int[count][count];
		SLEEPTIME =  sleepTime;
		for(int i=0;i<arr.length;i++){
			for(int j=0;j<=i;j++){
				triangle[i][j]=arr[i][j];
			}
		}
		child = new int[count][count];
		for(int i=0;i<count;i++){
			for(int j=0;j<count;j++){
				child[i][j]=-1;
			}
		}
		//this.child = arr;
		//sum = MaxTri();
	}
	
	public int MaxTri(Graphics g){
		//g=this.g
		//Graphics g;
		//Graphics g = new Graphics;
		for(int row=count-2;row>=0;row--){
			for(int col=0;col<=row;col++){
				if(triangle[row+1][col]>=triangle[row+1][col+1]){
					triangle[row][col]+=triangle[row+1][col];
					child[row][col] =col;
					
					//tf.drawChild(g,getChild());
					System.out.println("正在调用MaxTri");
					//ThreadSleep();
					tf.drawChild(g);
					//tf.repaint();
					ThreadSleep();
					//ThreadSleep();
					//tf.repaint();
					//tf.drawChild(g);
					//tf.drawChild(g);
					
					
				}
				else{
					triangle[row][col]+=triangle[row+1][col+1];
					child[row][col] =col+1;
					//ThreadSleep();
					//tf.drawChild(g,getChild());
					//ThreadSleep();
					tf.drawChild(g);
					//tf.repaint();
					ThreadSleep();
					System.out.println("正在调用MaxTri");
				}
			}//for
		}//for
		
		//tf.drawChild(g,getChild());
		//ThreadSleep();
		tf.drawChild(g);
		//tf.repaint();
		ThreadSleep();
		sum = triangle[0][0];
		return sum;
	}
	
	public int getSum(){
		return sum;
	}

	public int[][] getChild(){
		for(int i=0;i<count;i++){
			System.out.println();
			for(int j=0;j<=i;j++){
				System.out.print(" "+child[i][j]);
			}
		}
		return child;
	}
	
	

	 public void ThreadSleep(){
	    	try{
	    		Thread.sleep(SLEEPTIME);
	    	}catch(InterruptedException e){
	    		e.printStackTrace();	
	    	}
	    	
	    }
	/*
	public void run(){
		//Graphics g= (Graphics) g;
		MaxTri();
	}
	
*/
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -