Untitled
unknown
plain_text
2 years ago
1.6 kB
3
Indexable
#include <stdio.h> enum Constants { SEQUENCE_SIZE = 10, }; int checkRepeat(int *a, int size) { int repeat[SEQUENCE_SIZE] = {0}; for (int i = 0; i < size; ++i) { if (a[i] >= 10 || a[i] < 0) return 1; repeat[a[i]]++; if (repeat[a[i]] > 1) return 1; } return 0; } void swap(int *a, int *b) { int temp = *a; *a = *b; *b = temp; } unsigned nextPermutation(int *a, int size) { int i = size - 2; while (i >= 0 && a[i] > a[i + 1]) i--; if (i == -1) return 0; for (int j = i + 1, k = size - 1; j < k; j++, k--) swap(&a[j], &a[k]); int j = i + 1; while (a[j] < a[i]) j++; swap(&a[i], &a[j]); return 1; } int main() { int size = 0; int flagBadInput = 0; char currentChar; int a[SEQUENCE_SIZE]; for (int i = 0; i < SEQUENCE_SIZE + 1; i++) { if (scanf("%c", ¤tChar) < 1 || (currentChar != '\n' && i == SEQUENCE_SIZE)) { printf("%c", currentChar); flagBadInput = 1; break; } if (currentChar == '\n') { break; } size++; a[i] = currentChar - '0'; } int count; int printable = 0; if (scanf("%d", &count) < 1 || flagBadInput || checkRepeat(a, size)) { printf("bad input"); return 0; } while (nextPermutation(a, size) && printable < count) { printable++; for (int i = 0; i < size; i++) printf("%d", a[i]); printf("\n"); } return 0; }
Editor is loading...