[解答] 計程上機考Q9

看板NTUBIME96-HW作者 (柯南42真是妙)時間22年前 (2003/12/01 00:57), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串1/1
#include <stdafx.h> #include <iostream> using std::cout; using std::endl; #include <iomanip> using std::setw; void bubble( int [], int ); int main() { const int size = 15; int a[ size ] = { 3, 8, 10, 30, 2, 16, 27, 13, 22, 17, 42, 33, 38, 29, 14 }; int i; cout << "Data items in original order" << endl; for ( i = 0; i < size; i++ ) cout << setw( 4 ) << a[ i ]; cout << endl << endl; bubble( a, size ); cout << "Data items in ascending order" << endl; for ( i = 0; i < size; i++ ) cout << setw( 4 ) << a[ i ]; cout << endl << endl; return 0; } void bubble( int a[], int size ) { int i, j, hold; int i, j, hold; for ( i = 0; i < size - 1; i++ ) for ( j = 0; j < size - 1; j++ ) if ( a[ j ] > a[ j + 1 ] ) { hold = a[ j ]; a[ j ] = a[ j + 1 ]; a[ j + 1 ] = hold; } } -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 218.174.164.157
文章代碼(AID): #_oY5mhZ (NTUBIME96-HW)