0 - 1 가방 문제 (자바, 입 출력 파일)

1566 단어
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

//0-1    
public class Knapsack {
	int n;//     n
	int weight[];//     
	int value[];//     
	int maxweight;//     
	int result;//          
	public static void main(String[] args) {
		Knapsack ks = new Knapsack();//      ,           
		ks.iostream();
	}
	public void iostream(){
		try {
			FileWriter fw = new FileWriter("output.txt");//       
			for(int m=0;m<6;m++){//    6     
			Scanner in = new Scanner(new BufferedInputStream(new FileInputStream("input_assign01_0"+ m +".dat")));//       
			n = in.nextInt();//            
			maxweight = in.nextInt();//            
			weight=new int[n];
			value=new int[n];
			for(int i=0;ij){//   i          ,  i       
					maxvalue[i][j] = maxvalue[i-1][j];
				}
				else{
					if(maxvalue[i-1][j - weight[i-1]] + value[i-1]>maxvalue[i-1][j]){//  i    ,   i-1             maxvalue-weight[i]
					   maxvalue[i][j] = maxvalue[i-1][j - weight[i-1]] + value[i-1];
					}
					else{//   i          i     ,   i           i-1       
					   maxvalue[i][j] = maxvalue[i-1][j];						
					}
				}
			}
		}
		return maxvalue[n][maxweight];//            
	}
 
}

좋은 웹페이지 즐겨찾기