Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/146.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++新手。对于我第一个关于快速排序算法的问题,当我运行代码时,它不会停止请求输入。尽管我一直在从不同的IDE运行代码,但问题仍然存在 #include <bits/stdc++.h> #include <iostream> using namespace std; void swap(int &x, int &y){ int temp; temp=x; x=y; y=temp; } void part(int *a, int lo, int hi){ if(lo<hi){ int p=lo; int s=lo; int e=hi; while(s<e){ while(a[s]<=a[p]){ if(s!=hi+1){ s++; }else{ s--; } } cout<<s<<endl; while(a[e]>a[p]){ e--; } cout<<e<<endl; while(s<e){ swap(a[s], a[e]); cout<<a[s]<<a[e]; } } swap(a[p], a[e]); part(a, lo, e-1); part(a, e+1, hi); } } int main(){ int n; cin>>n; int *arr=new int[n]; for(int i=0; i<n; i++){ cin>>arr[i]; }"it wont go after this" part(arr, 0, n-1); for(int i=0; i<n; i++){ cout<<arr[i]<<" "; } return 0; }_C++ - Fatal编程技术网

控制台不会停止请求输入 我是C++新手。对于我第一个关于快速排序算法的问题,当我运行代码时,它不会停止请求输入。尽管我一直在从不同的IDE运行代码,但问题仍然存在 #include <bits/stdc++.h> #include <iostream> using namespace std; void swap(int &x, int &y){ int temp; temp=x; x=y; y=temp; } void part(int *a, int lo, int hi){ if(lo<hi){ int p=lo; int s=lo; int e=hi; while(s<e){ while(a[s]<=a[p]){ if(s!=hi+1){ s++; }else{ s--; } } cout<<s<<endl; while(a[e]>a[p]){ e--; } cout<<e<<endl; while(s<e){ swap(a[s], a[e]); cout<<a[s]<<a[e]; } } swap(a[p], a[e]); part(a, lo, e-1); part(a, e+1, hi); } } int main(){ int n; cin>>n; int *arr=new int[n]; for(int i=0; i<n; i++){ cin>>arr[i]; }"it wont go after this" part(arr, 0, n-1); for(int i=0; i<n; i++){ cout<<arr[i]<<" "; } return 0; }

控制台不会停止请求输入 我是C++新手。对于我第一个关于快速排序算法的问题,当我运行代码时,它不会停止请求输入。尽管我一直在从不同的IDE运行代码,但问题仍然存在 #include <bits/stdc++.h> #include <iostream> using namespace std; void swap(int &x, int &y){ int temp; temp=x; x=y; y=temp; } void part(int *a, int lo, int hi){ if(lo<hi){ int p=lo; int s=lo; int e=hi; while(s<e){ while(a[s]<=a[p]){ if(s!=hi+1){ s++; }else{ s--; } } cout<<s<<endl; while(a[e]>a[p]){ e--; } cout<<e<<endl; while(s<e){ swap(a[s], a[e]); cout<<a[s]<<a[e]; } } swap(a[p], a[e]); part(a, lo, e-1); part(a, e+1, hi); } } int main(){ int n; cin>>n; int *arr=new int[n]; for(int i=0; i<n; i++){ cin>>arr[i]; }"it wont go after this" part(arr, 0, n-1); for(int i=0; i<n; i++){ cout<<arr[i]<<" "; } return 0; },c++,C++,我无法得到答案。希望你们能帮我。提前感谢代码没有卡在输入端,而是遇到了一个无限循环,要检查同样的情况,您可以打印cout并检查语句是否打印 cout<<" Read the array elements"<<endl; part(arr, 0, n-1); 发生无限循环的一种情况是直线 while(a[s]<=a[p]){ if(s!=hi+1){ s++; }else{

我无法得到答案。希望你们能帮我。提前感谢

代码没有卡在输入端,而是遇到了一个无限循环,要检查同样的情况,您可以打印cout并检查语句是否打印

cout<<" Read the array elements"<<endl;
part(arr, 0, n-1);
发生无限循环的一种情况是直线

    while(a[s]<=a[p]){
        if(s!=hi+1){
        s++;
        }else{
            s--;
        }
    }
假设以下情况:

s的值达到hi+1,然后 我们减小s的值,然后s变为hi 下一步,因为s的值!=hi+1,增加s的值,现在它等于hi+1。我们回到第一点。
你还可以给程序添加什么输入吗?在第一个for循环之前,试着打印n的值,可能你的输入不正确,结果是一个巨大的数字。如果你刚刚开始学习,那么帮自己一个忙,从以下两个问题中学习:和。不要做第一个,很少考虑第二个,永远不要同时做这两个。嘿@舒巴姆班索德,我指出了一个无限循环的情况下,在代码中。你能试着解决这个问题吗?int*arr=newint[n];并不是真正有效的C++代码。我相信MSVC和其他一些编译器会允许这样做,但是如果您需要一个在编译时大小未知的容器,您可能需要一个std::vector。