题目一开始没看懂
#include <stdio.h>
//数组选择排序
void sortArray(int array[], int length)
{
int index, temp;
for (int i = 0; i < length - 1; i++)
{
index = i;
for (int j = i + 1; j < length; j ++)
{
if (array[j] < array[index])
{
index = j;
}
}
if (i != index)
{
temp = array[i];
array[i] = array[index];
array[index] = temp;
}
}
}
//返回第一个非零元素的索引
int indexInArray(int array[], int length)
{
int index = 0;
for (int i = 0; i < length; i++)
{
if (array[i] != 0)
{
index = i;
break;
}
}
return index;
}
//交换
void swap(int *a, int *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}
int main(void)
{
int array[50]; //保存有效数字
int i, index;
int count, length;
length = 0; //有效数字的个数
//将有效数字保存在数组中
for (i = 0; i < 10; i++)
{
scanf("%d", &count);
while (count > 0)
{
array[length] = i;
length++;
count--;
}
}
sortArray(array, length);
index = indexInArray(array, length);
if (index != 0)
{
swap(&array[0], &array[index]);
}
for (i = 0; i < length; i++)
{
printf("%d", array[i]);
}
printf("
");
return 0;
}