→ Caesar08: 麻煩變數名字不要取l,這樣看的都不知道是1還是l 02/01 18:15
已更正 感謝
※ 編輯: rareone (36.232.214.57), 02/01/2016 18:35:29
嗚......我自己找到答案了˙3˙:m[j]=>>hold
※ 編輯: rareone (36.232.214.57), 02/01/2016 18:41:45
開發平台(Platform): (Ex: VC++, GCC, Linux, ...)
Xcode
額外使用到的函數庫(Library Used): (Ex: OpenGL, ...)
C++ iostream
問題(Question):
插入排序的while只跑了一次
餵入的資料(Input):
7
7 6 5 4 3 2 1
預期的正確結果(Expected Output):
1 2 3 4 5 6 7
錯誤結果(Wrong Output):
6 5 4 3 2 1 7
程式碼(Code):(請善用置底文網頁, 記得排版)
#include <iostream>
using namespace std;
int main(){
int m[1000]={0},opp,hold;
while (cin>>opp){
for(size_t j=0;j<opp;j++){
cin>>m[j];
}
for (size_t j,i=1; i<opp; i++) {
j=i;
hold=m[j];
while ((m[j-1]>m[j])&&(j!=0)) {
m[j]=m[j-1];
j--;
}
m[j]=hold;
cout<<j<<endl;;;;;
}
for (size_t a=0; a<opp-1; a++) {
cout<<m[a]<<" ";
}
cout<<m[opp-1]<<endl;
}
}
補充說明(Supplement):
中間那行印出來是要確認while跑幾次用的
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 36.232.214.57
※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1454318147.A.F69.html