해결과 해석좀 부탁드립니다.
타키
질문 제목 : 해결과 해석좀 부탁드립니다.실행화면이 abcde abced abdec .... 뭐이런식으로 나와야 하는데 ..
실행을 시키면 abcd e abce d ... 이런식으로 나오다가 마지막 120개만.. 정상적으로 나오네요 ..
그리고 . 하나하나 해석할려니까 잘모르겠습니다질문 내용 :
#include stdio.h
#include string.h
#define swap(x,y,t) ( (t)=(x), (x)=(y), (y)=(t) )
long fac(int);
int perm(char*,int,int);
void main()
{
int n;
long f;
char list[]=abcde;
n=strlen(list); // abcde의 글자갯수 파악
f=fac(n);
printf(%d! = %ld\n, n, f);
perm(list,0,n);
}
long fac(int n)
{
long f=1,i;
for(i=n;i0;i--)
f*=i;
return f;
}
void perm(char *list, int i, int n)
/* generate all the permutations of list[i] to list[n]*/
{
int j, temp;
if (i == n) {
for (j = 0; j = n; j++)
printf(%c, list[j]);
printf();
}
else {
/* list[i] to list[n] has more than one permutation,
generate these recursively */
for ( j = i; j = n; j++) {
swap(list[i], list[j], temp);
perm(list, i+1, n);
swap(list[i], list[j], temp);
}
}
}
-
천칭자리 2023-12-19
perm 함수에 넘기는 세번째 인자는 첫번째 인자 배열의 길이가 아닌 마지막 항의 번호입니다. N - 1을 넘기세요.