这种题目由于keyword少,所以就能够转换为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("+");
}
}
版权声明:本文博客原创文章,博客,未经同意,不得转载。