微信红包
完成模拟微信红包的实现:
1、每个人都要能够领取到红包;
2、每个人领取到的红包金额总和=总金额;
3、每个人领取到的红包金额不等;
4、算法简单;
5、程序代码可读性要强;
6、如果有可能,对算法进行优化,让每个红包差额不要太大,打印出最佳手气者。
C语言微信红包
答案:1 悬赏:20 手机版
解决时间 2021-12-20 10:41
- 提问者网友:你给我的爱
- 2021-12-20 05:10
最佳答案
- 五星知识达人网友:过活
- 2022-01-05 23:22
#include <stdio.h>
#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 <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;
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯