當前位置:首頁 » 編程語言 » 等額發紅包用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;
}