当前位置:首页 » 编程语言 » 等额发红包用c语言表示
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

等额发红包用c语言表示

发布时间: 2023-01-11 11:09:01

① 求c语言/c++大神编写发红包问题

完整代码如下:

#include<stdio.h>
voidmain()
{
intmoney;
intmoney1,money2,money5,money10,money20,money50,money100;
printf("请输入金额:");
scanf("%d",&money);
money100=money/100;
money=money%100;
money50=money/50;
money=money%50;
money20=money/20;
money=money%20;
money10=money/10;
money=money%10;
money5=money/5;
money=money%5;
money2=money/2;
money1=money%2;
printf("100元:%d张 ",money100);
printf("50元:%d张 ",money50);
printf("20元:%d张 ",money20);
printf("10元:%d张 ",money10);
printf("5元:%d张 ",money5);
printf("2元:%d张 ",money2);
printf("1元:%d张 ",money1);
}

运行结果如下图:

这时我们再编译、链接、执行一下:i = 10, j = 3i=、,、空格和j=全都原样输出了。此外需要注意的是:“输出控制符”和“输出参数”无论在“顺序上”还是在“个数上”一定要一一对应。

4) printf("输出控制符 非输出控制符",输出参数);

这实际上就是上面那个例子。这时候会有一个问题:到底什么是“输出控制符”,什么是“非输出控制符”?很简单,凡是以%开头的基本上都是输出控制符。

② C语言编程:求一段发红包的代码。(随机数,能规定红包总钱数总人数,每个人拿到的钱不为0)

#include<stdio.h>
#include<time.h>
#include<stdlib.h>
intmain(void)
{
floattotal;
printf("输入总钱数: ");
scanf("%f",&total);

intnum;
printf("输入红包数量: ");
scanf("%d",&num);

floatmin=0.01;
floatsafe_total;
floatmoney;
inti;
srand((unsigned)time(NULL));
for(i=1;i<num;i++){
safe_total=(total-(num-i)*min)/(num-1);
money=(float)(rand()%((int)(safe_total*100)))/100+min;
total=total-money;
printf("红包%2d:%.2f元,余额:%.2f元 ",i,money,total);
}
printf("红包%2d:%.2f元,余额:0.00元 ",num,total);
return0;
}

③ C语言的这个问题!

我想试一试,但愿你这不是作业题。
终于有时间来写一个程序了。
程序中的注释是中文的,在TC2.0环境下是乱码,这个你要注意。

#include<stdio.h>
#include<string.h>

char name[10][14]; /*最多10人,每人名字最多14个字符*/

/*查找串在数组name中的位置*/
int index(char *p)
{
int k=0;
while(k<10&&strcmp(p,name[k]))
k++;
return k;
}

int main()
{
int people=1; /*有多少人?至少2人*/
char name_tmp[14];
int balance[10]; /*每个人的收支帐户*/
int i,j;
int children=0,money=0;

/*输入人数,并保证输入人数合法*/
printf("How many people?");
while(people<2||people>10)
scanf("%d",&people);

/*输入人的名字,并初始化他们的帐户*/
puts("Who are they?");
for(i=0;i<people;i++)
{
scanf("%s",name[i]);
balance[i]=0;
}

for(i=0;i<people;i++)
{
/*为了使输入的简单化,这儿没让用户输入名字,而是顺次输出人名*/
puts(name[i]);

/*提示输入钱数,和小孩数*/
printf("Input money and number of children:");
scanf("%d%d",&money,&children);

if(children>0) /*如果小孩的人数不为0*/
{
balance[i]+=0-(money-money%children); /*计算这人付出了多少钱*/

puts("Who are they?"); /*提示输入这些钱分给哪些人*/
for(j=0;j<children;j++)
{
scanf("%s",name_tmp);
balance[index(name_tmp)]+=(children==0?0:money/children); /*修改得到钱的人的帐户 */
}
}
}

/*输出人名和差额*/
for(i=0;i<people;i++)
printf("%s %d\n",name[i],balance[i]);

return 0;
}

④ 模拟一个微信抢红包的算法,钱数为x,人数为y,用随机数显示出每个红包的金额,c语言题目

#include<stdio.h>
#include<stdlib.h>
#include<time.h>

doubleRandom(){return(rand()%1000)*0.001;}
doubleRandom(doubleaver)
{
doubleret=0;
for(inti=0;i<2;++i)ret+=Random();
return(aver*ret);
}


intmain(intargv,int**argc)
{
doublex,v;
inty,i;
doubleaver,min;

srand(time(NULL));
printf("请输入金额:");
scanf("%lf",&x);
printf("请输人数:");
scanf("%d",&y);
aver=x/y;
min=0.01*y;
for(i=0;i<y-1&&x>min;++i)
{
v=Random(aver);
while(v>x)v=Random(aver);
printf("%.2f ",v);
x-=v;
min-=0.01;
}
for(i=0;i>y-1;++i)
{
printf("%.2f ",0.01);
x-=0.01;
}

printf("%.2f ",x);
return0;
}

⑤ C语言红包代码

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
main()
{
inti,n;
floata[100],all;
srand(time(NULL));
printf("请输入红包金额:");
scanf("%f",&all);
printf("请输入红包个数:");
scanf("%d",&n);
srand((unsigned)time(0));
for(i=1;i<n;i)
{
a[i]=(float)rand()/RAND_MAX*all;
if(a[i]>0)
{
all-=a[i];
printf("%f ",a[i]);
i++;
}
}
printf("%f ",all);
}

⑥ c语言成绩排序,做完微信红包

#include"stdio.h"
#include<stdlib.h>
#include<string.h>
#include"time.h"
structabcd{
charID[11],name[17];
ints1,s2,s3,s4,s5;
doubleave;
structabcd*next;
};
voidinp(structabcd(*p)[45]){
inti,j,k,t;
charid[11]="0000000000";
for(i=0;i<4;i++){
for(j=0;j<45;j++){
if(++id[9]>'9')
for(k=9;k>0;k--)
id[k]-=10,id[k-1]++;
strcpy(p[i][j].ID,id);
for(t=0,k=rand()%16;k;k--)
p[i][j].name[t++]=rand()%26+'A';
p[i][j].name[t]='';
p[i][j].s1=rand()%101;
p[i][j].s2=rand()%101;
p[i][j].s3=rand()%101;
p[i][j].s4=rand()%101;
p[i][j].s5=rand()%101;
}
}
}
voidaverage(structabcd(*p)[45]){
inti,j;
for(i=0;i<4;i++)
for(j=0;j<45;p[i][j].ave=(p[i][j++].s1+p[i][j].s2+p[i][j].s3+p[i][j].s4+p[i][j].s5)/5.0);
}
voidselsort(structabcd(*p)[45]){//4个班统一选择排序
inti,j,k,t;
structabcdq;
for(t=0;t<4;t++)
for(i=0;i<45;i++){
for(k=i,j=k+1;j<45;j++)
if(p[t][k].ave<p[t][j].ave)
k=j;
if(k!=i)
q=p[t][k],p[t][k]=p[t][i],p[t][i]=q;
}
}
voidbubsort(structabcd(*p)[45]){//4个班统一冒泡排序
inti,j,t;
structabcdq;
for(t=0;t<4;t++)
for(i=0;i<45;i++)
for(j=0;j<44;j++)
if(p[t][j].ave<p[t][j+1].ave)
q=p[t][j],p[t][j]=p[t][j+1],p[t][j+1]=q;
}
voidgetlink(structabcd(*p)[45],structabcd*x){//弄成链表
inti,j,k,t;
structabcdq;
for(t=i=0;i<4;i++)//把4个班的成绩拷贝到临时数组x中
for(j=0;j<45;x[t++]=p[i][j++]);
for(i=0;i<t;i++){//对数组x降序排序
for(k=i,j=k+1;j<t;j++)
if(x[k].ave<x[j].ave)
k=j;
if(k!=i)
q=x[k],x[k]=x[i],x[i]=q;
}
for(i=1;i<t;x[i-1].next=x+i++);//把数组p搞成单链表
x[i-1].next=NULL;
}
intmain(intargc,char*argv[]){
structabcdc[4][45],t[4*45],*p;
intk;
srand((unsigned)time(NULL));
inp(c);
average(c);
bubsort(c);
selsort(c);//这是脱了裤子FP,无论用什么方法排序结果是一样的,不懂为何这么出题
getlink(c,t);
printf("----------Lookat...---------- ");
for(k=0,p=t;p;p=p->next)//将链表中的值输出
printf(++k%15?"%5.1f":"%5.1f ",p->ave);
if(k%10)
printf(" ");
return0;
}

运行样例:

这种题打字太多……

⑦ C语言 微信红包

#include<stdio.h>
#include<string.h>
#include<time.h>

#defineMAX_TOTAL_MONEY200//红包的最大金额
#defineMIN_PER_PLAYER1//一个人抢到的的最小面额1元
#defineMAX_PLAYER_CNT(MAX_TOTAL_MONEY/MIN_PER_PLAYER)//最大抢红包的游戏人数

typedefstructplayer
{
char*name;//标记玩家可以不填
unsignedintmoney_get;//抢到的红包
}PLAYER_T;

//每个人领取到的红包金额不等这个要求比较难搞暂时不考虑
intmain(intargc,char*argv[])
{
unsignedinttotal_money=0;//不考虑角和分浮点运算比较复杂
unsignedintplayer_cnt=0;
inton_off=0;
inti=0;
intj=0;
PLAYER_Tplayer[MAX_PLAYER_CNT]={0};
PLAYER_Ttmp={0};

printf("输入红包金额: ");
scanf("%u",&total_money);
printf("输入游戏人数: ");
scanf("%u",&player_cnt);
printf("是否需要减小贫富差距(0为关闭其余为开启): ");
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人 ",MIN_PER_PLAYER,MAX_TOTAL_MONEY,MAX_PLAYER_CNT);
return0;
}

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元给你最小面额
elseif(0==player[i].money_get)
{
player[i].money_get=MIN_PER_PLAYER;
}
//剩下的要保证每个人能抢到最小面额
elseif(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元 ",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元 ",player[0].money_get);//如果填了name可以把名字打印出来
return0;
}