1. 程式人生 > >依次從鍵盤輸入20個無序整數,刪除陣列重複元素並按從小到大排序

依次從鍵盤輸入20個無序整數,刪除陣列重複元素並按從小到大排序

#include<stdio.h> //依次從鍵盤輸入20個無序整數,刪除陣列重複元素並按從小到大排序 #include #include using namespace std; #define maxn 1000 int a[maxn]; bool cmp ( int a, int b ) { return a > b; }int main() { int i, n; printf ( “請輸入要排序的元素個數:” ); scanf ( “%d”, &n ); printf ( “請依次輸入%d個數:”, n ); for ( i = 0; i < n; i++ ) scanf ( “%d”, &a[i] ); sort ( a, a + n); for ( i = 0; i < n; i++ ) printf ( “%3d”, a[i] ); cout<<endl; int index = 1,b[20],m;//索引 b[0] = a[0]; m = a[0]; for (int i = 1; i < 20; ++i) { if (a[i] != m) { m = a[i]; b[index++] = a[i]; } } for (int i = 0; i < index; ++i) { cout << b[0] << " "; } return 0; }