年会抽奖算法C语言实现1

充小云
2023-12-01

功能描述:
年会抽奖有特等奖,1,2,3等奖,根据设定概率,随机出现是哪个奖项,且总奖数维持和设定值一样。

#include <iostream>
//#include <stdlib.h>
#include<time.h>

using namespace std;

void main()
{
	int Prize_0 = 0, Prize_1 = 0, Prize_2 = 0, Prize_3 = 0, Prize_T = 0, Prize = 0;
	char enter;
	int count_p[4] = {0};
	int count=0;
	cout << "20221228 21:55 Writen by Young" << endl;
	cout << "请输入特等奖个数,一等奖个数,二等奖个数,三等奖个数:";
	cin >> Prize_0 >> Prize_1 >> Prize_2 >> Prize_3;

	Prize_T = Prize_0 + Prize_1 + Prize_2 + Prize_3;
	
	while (cin>>enter&&count<Prize_T)
	{
	    srand(time(NULL));
		count=0;
		Prize = rand() % Prize_T;
		if (Prize <= Prize_3&&count_p[3]<Prize_3)
			cout << "三等奖" <<++count_p[3]<< endl;
		else if (Prize <= Prize_3 + Prize_2 && count_p[2] < Prize_2)
			cout << "二等奖" << ++count_p[2] << endl;
		else if (Prize <= Prize_3 + Prize_2 + Prize_1 && count_p[1] < Prize_1)
			cout << "一等奖" << ++count_p[1] << endl;
		else if (Prize <= Prize_3 + Prize_2 + Prize_1 + Prize_0 && count_p[0] <Prize_0)
			cout << "特等奖" << ++count_p[0]  << endl;
		for (int i = 0; i <= 3; i++)
			count+= count_p[i];
		if (count == Prize_T)
		{
			for (int i = 0; i < 4; i++)
				cout <<i<<"等奖个数为"<< count_p[i] <<endl;
		}
	}



	system("pause");
}
链接:https://pan.baidu.com/s/1W1O3LH7v_mc52NuvJH6Fbw 
提取码:x4ue
 类似资料: