随机算法之雇用问题

  虽然写这个博客主要目的是为了给我自己做一个思路记忆录,但是如果你恰好点了进来,那么先对你说一声欢迎。我并不是什么大触,只是一个菜菜的学生,如果您发现了什么错误或者您对于某些地方有更好的意见,非常欢迎您的斧正!

问题描述:
  假如你要雇用一个新的办公助理。你找了一个雇用代理,这个雇佣代理每天给你推荐一个应聘者。你面试这个人,决定是否录取他。你需要付的钱分为几部分:一部分给雇用代理,一部分用于雇用应聘者。
  最近在学算法导论,这是第五章的内容。随机算法,我百度搜出来的解释是:“一个随机算法是一种算法,它采用了一定程度的随机性作为其逻辑的一部分。该算法通常使用均匀随机位作为辅助输入来指导自己的行为,超过随机位的所有可能的选择实现了“平均情况下的”良好业绩的希望。”那就我的理解而言,对于这个雇用问题,随机算法应该就是体现在面试者能力水平的随机上,也就是说可能今天这个面试者能力非常好,明天那个能力非常差,后天那个能力又比较一般。所以主要就是实现面试者数组中能力指数的随机。
  我这边贴出的代码,实现录取这个步骤,我技术一般,所以也就是很一般地实现。我这边想着,可能不止录取一个代理,所以面试多少人数,以及要录取多少人数都是自己设定的。也就是程序运行后要自己输入的。其实写这个,主要就是以后我自己看的时候,可以有一个思路回想。录取人数分两种,录取一个和录取多个。
录取一个:
  就是设定一个max,max< person[i](person是面试者的数组),就把person[i]赋值给max。很一般的思路,差不多就是一个数组求最大值。
录取多个:
  就是设定一个数组,然后开始遍历面试者数组,当person[i]大于当前助理的能力指数的时候,就加入录取者数组,录取者数组满了之后就按能力指数进行排序,拍完序后第一位肯定是能力最低的,同时继续遍历面试者数组,找一个面试者能力指数大于录取者第一位的,就录取他,加入录取者数组,再对录取者进行从小到大的排序。感觉是一个很耗时间的过程。
  接下来就贴出我的代码吧。

雇佣问题_随机算法.h

#pragma once
#define HIRE_ONE_PERSON_MONEY 100/*雇用一个人的钱*/
#define INTERVIRE_ONE_PERSON_MONEY 2/*面试一个人的钱*/
#define QUNODAM_QUALITY_NUMBER 6/*原来助理的能力指数*/

/*面试者能力高低随机化*/
void SetInterviewer(int person[], int count);

/*输出数组*/
void PrintArrayList(int person[], int n);

/*选择雇佣的人*/
void ChooseInterviewer(int person[], int count, int wantNumber);

/*自定义排序规则:依照能力升序排序*/
bool SortWay(int a, int b);

/*雇佣问题测试函数*/
void TestHirePerson();


/*雇佣问题_随机算法.cpp*/
#include "stdafx.h"
#include "雇佣问题_随机算法.h"
#include <iostream>
#include <math.h>
#include <algorithm>
#include <stdlib.h>
#include <time.h>
using namespace std;

/*面试者能力高低随机化*/
void SetInterviewer(int person[], int count)
{
	srand((unsigned)time(NULL));
	for (int i = 0; i < count; i++)
		person[i] = rand() % 100 + 1;
}

/*输出数组*/
void PrintArrayList(int person[], int n)
{
	cout << endl;
	for (int i = 0; i < n; i++)
	{
		cout << person[i];
		cout << " ";
		if ((i + 1) % 5 == 0)
			cout << endl;
	}
	cout << endl;
}

/*自定义排序规则:依照能力升序排序*/
bool SortWay(int a,int b)
{
	return a<b;
}

/*选择雇佣的人*/
void ChooseInterviewer(int person[], int count, int wantNumber)
{
	int i;
	int totalMoney = 0;
	int *stayPerson = new int[wantNumber];
	int temp = 0;
	if (wantNumber == 1)
	{
		int max = QUNODAM_QUALITY_NUMBER;
		for (i = 0; i < count; i++) 
		{
			if (person[i] > max)
			{
				max = person[i];
				temp = i;
				totalMoney += HIRE_ONE_PERSON_MONEY;
			}
		}
		totalMoney += (count* INTERVIRE_ONE_PERSON_MONEY);
		cout << endl;
		cout << "面试者如下:";
		PrintArrayList(person, count);
		cout << "雇佣了第" << temp+1 << "个面试者" << endl;
		cout << "他的能力指数为:" << max << endl;
		cout << "总开销为" << totalMoney << endl;
	}
	else
	{
		int length = 0;
		for (i = 0; i < count; i++)/*面试的for循环*/
		{
			/*把能力高于原来助理进行挑选*/
			if (person[i] > QUNODAM_QUALITY_NUMBER)
			{
				if (length < wantNumber)/*雇佣的人数如果小于*/
				{
				
						stayPerson[length] = person[i];
						length++;
						totalMoney += HIRE_ONE_PERSON_MONEY;
				}
			
				/*如果雇佣的人数已经达到了10个*/
				else
				{
					/*把雇佣的人按能力值进行排序*/
					sort(stayPerson, stayPerson + length, SortWay);
					if (person[i] > stayPerson[0])
					{
						stayPerson[0] = person[i];
						totalMoney += HIRE_ONE_PERSON_MONEY;
					}
				}
			}
		}
		totalMoney += (count* INTERVIRE_ONE_PERSON_MONEY);
		cout << endl;
		cout << "面试者如下:" << endl;
		PrintArrayList(person, count);
		cout << "雇佣者的能力排行为:" << endl;
		PrintArrayList(stayPerson, wantNumber);
		cout << "总开销为" << totalMoney << endl;
		delete[] stayPerson;
	}
}

/*雇佣问题测试函数*/
void TestHirePerson()
{
	int count;/*面试总人数*/
	int wantNumber;/*想雇佣的人数*/
	cout << "请输入面试的总人数:";
	cin >> count;
	cout << endl;
	cout << "请输入想要雇佣的的总人数:";
	cin >> wantNumber;
	cout << endl;
	int *person = new int[count];
	SetInterviewer(person, count);
	ChooseInterviewer(person, count, wantNumber);
	delete[] person;
}

/*主函数*/
int main()
{
	TestHirePerson();
	system("Pause");
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值