程序代码:
#include <stdio.h>
#include <stdlib.h>
int sNum[10];
int *pScanNum = sNum;
int comp(const void *p, const void*q) {
return (*(char*)p - * (char*)q);
}
int scanNum(void) {
int i;
int s = 0;
L:
for(i = 0; i < 10; i++) {
scanf("%d", (pScanNum + i));
s += *(pScanNum + i);
if(s > 50) {
s -= *(pScanNum + i);
*(pScanNum + i) = 0;
break;
}
}
if(0 == s) {
printf("不能少于一个非 0 数...请重新输入\n");
goto L;
}
for(i = 0; i < 10; i++) {
if(*(pScanNum + i) < 0) {
printf("不能输入负数...请重新输入\n");
s = 0;
goto L;
}
}
return s;
}
int main(void) {
int i, j, k, sum, *pnum, *sortA, nozero = 0;
sum = scanNum();
pnum = (int*)malloc(sum * sizeof(int));
for(i = 0; i < 10; i++) {
for(j = 0; j < * (pScanNum + i); j++) {
*(pnum + k) = i;
k++;
if(k == sum) {
break;
}
}
}
for(i = 0; i < sum; i++) {
if(*(pnum + i) != 0) {
nozero++;
}
}
sortA = (int*)malloc(nozero * sizeof(int));
for(i = 0, j = 0; i < sum; i++) {
if(*(pnum + i) != 0) {
*(sortA + j) = *(pnum + i);
j++;
}
}
qsort(sortA, nozero, sizeof(int), comp);
if(0 == (sum - *pScanNum)) {
printf("最小数位数为1...值为: 0");
} else {
printf("最小数位数为%d...值为: %d", sum, *sortA);
for(i = 0; i < (*pScanNum); i++) {
printf("0");
}
for(i = 1; i < nozero; i++) {
printf("%d", *(sortA + i));
}
}
printf("\n");
free(pnum);
free(sortA);
return 0;
}