自从受伤之后好久都打不起精神,好在今天垚哥提醒了我,让我一个人的时候就专心学习,跟朋友在一起的时候再玩。确实,一个人玩容易抑郁。
现在记录一下国王与金矿的编程题目:
有一个国家发现了5座金矿,每座金矿的黄金储量不同,需要参与挖掘的工人数也不同。参与挖矿工人的总数是10人。每座金矿要么全挖,要么不挖,不能派出一半人挖取一半金矿。要求用程序求解出,要想得到尽可能多的黄金,应该选择挖取哪几座金矿?
gold[] = {400, 500, 200, 300, 350}; people[] = {5, 5, 3, 4, 3};
第一种就是暴力求解法,每一个金矿只有挖或者不挖的可能,一共2的n次方种情况;再把每种情况是否有员工不足筛选出来,最后求最大值。这种方法我试试自己用C++和Java写一下
package com.cai.javase.test国王与金矿;
public class MostGold0 {
public static int Violence( int n, int worker, int[] gold, int [] people ) {
/**
* k是相当于二进制01011,反向表示每一块金矿是否挖掘
* g是挖得的金矿
* p是用的人数
*/
int maxgold = 0, k, g, p;
for ( int i = 0; i < n*n; i++ ) {
k = i; g = 0; p = 0;
for( int j = 0; j < n; j++ ) {
if( k % 2 == 1 ) {
//如果最后一位是1,则挖这块矿石
g += gold[j];
p += people[j];
}
k = k / 2;//二进制转换,右移一位
}
if( p <= worker && g > maxgold)//如果用的人小于总人数且挖的金子大于最大金矿
maxgold = g;
}
return maxgold;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] gold = {400, 500, 200, 300, 350};
int[] people = {5, 5, 3, 4, 3};
System.out.println(Violence(5, 10, gold, people));
}
}
好吧,其实自己还是不会写,还是借鉴了网上的代码
下面用C++再写一遍:(晚上再写)
下面是递归算法,也就是算最后一块挖和不挖所能得到的最大金矿;先把java代码抄上来:
package com.cai.javase.test国王与金矿;
public class MostGold {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] gold = {400, 500, 200, 300, 350};
int[] people = {5, 5, 3, 4, 3};
System.out.println(getMostGold(5, 10, gold, people));
}
public static int getMostGold(int n, int worker, int[] g, int[] p) {
if ( n > g.length )//n超过了金矿数
System.out.println("fOQ");
if ( n <= 1 && worker < p[0])//一个金矿没工人挖了
return 0;
if ( n == 1 && worker >= p[0])//一个金矿有工人
return g[0];
if ( n > 1 && worker < p[n - 1])//不是第一块,但是工人挖不了这一块了,因为人不够
return getMostGold(n - 1, worker, g, p);
return Math.max(getMostGold(n - 1, worker, g, p),
(getMostGold(n - 1, worker - p[n - 1], g, p) + g[n - 1]));
}
}
再改成C++ 代码:
#include<iostream>
#include<cmath>
using namespace std;
int MostGold(int n, int goldNum, int worker, int Gold[], int people[]){
// cout << "正在挖第" << n << "块金子" << endl;
if ( n > goldNum )
cout<<"FoQ That man!"<<endl;
if ( n <= 1 && worker < people[0]){
return 0;
// cout << "第一块金矿没工人挖了返回0" << endl;
}
if ( n <= 1 && worker >= people[0]){
return Gold[0];
// cout << "有工人挖就返回第一块金矿的金子数" << endl;
}
if ( n > 1 && worker < people[n - 1]){
return MostGold( n - 1, goldNum, worker, Gold, people );
// cout << "不是第一块金子且人数足够挖这块,返回不挖这块能得到的最大金子数" << endl;
}
int a = MostGold( n - 1, goldNum, worker, Gold, people);//不挖最后一块所能得到的最大金子数
int b = MostGold( n - 1, goldNum, worker - people[n - 1], Gold, people) + Gold [n - 1];//挖最后一块所能得到的最大金子数
// if( a > b )
// cout << "不挖这一块" << endl;
// else
// cout << "挖出了第" << n << "块金子" << endl;
return max( a, b );
}
int main(){
int gold[] = {400, 500, 200, 300, 350};
//20 cout << sizeof(gold) << endl;
//5 cout << sizeof(gold) / sizeof(gold[0]) << endl;
int people[] = {5, 5, 3, 4, 3};
int GoldNum = sizeof(gold) / sizeof(gold[0]);
cout << MostGold(5, GoldNum, 10, gold, people) << endl << endl;
// cout << gold << endl << people << endl;
}
java中的求数组长度函数是.lengh而C++中的是sizeof()
改的时候发现了数组传入函数之后用sizeof就测不出长度了,int类型的数组的sizeof永远是4,所以要在传入函数之前就把数组的长度求出来再放进函数。
而且不知道为什么要在最后加两个endl或者最后一行不注解掉才能显示出900,而且不加上就会变成病毒被杀。