这样的题目因为关键字少,所以就可以转换为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("+");
}
}
本文介绍了一个简单的程序设计思路,用于帮助数学初学者Xenia计算加法。通过输入只包含数字1、2、3的算式,程序会重新排列这些数字,使其按非递减顺序排列,从而让Xenia能够轻松地进行计算。
722

被折叠的 条评论
为什么被折叠?



