|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
想請問各位如何把程式排序的過程都顯示出來
還有如何讓他橫向排列,我的是直的
#include <iostream>
using namespace std;
int main()
{
int x[5]={18,2,20,34,12};
int i,j;
for(i=0;i<5;i++){
cout << x[i] << endl;
}
for(i=0;i<4;i++){
for(j=0;j<4-i;j++){
if(x[j]>x[j+1]){
int x1 = x[j];
x[j] = x[j+1];
x[j+1] = x1;
}
}
}
for(i=0;i<5;i++){
cout << x[i] << endl;
}
return 0;
}
感謝各位
- #include <iostream>
- using namespace std;
- int main()
- {
- int x[5]={18,2,20,34,12};
- int i,j;
-
- for(i=0;i<5;i++){
- cout << x[i] << " ";
- }
- cout << endl << endl;
- for(i=0;i<4;i++){
- for(j=0;j<4-i;j++){
- if(x[j]>x[j+1]){
- cout << "交换:" << x[j] << " " << x[j+1] << endl;
- int x1 = x[j];
- x[j] = x[j+1];
- x[j+1] = x1;
- continue;
- }
- cout << "保持不变:" << x[j] << endl;
- }
- cout << "交换后为:" ;
- for(int k=0; k<5; k++)
- {
- cout << x[k] << " ";
- }
- cout << endl;
- cout << "-------------第"<< i+1 <<"轮交换结束---------------" << endl;
-
- }
- cout << endl << endl;
- for(i=0;i<5;i++){
- cout << x[i] << " ";
- }
- cout << endl << endl;
- return 0;
- }
复制代码
|
|