You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C++三个输入字符串字母序排序问题:现有代码无法处理bac、bca输入

C++ 三字符串排序问题修复

问题描述

需要编写C++程序实现以下功能:从键盘输入三个未排序字符串存入变量a、b、c,排序后将最小字符串存入a,中间的存入b,最大的存入c。已编写的代码可正确处理abc、acb、cab、cba输入,但无法处理bac、bca输入,需修正逻辑问题。

原代码

#include <iostream>
#include <string>

using namespace std;

int main()
{
    string a, b, c, temp;

    cout << "enter a word" << endl;
    cin >> a;
    cout << "enter a different word" << endl;
    cin >> b;
    cout << "enter a third word" << endl;
    cin >> c;
    if (a < b && a < c) {
        if (b < c) {
            cout << endl;
            cout << a << endl;
            cout << b << endl;
            cout << c << endl;
        }
        else {
            temp = c;
            c = b;
            b = temp;
            cout << endl;
            cout << a << endl;
            cout << b << endl;
            cout << c << endl;
        }
    }
    else {
        if (b > c) {
            temp = a;
            a = c;
            c = temp;
            cout << endl;
            cout << a << endl;
            cout << b << endl;
            cout << c << endl;
        }
        else {
            temp = a;
            a = c;
            c = temp;
            temp = b;
            b = a;
            a = temp;
            cout << endl;
            cout << a << endl;
            cout << b << endl;
            cout << c << endl;
        }
    }

    return 0;
}

问题分析

原代码逻辑存在漏洞:当a不是最小字符串时,直接进入else分支,但未区分**b是最小字符串**的情况(比如bac、bca输入),导致这两类输入的排序逻辑完全错误。

解决方案

方案1:完善手动分支逻辑

通过分步判断确保a为最小值,再调整b和c的顺序,覆盖所有6种输入排列情况:

#include <iostream>
#include <string>

using namespace std;

int main()
{
    string a, b, c, temp;

    cout << "enter a word" << endl;
    cin >> a;
    cout << "enter a different word" << endl;
    cin >> b;
    cout << "enter a third word" << endl;
    cin >> c;

    // 第一步:确保a是最小的字符串
    if (b < a && b < c) {
        temp = a;
        a = b;
        b = temp;
    } else if (c < a && c < b) {
        temp = a;
        a = c;
        c = temp;
    }

    // 第二步:此时a已最小,只需调整b和c的顺序
    if (c < b) {
        temp = b;
        b = c;
        c = temp;
    }

    // 输出排序结果
    cout << endl;
    cout << a << endl;
    cout << b << endl;
    cout << c << endl;

    return 0;
}

方案2:使用标准库sort函数(更简洁)

借助C++标准库的sort函数,将字符串存入数组后直接排序,代码更简洁且不易出错:

#include <iostream>
#include <string>
#include <algorithm> // 包含sort函数的头文件

using namespace std;

int main()
{
    string strs[3];
    cout << "enter a word" << endl;
    cin >> strs[0];
    cout << "enter a different word" << endl;
    cin >> strs[1];
    cout << "enter a third word" << endl;
    cin >> strs[2];

    sort(strs, strs + 3); // 对数组中的字符串排序

    // 将排序后的值赋值回a、b、c
    string a = strs[0], b = strs[1], c = strs[2];

    // 输出结果
    cout << endl;
    cout << a << endl;
    cout << b << endl;
    cout << c << endl;

    return 0;
}

说明

  • 方案1通过分步逻辑调整,确保覆盖所有输入情况,适合理解手动排序的实现思路;
  • 方案2利用标准库函数,代码简洁高效,是实际开发中的常用写法,避免手动分支可能出现的逻辑遗漏。

内容的提问来源于stack exchange,提问作者lealdrake

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.17 07:45:13