#include <string.h>
#include <time.h>
#define MAX_TOTAL_MONEY 200 //红包的最大金额
#define MIN_PER_PLAYER 1 //一个人抢到的的最小面额1元
#define MAX_PLAYER_CNT (MAX_TOTAL_MONEY/MIN_PER_PLAYER) //最大抢红包的游戏人数
typedef struct player
{
char *name//标记玩家 可以不填
unsigned int money_get//抢到的红包
}PLAYER_T
//每个人领取到的红包金额不等 这个要求比较难搞 暂时不考虑
int main(int argc, char *argv[])
{
unsigned int total_money = 0 //不考虑角和分 浮点运算比较复杂
unsigned int player_cnt = 0
int on_off = 0
int i = 0
int j = 0
PLAYER_T player[MAX_PLAYER_CNT] = {0}
PLAYER_T tmp = {0}
吵盯
printf("输入红包金额:\n")
scanf("%u", &total_money)
printf("输入游戏人数:\n")
scanf("%u", &player_cnt)
printf("是否需要减小贫富差距(0为关闭其余为开启):\n")
scanf("%u", &on_off)
//不符合规则的液枝输入判断
if (total_money > MAX_TOTAL_MONEY || 0 == total_money || 0 == player_cnt || player_cnt*MIN_PER_PLAYER > total_money)
{
printf("红升埋和包金额最小%u元 最大%u元 游戏人数最小1人 最大%u人\n", MIN_PER_PLAYER, MAX_TOTAL_MONEY, MAX_PLAYER_CNT)
return 0
}
for (i = 0 i < player_cnt i++)
{
//设置随机种子
srand(time(NULL)+i)
//根据随机种子获取一个伪随机数作为抢到的红包 并通过余运算使其始终小于total_money
player[i].money_get = rand()%total_money
//限制所有人所能抢到的最大红包为当前金额池的1/5而不是全部
if (0 != on_off)
{
if (total_money > 5)//5块钱以上再限制
{
player[i].money_get = rand()%(total_money/5)
}
}
//最后一个人拿所有剩下的红包
if (player_cnt - 1 == i)
{
player[i].money_get = total_money
}
//运气差随机到0元 给你最小面额
else if (0 == player[i].money_get)
{
player[i].money_get = MIN_PER_PLAYER
}
//剩下的要保证每个人能抢到最小面额
else if (total_money - player[i].money_get < (player_cnt-i-1)*MIN_PER_PLAYER)
{
player[i].money_get = total_money - (player_cnt-i-1)*MIN_PER_PLAYER
}
//把抢到的金额从红包池中减掉
total_money -= player[i].money_get
//如果填了name 可以把名字打印出来
printf("第%d个玩家抢到红包:%u元\n", i+1, player[i].money_get)
}
//冒泡排序 找出手气最佳者
for (i = 0 i < player_cnt i++)
{
for (j = i+1 j < player_cnt j++)
{
if (player[i].money_get < player[j].money_get)
{
memcpy(&tmp, &player[j], sizeof(PLAYER_T))
memcpy(&player[j], &player[i], sizeof(PLAYER_T))
memcpy(&player[i], &tmp, sizeof(PLAYER_T))
}
}
}
printf("手气最佳者抢到红包:%u元\n", player[0].money_get)//如果填了name 可以把名字打印出来
return 0
}
#include<stdio.h>改慧#include<time.h>
#include<stdlib.h>
int main(void)
{
float total
printf("输入总钱数:\n")
scanf("%f",&total)
int num
printf("输入兆悄红包数量:\n")
scanf("%d",&num)
float min=0.01
float safe_total
float money
int i
srand((unsigned)time(NULL))
for(i=1i<numi++){
safe_total=(total-(num-i)*min)/(num-1)
money=(float)(rand()%((int)(safe_total*100)))/100+min
total=total-money
printf("红包%2d: %.2f元,余额:%.2f元\n",i,money,total)
}
printf("红包%2d: %.2f元族歼渣,余额:0.00元\n",num,total)
return 0
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)