2017년 8월 21일 월요일

중복되지 않게 나열하기 알고리즘


 int SET[8] = { 0, 1, 2, 3, 4, 5, 6, 7 };
 int LIST[8] = { 3, 5, 7, 2, 7, 0, 0, 3 };
 int INDEX[8];
 short k,j,ii=0;
 for(ii=0;ii<8;ii++){
  LIST[ii] = rand()%8;
 }
 // 순서 정하기 
 for(ii=0;ii<8;ii++){
  for(j=0; j<8; j++){
   if( SET[j] == LIST[ii] ) INDEX[ii] = j;
  }
  INDEX[ii] = INDEX[ii] % 8;
 }
 // 순서가 현재 것이 다음들 것과 같은 것이 있으면 다음들 것을 + 1 처리
 // 이전 것들과도 비교.
 for(ii=0;ii<8;ii++){
  for(j=ii+1; j<8; j++){
   if( INDEX[ii] == INDEX[j] ){
    int uni = (INDEX[ii]+1)%8;
    for(k=0; k<ii; k++){
     if( INDEX[k] == uni ){
      uni = (uni+1)%8;
      k=-1; // ++이 수행됨.
     }
    }
    INDEX[j] = uni;
   }
  }
 }

 CString bf, str("");
 int sum = 0;
 for(ii=0;ii<8;ii++){
  sum += INDEX[ii];
  bf.Format( "[%d] SET=%d LIST=%d INDEX=%d \r\n" , ii, SET[ii], LIST[ii], INDEX[ii] ); str += bf;
 }
 bf.Format( "    SUM =%d \r\n" , sum ); str += bf;
// if( sum != 28 ) 
  AfxMessageBox(str);