完美世界JAVA笔试题

年会游戏

时间限制:C/C++语言 1000MS;其他语言 3000MS
内存限制:C/C++语言 65536KB;其他语言 589824KB

题目描述:

公司年会设计了一个游戏,抽到号码的员工可以到舞台的桌子上带回总重量不超过固定重量的若干个奖品,桌子上的奖品数量是有限的,每个奖品上都标明了它的重量和价值,对于一个奖品它只能选择带走或者不带走,不能将它掰成几份带走部分,请设计实现一个算法计算当奖品数量、奖品价值、能带走的总重量取不同值时,员工能带走的最大价值是多少。

输入

输入奖品数量,每个奖品的价值和重量,能够带走的奖品重量限度

输入第一行:奖品数量

输入第二行:每个奖品的价值,通过空格分割

输入第三行:每个奖品的重量,通过空格分割

输入第四行:能够带走的奖品总重量限度

输出

员工能够带走的最大价值

 

样例输入

5

5000 4000 3000 500 200

5 4 6 3 1

10

样例输出

9200


package test;

import java.util.Scanner;

public class Demo {
    private static Scanner sc;
	private static int count = 0;
	private static int[] worths;
	private static int[] weights;

	public static void main(String[] args)  {
		sc = new Scanner(System.in);
                count = sc.nextInt();
                sc.nextLine();
		String worth = sc.nextLine();
		worths = stringArrayintArray(worth.trim().split(" "));
		String weight = sc.nextLine();
		weights = stringArrayintArray(weight.trim().split(" "));
		int remainWeight = sc.nextInt();
		System.out.print(getMostWorth(remainWeight, 0));
	}
	
	
	public static int getMostWorth(int remainWeight, int index){
		if(index < count){
			if(remainWeight>=weights[index]){
				int yes = worths[index] + getMostWorth(remainWeight-weights[index], index+1);
				int no = getMostWorth(remainWeight, index+1);
				return yes>no?yes:no;
			}else{
				return getMostWorth(remainWeight, index+1);
			}
		}
		return 0;
	}
	
	
	public static int[] stringArrayintArray(String[] strArray){
		int[] intArray = new int[strArray.length];
		for(int i=0;i<strArray.length;i++){
			intArray[i] = Integer.parseInt(strArray[i]);
		}
		return intArray;
	}

}

题目分析:典型的0-1背包问题(笔试时由于时间紧张,根本没时间看,好可惜,后面补上)问题可以描述为:给定一组物品,每种物品都有自己的重量和价格,在限定的总重量内,我们如何选择,才能使得物品的总价格最高。

爸爸去哪儿

时间限制:C/C++语言 1000MS;其他语言 3000MS
内存限制:C/C++语言 65536KB;其他语言 589824KB

题目描述:

小鱼儿和安吉一起去参加爸爸去哪儿,村长交给他们一项任务,是用老乡的水果去给爸爸兑换一个礼物,要求水果和礼物等价,不能多也不能少。假设老乡有n种水果,每种水果的数量不限,每种水果的价值不同。请帮小鱼儿和安吉计算出他们最少要和老乡要几个水果。如果无法兑换返回-1.

举例:

1.有3种水果,价值分别是5,2,3。礼物的价值是20.用4个5元的水果正好兑换,其他的兑换方法都要更多的水果,所以返回4

2.有两种水果,价值分别是5,3,礼物的价值是2.无法正好兑换,所以返回-1

输入

输入数据是一个数组和一个整数代表礼物价值

输出

对于每个测试实例,要求输出最少水果数

样例输入

5,2,3

20

样例输出

4

package test2;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;


public class Test1 {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		try {
			String value = br.readLine().trim();
			String targetValue = br.readLine().trim();
			String[] str_values = value.split(" ");
			int[] values = parseIntArray(str_values);
			NormalQuikSort(values,0,values.length-1);
			System.out.print(getCount(values,Integer.parseInt(targetValue),0, 0));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private static int getCount(int[] values, int targetValue,int currentCount,int index) {
		int num = targetValue/values[index];
		int remain = targetValue%values[index];
		if(num > 0)
		{	int ret = -1;		
			while(num > 0 )
			{
				if(remain == 0)
					return num+currentCount;
				ret = getCount(values,(targetValue-values[index]*num),currentCount+num,index+1);
				if( ret != -1)
					return ret;
				num--;
			}
		}else if(num <= 0 && (index+1) < values.length)
			return getCount(values,targetValue,currentCount,index+1);
		return -1;
	}
	private static int[] parseIntArray(String[] str_values) {
		// TODO Auto-generated method stub
		int[] arr = new int[str_values.length];
		for (int i = 0; i < arr.length; i++) {
			arr[i] = Integer.parseInt(str_values[i]);
		}
		return arr;
	}
	public static void Swap(int[] arr,int m,int n)
	{
		int temp;
		temp = arr[m];
		arr[m]=arr[n];
		arr[n]=temp;
	}
	public static void NormalQuikSort(int[] arr,int p,int q)
	{
		if (p < q) {
			int i = Partition(arr,p,q);
			NormalQuikSort(arr,p,i-1);
			NormalQuikSort(arr,i+1,q);
		}
	}
	public static int Partition(int[] arr,int p,int q)
	{
		int x = arr[p];
		int i = p;
		for (int j = p+1; j <= q; j++) {
			if (arr[j] > x ) {
				i = i+1;
				Swap(arr,i,j);
			}
		}
		Swap(arr,p,i);
		return i;
	}	
}


思路:使用快排为将水果价值排序,从最大价值算起,通过递归,在满足一定的总价值情况下,保证所换得的水果数目最少

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值