#include <stdio.h>
#include <stdlib.h>
/**
* 選擇排序法
* @param a 排序數組
* @param len 元素個數
*/
void selction_sort(int a[], int len) {
int i, j, t;
for (i = 0; i < len - 1; i++) {
for (j = i + 1; j < len; j++) {
if (a[i] < a[j]) {
t = a[i];
a[i] = a[j];
a[j] = t;
}
}
}
}
void main() {
int m, n, t, i, c, *p;
/*輸入數字n*/
scanf("%d", &n);
/*分析位數c*/
t = n;
c = 0;
while (t > 0) {
c++;
t /= 10;
}
/*分解數字n*/
t = n;
i = 0;
p = (int *) malloc(sizeof(int) * c);
while (t > 0) {
p[i++] = t % 10;
t /= 10;
}
/*排序數組p*/
selction_sort(p, c);
/*數組還原m*/
m = 0;
i = 0;
while (i < c) {
m *= 10;
m += p[i++];
}
/*輸出結果*/
printf("n = %d\n", n);
printf("m = %d\n", m);
printf("(m + n) / 2 = %lf", ((m + n) / 2.0));
}
由用戶輸入一個正整數n,分析出該正整數的每一位,然後將各位數字從大到小重新排序後得到正整數m,輸出m和n的平均值。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.