Search results

'알고리즘'에 해당하는 글들

  1. 2007/09/08  중복되지 않는 경우의 수의 조합 구하기.

// 경우의수.cpp : 콘솔 응용 프로그램에 대한 진입점을 정의합니다.
//

#include "stdafx.h"
#include "경우의수.h"
#include <conio.h>
#include <stdio.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// 유일한 응용 프로그램 개체입니다.

CWinApp theApp;
#define MAX 5
using namespace std;
unsigned long cnt=0;
int monoselect(int curser,char monocode[MAX],int cnt);
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
 int nRetCode = 0;

 // MFC를 초기화합니다. 초기화하지 못한 경우 오류를 인쇄합니다.
 if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
 {
  // TODO: 오류 코드를 필요에 따라 수정합니다.
  _tprintf(_T("심각한 오류: MFC를 초기화하지 못했습니다.\n"));
  nRetCode = 1;
 }
 else
 {
  char monocode[MAX];
  for(int i=0;i<MAX;i++){
   monocode[i]='0';
  }
  int cnt=0;
  cnt = monoselect(MAX,monocode,cnt);
  printf("%ld",cnt);
  getch();
  // TODO: 응용 프로그램의 동작은 여기에서 코딩합니다.
 }
 
 return nRetCode;
}

int monoselect(int curser,char monocode_s[MAX],int cnt){
 char monocode[MAX];

 for(int i=curser-1;i>=0;i--){
  for(int a=0;a<MAX;a++){
   monocode[a] = monocode_s[a];
  }
  monocode[i]='1';
  cnt++;
  for(int j=0;j<MAX;j++){
   printf("%c",monocode[j]);
  }
 
   printf("\n");
  cnt=monoselect(i,monocode,cnt);
 }
 return cnt;
}


중복되지 않는 경우의 수 조합을 구하는 프로그램입니다.
재귀호출을 이용한 순환알고리즘으로 쉽게 풀 수 있습니다.

대략 5자리의 수라고 하면  11111 / 11110 / 11101 / 11011 / 10111 / 01111 이런 조합들을 출력하는거죠.
이올린에 북마크하기(0) 이올린에 추천하기(0)
2007/09/08 20:24 2007/09/08 20:24
─ tag  ,
openclose