//第二题
#include
#include
#include
#include
bool greater100 ( int value ) {
return value >=100;
}
bool between0_100 ( int value ) {
return value >=0 && value < 100;
}
bool negative ( int value ) {
return value < 0;
}
void main( ) {
using namespace std;
vector
vector
vector
vector
srand((unsigned) time(NULL));;
int i;
for ( i = 0 ; i < 10 ; i++ )
{
v1.push_back( -100+rand()%1100 );
}
random_shuffle( v1.begin( ), v1.end( ) );
cout << "随机数 ( " ;
for ( Iter1 = v1.begin() ; Iter1 != v1.end() ; Iter1++ )
cout << *Iter1 << " ";
cout << ")." << endl;
Iter_100=partition ( v1.begin(), v1.end(), greater100 );
Iter_0=partition ( Iter_100, v1.end(), between0_100 );
cout << endl << "划分后 ( " ;
for ( Iter1 = v1.begin() ; Iter1 != v1.end() ; Iter1++ )
cout << *Iter1 << " ";
cout << ")." << endl;
cout << "大小等于100 ( " ;
for ( Iter1 = v1.begin( ) ; Iter1 != Iter_100 ; Iter1++ )
cout << *Iter1 << " ";
cout << ")." << endl;
cout << "0到100 ( " ;
for ( Iter1 = Iter_100 ; Iter1 != Iter_0 ; Iter1++ )
cout << *Iter1 << " ";
cout << ")." << endl;
cout << "小于0 ( " ;
for ( Iter1 = Iter_0 ; Iter1 != v1.end() ; Iter1++ )
cout << *Iter1 << " ";
cout << ")." << endl;
}
第二题
#include
#include
#include
int main()
{
int a[50];
int i, r;
srand((unsigned)time( NULL ));
for(i = 0; i < 50; ++i)
{
while (1)
{
r = rand() % 1000;
if ((r >= 100) && ((r % 3) == 0) && ((r % 7) == 0) && (r & 0x1))
{
a[i] = r;
break;
}
}
}
for (i = 0; i < 100; ++i)
{
printf("%d ", a[i]);
if ((i + 1) % 10 == 0)
putchar('\n');
}
system("PAUSE");
return 0;
}
#include
#include
#include
#define MAX_RANDOM_NUM 100
void sort(int pNum[],int nCount);
void main (int argc, char *argv[])
{
int pRandNum[MAX_RANDOM_NUM] = {0};
DWORD dwBegin = GetTickCount();//起始时间
for (int i = 0; i < MAX_RANDOM_NUM; i++)
{
pRandNum[i] = rand();//产生100各随机整数
}
sort(pRandNum,MAX_RANDOM_NUM);
for (int j = 0; j < 10; j++)
{
printf("%d ",pRandNum[j]);
}
DWORD dwEnd = GetTickCount();//结束时间
DWORD dwTotal = dwEnd - dwBegin;//程序运行的时间
}
//排序,大->小,前面10个就是最大的了
void sort(int pNum[],int nCount)
{
int nTemp = 0;
for (int k = 0; k < nCount; k++)
{
for (int m = k + 1; m < nCount; m++)
{
if (pNum[k] < pNum[m])
{
nTemp = pNum[m];
pNum[m] = pNum[k];
pNum[k] = nTemp;
}
}
}
}