[問題] 取亂數存到陣列
開發平台(Platform): (Ex: VC++, GCC, Linux, ...)
DEV C++
額外使用到的函數庫(Library Used): (Ex: OpenGL, ...)
問題(Question):
我的程式是要取20個 10~50 之間不重複的整數存到陣列存放
餵入的資料(Input):
預期的正確結果(Expected Output):
錯誤結果(Wrong Output):
程式進入無窮迴圈,無結果
程式碼(Code):(請善用置底文網頁, 記得排版)
#include<iostream>
#include<time.h>
using namespace std;
int RND(int,int);
float random();
int main(){
int temp,count=0;
int result[20];
bool flag;
for(int i=0;i<20;i++)
result[i]=0;
srand(time(0));
while(count<20){
flag=false;
temp=RND(10,50);
for(int i=0;i<count;i++){
if(temp==result[i])
flag=true;
}
if(flag==true)
continue;
result[count]=temp;
count++;
}
system("pause");
return 0;
}
float random(){
float d=rand()%10;
d=d/10;
return d;
}
int RND(int m,int n){
int r=(int)(m+(n-m)*random()+0.5);
return r;
}
補充說明(Supplement):
請問到底哪裏出錯,感覺沒錯0.0
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 111.252.241.88
→
03/04 21:47, , 1F
03/04 21:47, 1F
→
03/04 21:47, , 2F
03/04 21:47, 2F
→
03/05 09:47, , 3F
03/05 09:47, 3F
→
03/05 09:47, , 4F
03/05 09:47, 4F
→
03/05 09:51, , 5F
03/05 09:51, 5F
→
03/05 09:51, , 6F
03/05 09:51, 6F
推
03/05 11:32, , 7F
03/05 11:32, 7F
→
03/05 11:32, , 8F
03/05 11:32, 8F
推
03/05 14:07, , 9F
03/05 14:07, 9F