『壹』 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語言邏輯語序 輸出 一個數一換行 改成兩個數一換行
char s[100][5]={"ab","jg","in","jg","en"};
for(int i=0;i<5;i++)
{printf("%d,%s,",i,s[i]);
if(i%2==0)
printf("\n");
}
『叄』 模擬一個微信搶紅包的演算法,錢數為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語言編程題,求解題思路!
只要按照公式計算每月還款金額就可以了,其中"已歸還本金累計額"等於"貸款本金 除以 還款月數 乘以 已還款月數".
按照題目要求編寫的等額本金還款的C語言程序如下
#include<stdio.h>
int main(){
double P,R,repayment;
int N,i;
scanf("%lf;%d;%lf",&P,&N,&R);
printf("[");
for(i=0;i<N;i++){
repayment=(P/N)+(P-P/N*i)*R;
if(i==N-1)
printf("%.2lf]",repayment);
else
printf("%.2lf,",repayment);
}
return 0;
}
『伍』 C語言:恭喜發財 利是竇來
BIT的同學,立刻崩潰就用調試糾錯,程序崩潰很好debug,這個要自己親自嘗試,別人幫你學不會。
『陸』 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語言 微信紅包
#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;
}
『捌』 c語言:解題思路
只要按照公式計算每月還款金額就可以了,其中"已歸還本金累計額"等於"貸款本金 除以 還款月數 乘以 已還款月數".
按照題目要求編寫的等額本金還款的C語言程序如下
#include<stdio.h>
int main(){
double P,R,repayment;
int N,i;
scanf("%lf;%d;%lf",&P,&N,&R);
printf("[");
for(i=0;i<N;i++){
repayment=(P/N)+(P-P/N*i)*R;
if(i==N-1)
printf("%.2lf]",repayment);
else
printf("%.2lf,",repayment);
}
return 0;
}
『玖』 C語言計算貸款問題 輸出不正確
printf("money(%lf,%d)=%lf\n",loan,year,money);