【发布时间】:2020-02-25 04:46:02
【问题描述】:
我想创建两个数组,它们都不能有重复的值,如果可能的话,我会在同一个循环中寻找答案。
for(int i=0;i<MAX;i++){//MAX=vector's number of values
scanf("%d", &vector[i]);//reads the number from the keyboard
while(j!=i){//Looks for each vector[i] if there is a vector[j], with j<i, with that value already. j starts at 0
if(vector[j]==vector[i]){
printf("\nWrite another number\n");
scanf("%d",&vector[i]);
j=0;
}
else
j++;
}
}
对于第二个循环,我寻找相同的结果,一组不重复的值,但带有随机数。
for(int i=0;i<MAX;i++){
vector[i]=(rand()%MAX-MIN+1)+MIN;
while(j!=i){
if(vector[j]==vector[i]){
vector[i]=(rand()%MAX-MIN+1)+MIN;;
j=0;
}
else
j++;
}
}
我一直在尝试用一种方法在两个区间之间生成一个随机数,比如 (2,10)U(12,20),所以每当你找到一个重复的值时,下一个随机数不包含它,但我找不到任何办法这样做。
感谢您的帮助
【问题讨论】:
标签: c arrays loops for-loop random