Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <math.h>
- #define MAX_LENGTH 1000
- int cmp(const void* a, const void* b) {
- char* f = (char*)a;
- char* s = (char*)b;
- if (strlen(f) == strlen(s)) {
- while (*f == *s) {
- f++;
- s++;
- }
- return *f - *s;
- }
- return strlen(f) - strlen(s);
- }
- char* make_palindrome(char* word, int k, char add) {
- char* c = malloc(sizeof(char) * 1000 + 2 * k);
- for (int j = 0; j < k; j++) {
- c[j] = add;
- }
- strcpy(c + k, word);
- char* p = malloc(k * sizeof(char) + 1);
- for (int i = 0; i < k; i++) {
- p[i] = add;
- }
- p[k] = '\0';
- strcat(c + k + strlen(word), p);
- return c;
- }
- int main() {
- int n;
- scanf("%d", &n);
- char palindromes[MAX_LENGTH][1000];
- int m = 0;
- char even[500][32] = {{"aa"}, {"bb"}};
- char odd[500][32] = {{"a"}, {"b"}};
- int q = 1;
- char temp[500][32];
- char temp2[500][32];
- for (int j = 0; j < 6; j++) {
- int l = 0;
- for (int i = 0; i < pow(2, q); i++) {
- strcpy(temp[l], make_palindrome(even[i], 1, 'a'));
- strcpy(temp[l + 1], make_palindrome(even[i], 1, 'b'));
- strcpy(temp2[l], make_palindrome(odd[i], 1, 'a'));
- strcpy(temp2[l + 1], make_palindrome(odd[i], 1, 'b'));
- l += 2;
- }
- for (int i = 0; i < pow(2, q + 1); i++) {
- strcpy(even[i], temp[i]);
- strcpy(odd[i], temp2[i]);
- strcpy(palindromes[m++], temp[i]);
- strcpy(palindromes[m++], temp2[i]);
- }
- q++;
- memset(temp, 0, sizeof(temp));
- memset(temp2, 0, sizeof(temp2));
- }
- strcpy(palindromes[m++], "");
- strcpy(palindromes[m++], "a");
- strcpy(palindromes[m++], "b");
- strcpy(palindromes[m++], "bb");
- strcpy(palindromes[m++], "aa");
- qsort(palindromes, m, 1000, cmp);
- for (int i = 0; i < n; i++) {
- printf("%4d. %s\n", i + 1, palindromes[i]);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement