編程題#3:奇偶排序(二)
阿新 • • 發佈:2017-10-07
log cnblogs 保存 比較 如果 () return names space
#include <iostream> using namespace std; int main() { int n, a[1000]; // 一共n個數,n不超過1000。a用來保存這些數 cin >> n; // 輸入n個數 for (int i = 0; i < n; i++) { cin >> a[i]; } // 冒泡,不斷比較相鄰的兩個數,如果順序錯了,就交換 for (int i = 0; i < n - 1; i++) { for (int j = 1; j < n - i; j++) {if (a[j - 1] > a[j]) { int temp = a[j]; a[j] = a[j - 1]; a[j - 1] = temp; } } } // 依次輸出 for (int i = 0; i < n; i++) { cout << a[i] << endl; } return 0; }
編程題#3:奇偶排序(二)