思考:这道题用的是插入排序的算法思想。
#include<stdio.h> #include<cmath> int main() { int n; char d; int r[101]; while (scanf_s("%d%c", &n, &d) != EOF) { if (n == 0) break; for (int i = 1; i <= n; i++) { int a; scanf_s("%d", &a); r[i] = a; } for (int i = 2; i <= n; i++) { r[0] = r[i]; int j = i - 1; while (abs(r[0]) > abs(r[j])) { r[j + 1] = r[j]; j = j - 1; } r[j + 1] = r[0]; } for (int i = 1; i <= n; i++) { if (i!=n) printf("%d ", r[i]); else printf("%d ", r[i]); } } }