📄 random.cpp
字号:
#include <stdlib.h>
#include <time.h>
#include <iostream.h>
#include <conio.h>
int random(int min,int max)
{
unsigned i=rand();
while(!(int(i%(max+1))>=min&&int(i%(max+1))<=max))
{
i=rand();
}
return i%(max+1);
}
void main()
{
cout<<"下面从10个票房中随机抽取6个票房:"<<endl;
char *str[10]=
{
{"票房1"},
{"票房2"},
{"票房3"},
{"票房4"},
{"票房5"},
{"票房6"},
{"票房7"},
{"票房8"},
{"票房9"},
{"票房10"}
};
int i;
int a[10];
srand((unsigned)time(NULL));
int k;
for(int j=0;j<6;j++)
{
rerand:
i=random(0,9);
for(k=0;k<j;k++)
if(i==a[k])
goto rerand;
a[j]=i;
cout<<"第"<<j+1<<"次:"<<str[i]<<endl;
}
getch();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -