微信红包算法

#include <iostream>
#include <stdio.h>      /* printf, scanf, puts, NULL */
#include <stdlib.h>     /* srand, rand */
#include <time.h>       /* time */
using namespace std;



int main(void)
{
    int total = 10;
    int num = 8;
    int minuim = 0.01;
    
    srand (time(NULL));
    for(int i = 1; i< num; i++){
        float safe = (total - (num - i) * minuim)*1.0/(num - i);
        float money = (rand() %(int) (safe*100 - minuim* 100 ) + minuim * 100) * 1.0 / 100;
        cout << i <<" " << " money = "<< money << " res = "<< total - money <<endl;
        total -= money;
    }
    cout << 8 << " " << " money = "<< total  << " res = "<< 0 <<endl;


}

 

posted @ 2017-02-27 22:47  WillWu  阅读(317)  评论(0编辑  收藏  举报