2 문자 P 및 C와 N- 가감 번호 (짝수) 예. 만약 N = 4 => CCPP, PPCC, CPPC, PCCP, CPCP, PCPC (아이디어는 단지 Ps의 no = Cs = no의 이런 종류의 해결책을 보여 주어야한다)순열 문자
Q
순열 문자
-1
A
답변
1
순열 C
에서
int N;
cin >> N;
string str = string(N/2, 'C') + string(N/2, 'P');
do {
cout << str << endl;
} while(next_permutation(str.begin(), str.end()));
, 당신은
permutation function 자신을 작성해야합니다.
typedef int bool;
bool true = 1;
bool false = 0;
int compare (const void *a, const void * b)
{ return (*(char *)a - *(char *)b); }
void swap (char* a, char* b)
{
char t = *a;
*a = *b;
*b = t;
}
// This function finds the index of the smallest character
// which is greater than 'first' and is present in str[l..h]
int findCeil (char str[], char first, int l, int h)
{
// initialize index of ceiling element
int ceilIndex = l, i;
// Now iterate through rest of the elements and find
// the smallest character greater than 'first'
for (i = l+1; i <= h; i++)
if (str[i] > first && str[i] < str[ceilIndex])
ceilIndex = i;
return ceilIndex;
}
// Print all permutations of str in sorted order
void permute (char str[])
{
// Get size of string
int size = strlen(str);
// Print permutations one by one
bool isFinished = false;
while (! isFinished)
{
int i;
// print this permutation
printf ("%s \n", str);
// Find the rightmost character which is smaller than its next
// character. Let us call it 'first char'
for (i = size - 2; i >= 0; --i)
if (str[i] < str[i+1])
break;
// If there is no such chracter, all are sorted in decreasing order,
// means we just printed the last permutation and we are done.
if (i == -1)
isFinished = true;
else
{
// Find the ceil of 'first char' in right of first character.
// Ceil of a character is the smallest character greater than it
int ceilIndex = findCeil(str, str[i], i + 1, size - 1);
// Swap first and second characters
swap(&str[i], &str[ceilIndex]);
// Sort the string on right of 'first char'
qsort(str + i + 1, size - i - 1, sizeof(str[0]), compare);
}
}
}
int main(void) {
int N;
char *a = NULL;
if(1 != scanf("%d\n", &N)) {
fprintf(stderr, "Can not read the value of N\n");
return 1;
}
a = malloc(N + 1);
if(!a) {
fprintf(stderr, "Out of mem\n");
return 1;
}
memset(a, 'C', N/2);
memset(a + N/2, 'P', N/2);
a[N] = '\0';
permute(a, 0, strlen(a) - 1);
free(a);
return 0;
}
+0
C에서 어떻게 할 수 있습니까? –
+0
''P ''앞에''C ''를 넣는 것이 중요합니다. 왜냐하면'str'은 모든 순열에 대해 반복되도록 정렬되어야하기 때문입니다. – Jarod42
+0
'C'의 경우, next_permutaion 함수를 작성해야합니다. 시간이 될 때마다 대답을 업데이트 할 것입니다. –
관련 문제
- 1. 임의의 문자 순열
- 2. 은 (순열?) 문자
- 3. 문자 배열을 통해 순열 변환
- 4. 후행 문자 연결에 대한 문자열 순열
- 5. 알파벳의 n 번째 6 문자 순열 계산
- 6. PHP : X 문자 26 자의 순열
- 7. 큰 문자 집합에서 반복되는 Char # 순열
- 8. 문자열의 순열
- 9. 순열 생성
- 10. 일부 고정 숫자가있는 순열
- 11. 암호 해독과 순열
- 12. Python에서 순열 얻기, itertools
- 13. 슬라이스의 모든 순열 얻기
- 14. 정말 큰 순열 목록
- 15. 중복을 포함하여 여러 순열
- 16. 인코딩 방법 대체 순열
- 17. 순열 연산자에서 LotusScript로 순열 연산자를 어떻게 변환합니까?
- 18. 반복되지 않는 순열
- 19. Python - 설정된 문자 수 목록의 모든 순열 목록 가져 오기
- 20. 순열 비용
- 21. 파이썬 순열
- 22. 재귀가없는 순열?
- 23. MySQL 순열
- 24. 재귀 순열
- 25. 엑셀의 순열
- 26. 순열/변형
- 27. 조합과 순열
- 28. 주문 순열
- 29. 순열 문제
- 30. ArrayList의 순열
는 N = 4, N = 2? –
그리고 지금까지 시도한 것은 무엇입니까? 그리고 하나의 언어 만 선택하십시오. 언어 선택에 따라 솔루션이 달라질 수 있습니다. –
질문이 있으십니까? – Lundin