高中生程式解題系統:明明愛排列
題目連結 http://zerojudge.tw/ShowProblem?problemid=a225 。 此題修改 C語言qsort 的比較函數 compare ,或是 C++語言sort 的比較函數 comp 即可。 程式碼: # include <cstdlib> # include <cstdio> using namespace std ; int compare ( const void *a, const void *b) { int c = *( int *)a; int d = *( int *)b; if (c % 10 < d % 10 ) { return -1 ;} else if (c % 10 > d % 10 ) { return 1 ;} else return c < d; } int main ( void ) { int num = 0 ; while ( scanf ( "%d" , &num) != EOF) { int data[num]; for ( int i = 0 ; i < num; i++) scanf ( "%d" , &data[i]); qsort(data, num, sizeof ( int ), compare); for ( int i = 0 ; i < num; i++) printf ( "%d " , data[i]); printf ( "\n" ); } return 0 ; } 若您覺得文章寫得不錯,請點選文章上的廣告,來支持小編,謝謝。