随机函数问题~
#include <conio.h>
#include <time.h>
#include <iostream>
using namespace std;
int main()
{
int i,j;
int a[100]={0};
int b[100]={0};
srand((unsigned) time(NULL));
for(i = 0; i < 5; i++)
{
a[i]=rand() % 5;
printf("%2d",a[i]);
}
printf("\n");
for(i= 0; i < 5; i++)
{
b[i]=rand() % 5;
printf("%2d",b[i]);
}
getch();
}
怎样使两个random函数上下相等时 如(4,4)(2,2)
和重复出现的(4,0),(0,4)发生时重新运行 展开
#include <stdio.h>
#include <conio.h>
#include <time.h>
#include <iostream>
using namespace std;
int main()
{
int i, j;
int a[100] = {
0
};
int b[100] = {
0
};
srand((unsigned)time(NULL));
for (i = 0; i < 5; i++) {
a[i] = rand() % 5;
printf("%2d", a[i]);
}
printf("\n");
i = 0;
bool b1;
while (i < 5) {
b[i] = rand() % 5;
if (a[i] != b[i]) {
b1 = false;
for (int j = 0; j < i - 1; j++) {
if (((a[i] == a[j]) && (b[i] == b[j])) ||
((a[i] == b[j]) && (b[i] == a[j]))) // 有多余括号,易读
b1 = true;
}
if (!b1) {
printf("%2d", b[i]);
i++;
}
}
}
getch();
}
大哥 你太帅气了 谢谢你啊~~ 好多问题基本都没人回答的 真心感谢