我的算法是,通过不断拆分最大的红包,完成所有红包金额的分配。
先把总金额存入红包列表中,然后开始。
第一步:从列表中找出已分配的红包中最大的红包金额,通过随机数将这个红包金额拆分成两个金额,然后保存在红包列表中。
第二步:继续第一步,直到红包拆分完毕。
为了方便运算,金额统一转换为分,输出结果时再转换回元和分。
头文件:
#pragma once
const unsigned int g_unMaxMoney = 10000 * 100; // 最大允许发10000元红包
class CSendRadPacket
{
public:
CSendRadPacket();
~CSendRadPacket();
bool CalRadPacket(unsigned int unYuan, unsigned int unFen, unsigned int unCount);
void PrintRadPacket();
protected:
unsigned int GetMaxNum();
void SaveMoney(unsigned int unNum);
protected:
unsigned int *m_pPackeData; // 保存计算好的每一个红包金额
unsigned int m_unPacketCount; // 红包数量
unsigned int m_unAllMoney; // 红包总金额
};
cpp代码:
#include "stdafx.h"
#include "CSendRadPacket.h"
#include <iostream>
#include <iomanip>
using namespace s