c语言输出两个小写字母,按顺序排列
时间: 2024-05-14 11:12:50 浏览: 14
可以用以下代码实现:
```c
#include <stdio.h>
int main() {
char ch1, ch2;
printf("请输入两个小写字母,用空格隔开:\n");
scanf("%c %c", &ch1, &ch2);
if (ch1 < ch2) {
printf("%c %c\n", ch1, ch2);
} else {
printf("%c %c\n", ch2, ch1);
}
return 0;
}
```
首先提示用户输入两个小写字母,然后使用scanf函数读取输入的两个字符。然后用if语句判断两个字符的大小关系,如果ch1小于ch2,则按顺序输出ch1和ch2;否则按顺序输出ch2和ch1。最后返回0表示程序正常结束。
相关问题
用c语言实现哈夫曼编码 输入一行字符串,由长度不超过255个字符的小写英文字母组成要求输出有若干行,每行由两部分组成,一个字母和该字母出现的频率,频率按高到低排序,频率相同则按asc码的先后顺序排列
好的,下面是一个C语言的实现示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXN 26
struct HuffmanNode {
char ch;
int freq;
struct HuffmanNode *left, *right;
};
struct HeapNode {
struct HuffmanNode *data;
struct HeapNode *next;
};
struct Heap {
struct HeapNode *head, *tail;
};
struct HuffmanNode* createHuffmanNode(char ch, int freq) {
struct HuffmanNode *node = (struct HuffmanNode*)malloc(sizeof(struct HuffmanNode));
node->ch = ch;
node->freq = freq;
node->left = node->right = NULL;
return node;
}
struct HeapNode* createHeapNode(struct HuffmanNode *data) {
struct HeapNode *node = (struct HeapNode*)malloc(sizeof(struct HeapNode));
node->data = data;
node->next = NULL;
return node;
}
struct Heap* createHeap() {
struct Heap *heap = (struct Heap*)malloc(sizeof(struct Heap));
heap->head = heap->tail = NULL;
return heap;
}
void push(struct Heap *heap, struct HuffmanNode *data) {
struct HeapNode *node = createHeapNode(data);
if (heap->head == NULL) {
heap->head = heap->tail = node;
} else {
heap->tail->next = node;
heap->tail = node;
}
}
struct HuffmanNode* pop(struct Heap *heap) {
struct HeapNode *node = heap->head;
heap->head = heap->head->next;
if (heap->head == NULL) {
heap->tail = NULL;
}
struct HuffmanNode *data = node->data;
free(node);
return data;
}
void buildHuffmanTree(struct Heap *heap) {
while (heap->head != heap->tail) {
struct HuffmanNode *left = pop(heap);
struct HuffmanNode *right = pop(heap);
struct HuffmanNode *parent = createHuffmanNode('\0', left->freq + right->freq);
parent->left = left;
parent->right = right;
push(heap, parent);
}
}
void printHuffmanCode(struct HuffmanNode *root, char *buf, int depth) {
if (!root->left && !root->right) {
buf[depth] = '\0';
printf("%c %s\n", root->ch, buf);
return;
}
if (root->left) {
buf[depth] = '0';
printHuffmanCode(root->left, buf, depth + 1);
}
if (root->right) {
buf[depth] = '1';
printHuffmanCode(root->right, buf, depth + 1);
}
}
int cmp(const void *a, const void *b) {
struct HuffmanNode **pa = (struct HuffmanNode**)a;
struct HuffmanNode **pb = (struct HuffmanNode**)b;
if ((*pa)->freq != (*pb)->freq) {
return (*pb)->freq - (*pa)->freq;
}
return (*pa)->ch - (*pb)->ch;
}
int main() {
char str[256];
int cnt[MAXN] = {0};
fgets(str, 256, stdin);
int len = strlen(str) - 1;
for (int i = 0; i < len; i++) {
cnt[str[i] - 'a']++;
}
qsort(cnt, MAXN, sizeof(int), cmp);
struct Heap *heap = createHeap();
for (int i = 0; i < MAXN; i++) {
if (cnt[i] == 0) {
break;
}
struct HuffmanNode *node = createHuffmanNode('a' + i, cnt[i]);
push(heap, node);
}
buildHuffmanTree(heap);
char buf[256];
printHuffmanCode(heap->head->data, buf, 0);
return 0;
}
```
上述代码中,我们使用了链表实现了一个最小堆,然后使用堆来构建哈夫曼树。最后,我们使用递归的方式输出每个叶子节点对应的哈夫曼编码。请注意,本题中要求按照频率的高低和字母的先后顺序排序,因此我们在排序函数中进行了处理。
用c语言写 给定一个字符串,里面仅仅由大写字母,小写字母和数字构成,现要求数字在字符串中的位置不能改变,但是需要按降序排列。大小写字母相对位置可以改变,也需要降序输出,大写字母要在小写字母的前面。 例如:aB1xb2C 输出:CB2xb1a
可以按照以下步骤进行操作:
1. 将字符串中的数字按照降序排列,可以使用快速排序等算法实现。同时记录下数字在原字符串中的位置。
2. 将字符串中的大小写字母分别存储在两个数组中,并且记录下它们在原字符串中的位置。
3. 对大小写字母数组分别按照降序排列。
4. 根据数字、大写字母、小写字母的位置信息,重新构造字符串,按照数字、大写字母、小写字母的顺序输出即可。
下面是一份参考代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LEN 100
void quick_sort(int *arr, int *pos, int left, int right) {
if (left >= right) {
return;
}
int pivot = arr[left];
int l = left, r = right;
while (l < r) {
while (l < r && arr[r] <= pivot) {
r--;
}
arr[l] = arr[r];
pos[l] = pos[r];
while (l < r && arr[l] >= pivot) {
l++;
}
arr[r] = arr[l];
pos[r] = pos[l];
}
arr[l] = pivot;
pos[l] = left;
quick_sort(arr, pos, left, l-1);
quick_sort(arr, pos, l+1, right);
}
void sort_digit(char *str, int *pos, int len) {
int *digits = (int *)malloc(len * sizeof(int));
int cnt = 0;
for (int i = 0; i < len; i++) {
if (isdigit(str[i])) {
digits[cnt++] = str[i] - '0';
pos[cnt-1] = i;
}
}
quick_sort(digits, pos, 0, cnt-1);
for (int i = 0; i < cnt; i++) {
str[pos[i]] = digits[i] + '0';
}
free(digits);
}
void sort_alpha(char *str, int *pos, int len, int is_upper) {
char *alpha = (char *)malloc(len * sizeof(char));
int cnt = 0;
for (int i = 0; i < len; i++) {
if (isalpha(str[i]) && isupper(str[i]) == is_upper) {
alpha[cnt++] = str[i];
pos[cnt-1] = i;
}
}
for (int i = 0; i < cnt; i++) {
for (int j = i+1; j < cnt; j++) {
if (alpha[i] < alpha[j]) {
char t = alpha[i];
alpha[i] = alpha[j];
alpha[j] = t;
int p = pos[i];
pos[i] = pos[j];
pos[j] = p;
}
}
}
for (int i = 0; i < cnt; i++) {
str[pos[i]] = alpha[i];
}
free(alpha);
}
int main() {
char str[MAX_LEN];
int digit_pos[MAX_LEN];
int upper_pos[MAX_LEN];
int lower_pos[MAX_LEN];
printf("Input a string: ");
fgets(str, MAX_LEN, stdin);
str[strcspn(str, "\n")] = '\0';
int len = strlen(str);
sort_digit(str, digit_pos, len);
sort_alpha(str, upper_pos, len, 1);
sort_alpha(str, lower_pos, len, 0);
printf("Output: %s\n", str);
return 0;
}
```