Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/130.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 我的升序排序问题是一个好的解决方案吗? #包括 使用名称空间std; int main(){ //问题#2升序 整数num1、num2、num3、小型、中型、大型; cin>>num1>>num2>>num3; 如果(num1_C++_If Statement_C++17_Minmax - Fatal编程技术网

C++ 我的升序排序问题是一个好的解决方案吗? #包括 使用名称空间std; int main(){ //问题#2升序 整数num1、num2、num3、小型、中型、大型; cin>>num1>>num2>>num3; 如果(num1

C++ 我的升序排序问题是一个好的解决方案吗? #包括 使用名称空间std; int main(){ //问题#2升序 整数num1、num2、num3、小型、中型、大型; cin>>num1>>num2>>num3; 如果(num1,c++,if-statement,c++17,minmax,C++,If Statement,C++17,Minmax,您的代码不好,因为 您为排序写了太多的行,这是一种非常常用的算法,并且有一个标准库可用于排序 编写大量代码会产生大量的bug 我更喜欢用C++来排序。 #include <iostream> using namespace std; int main() { // Problem #2 Ascending Order int num1, num2, num3, small, medium, large; cin >> num1 >> num2 >

您的代码不好,因为

  • 您为排序写了太多的行,这是一种非常常用的算法,并且有一个标准库可用于排序
编写大量代码会产生大量的bug

<>我更喜欢用C++来排序。
#include <iostream>
using namespace std;

int main() {
// Problem #2 Ascending Order
int num1, num2, num3, small, medium, large;
cin >> num1 >> num2 >> num3;

if(num1 <= num2 && num1 <= num3) {
    small = num1;
    if(num2 <= num3) {
        medium = num2;
        large = num3;
    } else {
        large = num2;
        medium = num3;
    }
} else if(num2 <= num1 && num2 <= num3) {
    small = num2;
    if(num3 <= num1) {
        medium = num3;
        large = num1;
    } else {
        large = num3;
        medium = num1;
    }
} else {
    small = num3;
    if(num2 <= num1) {
        medium = num2;
        large = num1;
    } else {
        large = num2;
        medium = num1;
    }
}

cout << small << " " << medium << " " << large;
return 0;
#包括
#包括
内部主(空){
constexpr int N=3;
整数[N];
对于(int i=0;i>num[i])){

对于三个数字的排序问题,这是一个不错的解决方案。它很容易理解,性能也很好。而且很容易验证它的正确性


但是在现实世界或学术界,如果你被要求做的下一件事不是对四个数字进行排序,或对
n
数字进行排序,你应该感到非常惊讶。你的方法不适用于
n
的任何其他值,并且不能真正扩展。

如果你不允许使用数组和更改为变量
num1
num2
num3
输入值,然后我可以建议以下解决方案,仅使用三个if语句

#include <iostream>
#include <algorithm>

int main(void) {
    constexpr int N = 3;
    int num[N];
    for (int i = 0; i < N; i++) {
        if (!(std::cin >> num[i])) {
            std::cerr << "read error" << std::endl;
            return 1;
        }
    }
    std::sort(num, num + N);
    for (int i = 0; i < N; i++) {
        if (i > 0) std::cout << ' ';
        std::cout << num[i];
    }
    return 0;
}
然后输出将是

5 3 4
5 3 4 
另一种在某些方面与您的方法类似的方法是

small = 3, medium = 4, large = 5
然后输出将是

5 3 4
5 3 4 
对于原始解决方案,您可以减少if语句条件中的子表达式数量

small = 3, medium = 4, large = 5
#包括
int main()
{
int num1,num2,num3;
int小型、中型、大型;
标准:cin>>num1>>num2>>num3;
如果(非(num2std::cout不。你的代码太复杂了。说真的,我需要花一些精力来阅读它并理解每一个细节。它是非常重复的,并且有很多可能发生简单的错误,破坏正确性。对三个数字进行排序代价太高了

不要重新发明一个轮子,而要知道你的方向

您需要最小的数字(
std::min
),最大的数字(
std::max
)和其他数字:

#include <iostream>

int main() 
{
    int num1, num2, num3;
    int small, medium, large;

    std::cin >> num1 >> num2 >> num3;
    

    if ( not ( num2 < num1 ) and not ( num3 < num1 ) ) 
    {
        small = num1;
        if ( not ( num3 < num2 ) )
        {
            medium = num2;
            large = num3;
        }
        else
        {
            medium = num3;
            large = num2;
            
        }
    }       
    else if ( not ( num3 < num2 ) ) 
    {
        small = num2;
        if ( not ( num3 < num1 ) )
        {
            medium = num1;
            large = num3;
        }
        else
        {
            medium = num3;
            large = num1;
            
        }
    } 
    else 
    {
        small = num3;
        if ( not ( num2 < num1 ) )
        {
            medium = num1;
            large = num2;
        }
        else
        {
            medium = num2;
            large = num1;
            
        }
    }

    std::cout << "small = " << small
              << ", medium = " << medium
              << ", large = " << large
              << '\n';
              
    return 0;
}
#包括

帕特里克·罗伯茨指出了溢出并提供了非溢出解决方案

即使,例如,作为练习,你会避免使用标准算法,你仍然应该使用函数。它们不需要太复杂。a
int max(int a,int b)
将有助于大大简化你的代码。

为什么有这么多变量

#include <algorithm>
#include <iostream>

int main(){
    int a = 42;
    int b = 2;
    int c = -13;

    int smallest = std::min({a,b,c});
    int biggest = std::max({a,b,c});
    // int middle = a+b+c-smallest-biggest;      // might overflow
    int middle = a ^ b ^ c ^ smallest ^ biggest; // no overflow

    std::cout << smallest << " " << middle << " " << biggest << "\n";
}

我遗漏了什么吗?

那么,代码正在运行,您需要一个新的解决方案吗?这样的代码是不可能找到工作的,因为它是一个非常死板的解决方案。对于改进,我建议(1)能够对数组中的数字进行排序,这样您就可以对任意数量的值进行排序(2)在实现排序方法之前,编写练习排序方法的测试。@Den Jason当被要求对3个数字进行排序时,如果你花一周的时间来开发一个通用排序算法,你也不会得到这份工作。我设法对解决方案多考虑了一会儿;)您可以使用
intmiddle=a^b^c^minimen^maxist;
并完全避免溢出。我同意写太多行是一个问题,因为它会产生很多引入错误的机会。但是,我也会犹豫使用
std::sort
对三个项目进行排序。上面显示了在3~5行代码内实现这一点的许多方法,而我认为你更适合这项任务。好眼力!谢谢!我检查了很多次,并仔细检查了一遍,没有发现这个问题。
int small, medium, large;
std::cin >> small >> medium >> large;

// Force "small" to be the smallest.
if (small > medium) std::swap(small, medium);
if (small > large)  std::swap(small, large);

// Now to deal with "medium" and "large"
if (medium > large) std::swap(medium, large);