HJ26 字符串排序

文章讲述了作者对HJ26字符串排序问题的探索,从最初的代码实现到优化版本,重点在于字符比较和排序逻辑的改进,最终第三版代码解决了排序问题并通过测试。
摘要由CSDN通过智能技术生成

提示:文章

文章目录

前言

前期疑问:
本文目标:


一、背景

最近

二、HJ26 字符串排序

HJ26 字符串排序

2.1 代码

2.1.1 第一版

\#include <stdio.h>

\#include <stdlib.h>

int compar(void* a, void* b) {

  return *(int*)a - *(int*)b;

}

int main() {

  int array[10] = {24, 3, 4, 6, 8, 3, 7, 2, 5, 9};

  qsort(array, 10, sizeof(int), compar);

  for (int i = 0; i < 10; i++) {//printf("%d ", array[i]);

  }

  char str[1001] = {'\0'};

  while (gets(str) != NULL) { // 注意 while 处理多个 case// 64 位输出请用 printf("%lld") to//printf("%s\n", str);int len = strlen(str);for (int i = 0; i < len; i++) {for (int j = i + 1; j < len; j++) {if ( (str[i] < 'a' && str[i] > 'z') && (str[i] < 'A' && str[i] > 'Z') ) {break;} else if ( (str[j] < 'a' && str[j] > 'z') && (str[j] < 'A' && str[j] > 'Z') ) {break;} else {if (abs(str[i] - str[j]) == 32) {continue;} else if ( (str[i] >= 'a' && str[i] <= 'z') && (str[j] >= 'a' &&

​                str[j] <= 'z') ) {if (str[i] > str[j]) {char temp = str[i];

​              str[i] = str[j];

​              str[j] = temp;}} else if ( (str[i] >= 'A' && str[i] <= 'Z') && (str[j] >= 'A' &&

​                str[j] <= 'Z') ) {if (str[i] > str[j]) {char temp = str[i];

​              str[i] = str[j];

​              str[j] = temp;}} else if ( (str[i] >= 'a' && str[i] <= 'z') && (str[j] >= 'A' &&

​                str[j] <= 'Z') ) {if (str[i] - 32 > str[j]) {char temp = str[i];

​              str[i] = str[j];

​              str[j] = temp;}} else if ( (str[i] >= 'A' && str[i] <= 'Z') && (str[j] >= 'a' &&

​                str[j] <= 'z') ) {if (str[i] + 32 > str[j]) {char temp = str[i];

​              str[i] = str[j];

​              str[j] = temp;}}}}}printf("%s\n", str);

  }

  return 0;

}

2.1.2 第二版

#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include "string.h"
#include "stdbool.h"

bool isNeedExchange(char a, char b)
{ 
    if( (!isalpha(a)) || (!isalpha(b)) )
    {
        return false;
    }

    if(tolower(a) - tolower(b) < 0)
    {
        return false;
    }
    
    return true;
}

int main() {
    char str[1001] = {'\0'};
    while (fgets(str, 1001, stdin) != NULL) { // 注意 while 处理多个 case
        // 64 位输出请用 printf("%lld") to
        //printf("%s\n", str);
        strtok(str, "\n");
        int len = strlen(str);
        for (int i = 0; i < len; i++) {
            for (int j = i + 1; j < len; j++) {
                if(isNeedExchange(str[i], str[j]))
                {
                    char temp = str[i];
                    str[i] = str[j];
                    str[j] = temp;
                }
            }
        }
        printf("%s\n", str);
    }
    return 0;
} 

//打印信息
自测输入:A Famous Saying: Much Ado About Nothing (2012/8).
预期输出:A aaAAbc dFgghh: iimM nNn oooos Sttuuuy (2012/8).
实际输出:A AaaAbc dFgghh: iiMm nNn ooooS sttuuuy (2012/8).

第二版代码虽然简化了代码,但是还是不对的。
至此,可能是我的算法不对。需要换算法。

2.1.3 第三版

只能说用对算法很简单

#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include "string.h"

int main() {
    char str[1001] = {'\0'};
    char dstStr[1001] = {'\0'};
    int dstStrIndex = 0;
    while (fgets(str, 1001, stdin) != NULL) { // 注意 while 处理多个 case
        // 64 位输出请用 printf("%lld") to
        //printf("%s\n", str);
        strtok(str, "\n");
        int len = strlen(str);
        for (int i = 0; i < 26; i++) {
            for (int j = 0; j < len; j++) {
                if(str[j] - 'a' == i || str[j] - 'A' == i)
                {
                    dstStr[dstStrIndex++] = str[j];
                }
            }
        }

        dstStrIndex = 0;

        for(int i = 0; i < len; i++)
        {
            if(isalpha(str[i]))
            {
                str[i] = dstStr[dstStrIndex++];
            }
        }
        printf("%s\n", str);
    }
    return 0;
}

上面的代码ac了


总结

未完待续

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值