抽奖程序:如何从1-500个数字中随机选择1,2,3等奖?在线等给分

558047 2008-01-09 04:09:58
想要做一个简单的抽奖程序
就是从1-500的数字中随机选择10个数,作为3等奖
然后再从剩下的数字中选择5个数,作为2等奖
然后再冲剩下的数字中选择1个数,作为1等奖
我只要用random函数
但不会控制抽另一个奖项时排除先前已经产生的数字
要求:抽3等奖时,屏幕一次就把这10个数字都显示出来,而非一个一个的出来
2等奖也是如此
...全文
1903 33 打赏 收藏 转发到动态 举报
写回复
用AI写文章
33 条回复
切换为时间正序
请发表友善的回复…
发表回复
dfdscx 2009-05-14
  • 打赏
  • 举报
回复
才发现竟然是被挖出来的帖子。
而且还没结贴。。
morris88 2009-05-13
  • 打赏
  • 举报
回复
hemiya 2009-05-13
  • 打赏
  • 举报
回复
晕,一年多了还不结。
僵哥是古墓丽影!!!!!!!!!!!!!
僵哥 2009-05-13
  • 打赏
  • 举报
回复
我挖
僵哥 2008-01-28
  • 打赏
  • 举报
回复
变得快一点,没人看得出来中间有跳动的
=======================
这样的程序是经不起检验的,检验程序可以跟踪到显示用的TextOut/DrawText
Richardw 2008-01-26
  • 打赏
  • 举报
回复
感觉不惑和安吉儿的方法都不错,生成的16个数先放在一边,前台做着不断变换数据的样子,一点三等奖,把前10个数交出来就得了

把数据变换显示和16个数的提取计算分开想就好了,不断变的数只是一个样子而已,变得快一点,没人看得出来中间有跳动的 ^_^
558047 2008-01-25
  • 打赏
  • 举报
回复
僵哥,我试着更改你的第一段代码
可是还是执行的特别慢
僵哥 2008-01-25
  • 打赏
  • 举报
回复
难道不可以自己优化一下效率?人家给你提供的只是一种思想,而不是最终代码。否则,你不如出钱让人给你写一个。
558047 2008-01-25
  • 打赏
  • 举报
回复
老大们
在抽奖的时候
需要那种屏幕数字滚动的效果
然后一按按键,停止
然后出来中奖号码
可是这几段代码实现的效果都滚动的太慢了
不知道怎么解决阿?
mzy100 2008-01-25
  • 打赏
  • 举报
回复
写个循环链表,抽中的节点删除,省去比较部分,应该能提高速度
僵哥 2008-01-25
  • 打赏
  • 举报
回复

bool Finish = false;
void __fastcall TForm1::Button1Click(TObject *Sender)
{
int F1[1]/*一等奖*/,
F2[5]/*二等奖*/,
F3[10]/*三等奖*/;
TStaticText *ShowText;

int count=500;
int *tls=new int[500];
try {
//1-500已经在列表当中
for(int i = 0; i < 500; i++)
tls[i] = i+1;
//>>>>
//randomize();
//<<<<
//抽10个三等奖
for(int j3=0;j3<10;j3++){

int Index = random(count);
//>>>>
ShowText = dynamic_cast<TStaticText *>(FindComponent("F3_"+IntToStr(j3)));
Finish = false;
Caption = "正在抽取第"+IntToStr(j3+1)+"/10位三等奖...";
while (!Finish){
if (Index == count - 1) {
Index = 0;
}
else
Index ++;
ShowText->Caption = IntToStr(tls[Index]);
Application->ProcessMessages();
}
//<<<<

F3[j3] = tls[Index];
if (count != Index) {
tls[Index] = tls[count-1];
tls[count-1] = F3[j3];
}
count --;
}
//>>>>
//randomize();
Caption = "10位三等奖抽取完毕.";
Finish = false;
while (!Finish){
Sleep(1);
Application->ProcessMessages();
}
//<<<<
//抽5个二等奖
for(int j2=0;j2<5;j2++){
int Index = random(count);
//>>>>
ShowText = dynamic_cast<TStaticText *>(FindComponent("F2_"+IntToStr(j2)));
Finish = false;
Caption = "正在抽取第"+IntToStr(j2+1)+"/5位三等奖...";
while (!Finish){
if (Index == count - 1) {
Index = 0;
}
else
Index ++;
ShowText->Caption = IntToStr(tls[Index]);
Application->ProcessMessages();
}
//<<<<
F2[j2] = tls[Index];
if (count != Index) {
tls[Index] = tls[count-1];
tls[count-1] = F2[j2];
}
count --;
}
//>>>>
//randomize();
Caption = "5位二等奖抽取完毕.";
Finish = false;
while (!Finish){
Sleep(1);
Application->ProcessMessages();
}
//<<<<
//抽出一等奖
int Index = random(count);
//>>>>
ShowText = F1_0;
Finish = false;
Caption = "正在抽取第1/1位一等奖...";
while (!Finish){
if (Index == count - 1) {
Index = 0;
}
else
Index ++;
ShowText->Caption = IntToStr(tls[Index]);
Application->ProcessMessages();
}
//<<<<
F1[0] = tls[Index];
if (count != Index) {
tls[Index] = tls[count-1];
tls[count-1] = F1[0];
}
count --;
//抽奖完毕,一等奖在F1中,二等奖在F2中,三等奖在F3中
AnsiString Result;
Result.sprintf("抽奖完毕!\r\n一等奖:%d\r\n二等奖:%d,%d,%d,%d,%d\r\n三等奖:%d,%d,%d,%d,%d,%d,%d,%d,%d,%d",F1[0],F2[0],F2[1],F2[2],F2[3],F2[4],F3[0],F3[1],F3[2],F3[3],F3[4],F3[5],F3[6],F3[7],F3[8],F3[9]);
ShowMessage(Result);
}
__finally{
delete[] tls;
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
Finish = true;
}
//---------------------------------------------------------------------------
僵哥 2008-01-25
  • 打赏
  • 举报
回复
	int F1[1]/*一等奖*/,
F2[5]/*二等奖*/,
F3[10]/*三等奖*/;
int count=500;
int *tls=new int[500];
try {
//1-500已经在列表当中
for(int i = 0; i < 500; i++)
tls[i] = i+1;
randomize();
//抽10个三等奖
for(int j3=0;j3<10;j3++){
int Index=random(count);
F3[j3] = tls[Index];
if (count != Index) {
tls[Index] = tls[count-1];
tls[count-1] = F3[j3];
}
count --;
}
randomize();
//抽5个二等奖
for(int j2=0;j2<5;j2++){
int Index=random(count);
F2[j2] = tls[Index];
if (count != Index) {
tls[Index] = tls[count-1];
tls[count-1] = F2[j2];
}
count --;
}
randomize();
//抽出一等奖
int Index=random(count);
F1[0] = tls[Index];
if (count != Index) {
tls[Index] = tls[count-1];
tls[count-1] = F1[0];
}
count --;
//抽奖完毕,一等奖在F1中,二等奖在F2中,三等奖在F3中
}
__finally{
delete[] tls;
}
laowang2 2008-01-25
  • 打赏
  • 举报
回复
upup
僵哥 2008-01-25
  • 打赏
  • 举报
回复
把StringList改为数组,并且不要使用String,而使用Int类型。增加一个Count记录能够参与抽奖的号码数。
mubing102877 2008-01-20
  • 打赏
  • 举报
回复
有意思!
路过路人乙 2008-01-11
  • 打赏
  • 举报
回复
强贴,,MARK。。。。随即取数啊。。正想学呢。。。。
yhe888 2008-01-11
  • 打赏
  • 举报
回复
人多力量大
Waiting4you 2008-01-10
  • 打赏
  • 举报
回复
STL里有一个随机打乱功能:
random_shuffle is an overloaded name; there are actually two random_shuffle functions.
template <class RandomAccessIterator>
void random_shuffle(RandomAccessIterator first, RandomAccessIterator last);

template <class RandomAccessIterator, class RandomNumberGenerator>
void random_shuffle(RandomAccessIterator first, RandomAccessIterator last,
RandomNumberGenerator& rand)

constantine 2008-01-10
  • 打赏
  • 举报
回复
提取16个出来后先显示10个啊,接着再显示5,最后1个,用个变量表示到了哪一步不就行了吗?脑筋要动起来
558047 2008-01-10
  • 打赏
  • 举报
回复
学习中
加载更多回复(12)

13,825

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder相关内容讨论区
社区管理员
  • 基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧