模拟微信红包派发算法 深碍√TFBOYSˉ_ 2022-06-12 14:09 143阅读 0赞 输入红包总金额,红包数。 输出派发的红包金额。 #include <stdio.h> #include <stdlib.h> #include <sys/time.h> #define RAND(min,max) \ ((min) + (max)*((double)random()/RAND_MAX)) int main(int argc, char *arg[]) { double total, randmax, min = 0.01, bonuses; int num; if( argc < 3 ) { printf("usage: %s <total> <num>\n", arg[0]); } total = atof(arg[1]); num = atoi(arg[2]); struct timeval tv; gettimeofday(&tv, NULL); srandom(tv.tv_sec ^ tv.tv_usec); int i; for(i=1; i<num; i++) { randmax = (total-(num-i)*min)/(num-i); bonuses = RAND(min*100, randmax*100)/100; bonuses = (long(bonuses*100)/100.00); total -= bonuses; printf("the %dth red envelope:\t%.02f\n", i, bonuses); } printf("the %dth red envelope:\t%.02f\n", num, total); return 0; } \[root@lghvm001 tiny\_alg\]\# ./a.out 100 10 the 1th red envelope: 5.93 the 2th red envelope: 6.52 the 3th red envelope: 7.70 the 4th red envelope: 11.04 the 5th red envelope: 6.65 the 6th red envelope: 15.46 the 7th red envelope: 7.33 the 8th red envelope: 9.09 the 9th red envelope: 27.51 the 10th red envelope: 2.77 \[root@lghvm001 tiny\_alg\]\#
还没有评论,来说两句吧...