帮我写一个拼手气红包代码
时间: 2023-03-28 19:01:23 浏览: 89
好的,以下是一个简单的拼手气红包代码:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
int total, num, i, j;
printf("请输入红包总金额:");
scanf("%d", &total);
printf("请输入红包个数:");
scanf("%d", &num);
int *money = (int *)malloc(num * sizeof(int));
srand((unsigned)time(NULL));
int left = total;
for (i = ; i < num; i++)
{
if (i == num - 1)
{
money[i] = left;
}
else
{
int max = left - (num - i - 1);
int min = 1;
int m = rand() % (max - min + 1) + min;
money[i] = m;
left -= m;
}
}
printf("红包金额分别为:\n");
for (j = ; j < num; j++)
{
printf("%d ", money[j]);
}
free(money);
return ;
}
阅读全文