红包程序
// Practice1.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <ctime>
#include <iostream>
#include <math.h>
int main()
{
int i, number;
int best; //最佳的
float total;
std::cout << "请输入红包金额: ";
std::cin >> total;
std::cout << "请输入红包数量: ";
std::cin >> number;
//生成随机数
//设置种子
srand((unsigned)time(NULL));
float a[1024]; //保存每个人的随机数。最多支持1024个人抢红包。
float b[1024];//保存每个人获得的红包金额。
float suma = 0; //随机数总和
float sumb = 0; //红包总和
int max = 0;
for (i = 0; i < number; i++)
{
a[i] = rand() ;
if (a[i]>max)
{
max = a[i];
best = i; //获取手气最佳.相同金额以第一个为准
}
suma += a[i];
}
for (i = 0; i < number-1; i++)
{
float temp;
b[i] = (a[i] / suma)*total; //按照随机数计算每个人实际获得的金额
temp= round(b[i] * 100)/100; //将红包金额保留两位小数
sumb += temp;
if (best==i)
{
std::cout << "第" << i + 1 << "个人获得的红包金额是: " << temp <<"(手气最佳)"<< std::endl;
}
else
{
std::cout << "第" << i + 1 << "个人获得的红包金额是: " << temp << std::endl;
}
}
if (best==number-1)
{
std::cout << "第" << number << "个人获得的红包金额是: " << total - sumb << "(手气最佳)" << std::endl;
}
else
{
std::cout << "第" << number << "个人获得的红包金额是: " << total - sumb << std::endl;
}
system("pause");
return 0;
}