经典算法之背包问题

// In Practice, You should use the statndard input/output
// in order to receive a score properly.
// Do not use file input and output. Please be very careful.

#include <stdio.h>

#define SIZE 100000000
#if 1
	#define NMAX 1001
	#define KMAX 101
#else
	#define NMAX 10
	#define KMAX 5
#endif
int result[KMAX][NMAX];


int main(void)
{
	int tc, T;
	// The freopen function below opens input.txt file in read only mode, and afterward,
	// the program will read from input.txt file instead of standard(keyboard) input.
	// To test your program, you may save input data in input.txt file,
	// and use freopen function to read from the file when using scanf function.
	// You may remove the comment symbols(//) in the below statement and use it.
	// But before submission, you must remove the freopen function or rewrite comment symbols(//).

	freopen("input.txt", "r", stdin);

	// If you remove the statement below, your program's output may not be rocorded
	// when your program is terminated after the time limit.
	// For safety, please use setbuf(stdout, NULL); statement.

	setbuf(stdout, NULL);

	scanf("%d", &T);
	 for(tc = 0; tc < T; tc++)
		{
			int N=0,K=0;
			int i=0,j=0;
			scanf("%d",&N);
			scanf("%d",&K);

			result[0][0]=1;
			for(j=1;j<=N;j++)
				result[0][j]=0;

			for(i=1;i<=K;i++)
			{
				for(j=0;j<=N;j++)
				{
					if(i>j)
					{
						result[i][j]=result[i-1][j];
					}
					else
					{
						result[i][j]=(result[i-1][j]+result[i][j-i])%100000000;
					}

				}
			}

			printf("%d\n",result[K][N]%SIZE);
		}


	return (0);//Your program should return 0 on normal termination.
}


//

/************************************************************************************** 
* Function     : test 
* Create Date  : 2014/06/09 
* Author       : NTSK13 
* Email        : beijiwei@qq.com 
* Copyright    : 欢迎大家和我一起交流学习,转载请保持源文件的完整性。 
*                任何单位和个人不经本人允许不得用于商业用途 
* Version      : V0.1                    
***************************************************************************************                    
经典c程序(0031) 
          
题目:Juice
     
Mr. Kim has made healthy juice for his colleagues. This has been made of 100% natural
vitamin C especially for overcoming fatigue. With powder from various natural
fruits in it, it has diverse colors, prices and weights.
Mr. Kim has poured various kinds of powder in a cup to make a special colored
cup of juice. If you know the price and weight of each bag of powder and the
weight of the filled cup of juice, create a calculation program to find out the
lowest cost used for making the cup of juice.

Time limits : 1 second (java: 2 seconds)

Input Format

Input may include many test cases. The number of test cases, T, is given on the first
line of input and then the amount of T of test cases is given in a line. (T ≤ 20)
The weight of the empty cup, E, and the weight of the filled cup of juice, F, are given
separately as blanks for the first line of each test case. (1 ≤ E ≤ F ≤ 10,000)
The number of kinds of juice that is used, N, is given for the next line. (1 ≤ N ≤ 500)
From the next line through to the amount of N lines, the unit price and weight of each
juice is given separately as blanks. The unit price of juice is positive number ≤ 50,000;
and the weight is positive number ≤ 10,000.

Output Format

Output the minimum cost of cases for the first line of each test case. If you can not make
it, output "impossible".

Input Example

3
10 110
2
1 1
30 50
10 110
2
1 1
50 30
1 5
2
10 3
20 4

Output Example

60
100
20
**************************************************************************************/  
// In Practice, You should use the statndard input/output
// in order to receive a score properly.
// Do not use file input and output. Please be very careful.

#include <stdio.h>
#include<limits.h>

#if 1

#define NMAX 501
#define VMAX 10001

#else

#define NMAX 11
#define VMAX 15

#endif

#define MAX 50000 *10000
int result[NMAX][VMAX];

typedef struct{
	int weight;
	int price;
}Juice;

Juice array[NMAX];


int main(void)
{
	int tc, T;

	// The freopen function below opens input.txt file in read only mode, and afterward,
	// the program will read from input.txt file instead of standard(keyboard) input.
	// To test your program, you may save input data in input.txt file,
	// and use freopen function to read from the file when using scanf function.
	// You may remove the comment symbols(//) in the below statement and use it.
	// But before submission, you must remove the freopen function or rewrite comment symbols(//).

	freopen("input.txt", "r", stdin);

	// If you remove the statement below, your program's output may not be rocorded
	// when your program is terminated after the time limit.
	// For safety, please use setbuf(stdout, NULL); statement.

	setbuf(stdout, NULL);

	scanf("%d", &T);
	for(tc = 0; tc < T; tc++)
	{
		int E=0,F=0,N=0,value=0;
		int i=0,j=0,k=0;
		/**********************************************************************************/
		/*  Implement your algorithm here. */
		scanf("%d", &E);
		scanf("%d", &F);
		scanf("%d", &N);

		for(i=1;i<=N;i++)
		{
			scanf("%d",&(array[i].price) );
			scanf("%d",&(array[i].weight) );
		}

		for(j=0;j<=F-E;j++)//初始化跟结果相关的数
		{
			result[0][j]=MAX;
		}

		for(i=1;i<=N;i++)
		for(j=1;j<+F-E;j++)
			result[i][j]=MAX;

		for(i=1;i<=N;i++)
		{
			for(j=1;j<= F-E;j++)
			{
				if(array[i].weight > j)
					result[i][j]=result[i-1][j];
				else
				{
					value=result[i][j-array[i].weight] +array[i].price;
					if( value < result[i-1][j] )
						result[i][j]=value;
					else
						result[i][j]=result[i-1][j];

				}
			}
		}

		/*********************************************************************************/
		printf("%d\n",result[N][F-E]);

		// Print the answer to standard output(screen).
	}

	return 0;//Your program should return 0 on normal termination.
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值