가로,세로 값을 입력받아서
같은 숫자가 2번씩 나오게 1차원배열 이용해서 하는거.
예시로, 가로 4, 세로 5 받앗으면
1부터 10까지만!!! 2번씩 출력하ㅡㄴ거지
순서대로 2개씩 값을 배열에 차례대로 넣고,
그 차례로 된 배열을 랜덤하게 뽑아
출력하는 방법으로 하는거!
다했는데
배열 자체를 중복없이!!!?! 랜덤으로 출력하기가 힘듦
헬프
아래 소스 전문첨부
void FindingSameNumbers()
{
srand((unsigned)time(NULL)); //난수 초기화
char yesno;
int wd =0, hi =0;
int count =0;
while(1)
{
cout<<"Do you want to play \"finding the same number\" game? <y/n> ";
cin>>yesno;
if(yesno=='n') return; //대답 no일 경우 종료
else
{
while(1)
{
cout<<"Input the width: ";
cin>>wd;
cout<<"Input the height: ";
cin>>hi;
cout<<endl;
int area = wd * hi;
int number[100]={0};
int number2[100]={0};
if((area)%2 !=0)
{
cout<<"Input the width and height again!"<<endl<<endl;
continue;
}
else
//1부터 넓이/2 까지 숫자를 임의로 출력
//(예_넓이20인 경우 : 1부터10까지 2번씩)
{
for(int i=0; i<area; i++)
number[i]=(i/2)+1; //1부터 넓이의 절반 값까지 차례로 입력
//이 부분이 문제의 랜덤출력;;;
for(int i=0; i<area; i++){
int j = (rand()%area)+1;
number2[j]=number[i];
if(number2[j]==true){
}
//여기까지
if(count%wd==0) cout<<endl; //출력화면 줄 맞추기
if(count==area) break; //출력 완료
}
cout<<endl;
count=0; //사용한 count 초기화
break;
}
}
}
}
}
랜드 돌릴때 area 로 모듈러 연산하는데 이러면 1부터 넓이까지 값이 나오지 않나요?
아 아니구나 제가 잘못 봤네요 사과합니다
루프에서 이미 출력한 인덱스인지 아닌지 판단하면 될듯요