작업은 다음과 같습니다. 복제없이 n보다 작은 k 개의 양수를 생성하십시오.n보다 작은 k 개의 고유 번호 생성
내 방법은 다음과 같습니다.
먼저 우리가이 번호를 작성해야 K의 배열의 크기를 만들 :
int a[] = new int[k];
//Now I am going to create another array where I check if (at given number
//position is 1 then generate number again, else put this number in an
//array and continue cycle.
나는 코드와 설명으로 여기에 조각을 넣어.
int a[]=new int[k];
int t[]=new int[n+1];
Random r=new Random();
for (int i==0;i<t.length;i++){
t[i]=0;//initialize it to zero
}
int m=0;//initialize it also
for (int i=0;i<a.length;i++){
m=r.nextInt(n);//random element between 0 and n
if (t[m]==1){
//I have problems with this. I want in case of duplication element
//occurs repeat this steps afain until there will be different number.
else{
t[m]=1;
x[i]=m;
}
}
그래서 내 문제를 구체화합니다. t [m] == 1. 그것은이 요소가 이미 발생 했으므로 새 번호를 생성하기 위해 을 생성하지만 문제는 생성 된 숫자의 수입니다. 은 i == 0이면 중복 요소가 발생하고 계속 작성하므로 i가 전환됩니다. == 1. 반복 단계에 대해 goto처럼해야합니다. 또는 :
for (int i=0;i<x.length;i++){
loop:
m=r.nextInt(n);
if (x[m]==1){
continue loop;
}
else{
x[m]=1;
a[i]=m;
continue; //Continue next step at i=1 and so on.
}
}
Java에서이 코드가 필요합니다.
대문자 : 귀하의 경우에 유용 할 수 있습니다 플로이드의 구현은,
다음은? 또한 코드 스 니펫을'code' 블록에 넣어야합니다. – Eric
영어로 학습하십시오. –
같은 사람이 이전에 물어 본 질문에 따르면 :이 숙제는 무엇입니까? –