这样的题目因为关键字少,所以就可以转换为counting sort的思想去解决。这样时间效率就只有O(n)了。
void HelpfulMaths()
{
int A[4] = {0};
int a;
while (scanf("%d", &a) != EOF)
{
A[a]++;
getchar();
}
int total = A[1]+A[2]+A[3]-1;
for (unsigned i = 0; i < A[1]; i++, total--)
{
printf("1");
if (total) printf("+");
}
for (unsigned i = 0; i < A[2]; i++, total--)
{
printf("2");
if (total) printf("+");;
}
for (unsigned i = 0; i < A[3]; i++, total--)
{
printf("3");
if (total) printf("+");
}
}