我有作业,我必须创建一个C程序,从最小到最大排序5个数字。我知道如何使用函数和if
语句轻松编程(使用>=
和<=
)。
然而,问题是,我只能使用printf
和scanf
,因此我必须计算>=
内的所有<=
和printf
}。我不允许使用三元运算符。
我一直在努力。所以我试着只排序3个数字,我仍然无法通过排序最小的数字。它只是保持打印1
。
// This printf is just trying to figure out the smallest number from 3 numbers provided but it keeps printing 1.
printf("The ascending order of the numbers are: %d ",
(
(num1 <= num2 && num1 <= num3) || // Checking if num1 is the smallest
(num2 <= num1 && num2 <= num3) || // Checking if num2 is the smallest
(num3 <= num2 && num3 <= num1) // Checking if num3 is the smallest
));
我提出的一个可能的解决方案是添加((num1 + num2 + num3) -1)
,因为如果其中一个语句为真(例如,如果num2
是最小的,则会打印1
,因为{ {1}}表示真实)。如果为false,则打印1
。所以我可以在技术上添加真实的语句0
。因此,如果-1
的陈述为真,我可以num2
。
答案 0 :(得分:7)
请注意,如果条件的计算结果为false,则结果为0
;如果为真,1
。所以你可以使用一个技巧(只要乘法和加法也不是禁止的) - 对于三个不同的值,如问题所示:
printf("The smallest number is: %d ",
(num1 * (num1 <= num2 && num1 <= num3) +
num2 * (num2 <= num1 && num2 <= num3) +
num3 * (num3 <= num1 && num3 <= num2)));
如果两个值相同且值也较小,则会出现问题。
如果你需要处理5个值,那么(如评论中所述)比繁琐更乏味。
printf("The smallest number is: %d ",
(num1 * (num1 <= num2 && num1 <= num3 && num1 <= num4 && num1 <= num5) +
num2 * (num2 <= num1 && num2 <= num3 && num2 <= num4 && num2 <= num5) +
num3 * (num3 <= num1 && num3 <= num2 && num3 <= num4 && num3 <= num5) +
num4 * (num4 <= num1 && num4 <= num2 && num4 <= num3 && num4 <= num5) +
num5 * (num5 <= num1 && num5 <= num2 && num5 <= num3 && num5 <= num4)));
这只是为了找到最小值;为其他每个案例工作很快就会变得荒谬。事实上,整个练习相当愚蠢,但它也是一些相当典型的课程。
经过一番思考之后,我认为你可以处理2或3个相同的数字(这基本上是user3386109中所说的comment)。
#include <stdio.h>
static void print_smallest(int num1, int num2, int num3)
{
printf("The smallest number of (%d, %d, %d) is %d\n",
num1, num2, num3,
(num1 * (num1 <= num2 && num1 <= num3) +
num2 * (num2 < num1 && num2 <= num3) +
num3 * (num3 < num1 && num3 < num2)));
}
int main(void)
{
for (int i = 1; i < 4; i++)
{
for (int j = 1; j < 4; j++)
{
for (int k = 1; k < 4; k++)
print_smallest(i, j, k);
}
}
return 0;
}
输出:
The smallest number of (1, 1, 1) is 1
The smallest number of (1, 1, 2) is 1
The smallest number of (1, 1, 3) is 1
The smallest number of (1, 2, 1) is 1
The smallest number of (1, 2, 2) is 1
The smallest number of (1, 2, 3) is 1
The smallest number of (1, 3, 1) is 1
The smallest number of (1, 3, 2) is 1
The smallest number of (1, 3, 3) is 1
The smallest number of (2, 1, 1) is 1
The smallest number of (2, 1, 2) is 1
The smallest number of (2, 1, 3) is 1
The smallest number of (2, 2, 1) is 1
The smallest number of (2, 2, 2) is 2
The smallest number of (2, 2, 3) is 2
The smallest number of (2, 3, 1) is 1
The smallest number of (2, 3, 2) is 2
The smallest number of (2, 3, 3) is 2
The smallest number of (3, 1, 1) is 1
The smallest number of (3, 1, 2) is 1
The smallest number of (3, 1, 3) is 1
The smallest number of (3, 2, 1) is 1
The smallest number of (3, 2, 2) is 2
The smallest number of (3, 2, 3) is 2
The smallest number of (3, 3, 1) is 1
The smallest number of (3, 3, 2) is 2
The smallest number of (3, 3, 3) is 3
计算最大值而不是最小值是微不足道的;只需使用>
代替<
。
计算中位数变得更难。我怀疑有一种比这更好的方法,但至少这是有效的。注意减去的项 - 省略它,当三个值相同时,中值加倍。
#include <stdio.h>
static void print_smallest(int num1, int num2, int num3)
{
printf("The sorted order of (%2d, %2d, %2d) is (%2d, %2d, %2d)\n",
num1, num2, num3,
(num1 * (num1 <= num2 && num1 <= num3) + /* Min1 */
num2 * (num2 < num1 && num2 <= num3) + /* Min2 */
num3 * (num3 < num1 && num3 < num2)), /* Min3 */
(num1 * (num1 >= num2 && num1 <= num3) + /* Med1 */
num2 * (num2 > num1 && num2 <= num3) + /* Med2 */
num3 * (num3 > num1 && num3 < num2) - /* Med3 */
num1 * (num1 == num2 && num1 == num3) + /* Med4 */
num1 * (num1 <= num2 && num1 >= num3) + /* Med5 */
num2 * (num2 < num1 && num2 >= num3) + /* Med6 */
num3 * (num3 < num1 && num3 > num2)), /* Med7 */
(num1 * (num1 >= num2 && num1 >= num3) + /* Max1 */
num2 * (num2 > num1 && num2 >= num3) + /* Max2 */
num3 * (num3 > num1 && num3 > num2)) /* Max3 */
);
}
int main(void)
{
int lo = -7; // +1, -2
int hi = +6; // +4, +4
int jp = +6; // +1, +2
for (int i = lo; i < hi; i += jp)
{
for (int j = lo; j < hi; j += jp)
{
for (int k = lo; k < hi; k += jp)
print_smallest(i, j, k);
}
}
return 0;
}
输出:
The sorted order of (-7, -7, -7) is (-7, -7, -7)
The sorted order of (-7, -7, -1) is (-7, -7, -1)
The sorted order of (-7, -7, 5) is (-7, -7, 5)
The sorted order of (-7, -1, -7) is (-7, -7, -1)
The sorted order of (-7, -1, -1) is (-7, -1, -1)
The sorted order of (-7, -1, 5) is (-7, -1, 5)
The sorted order of (-7, 5, -7) is (-7, -7, 5)
The sorted order of (-7, 5, -1) is (-7, -1, 5)
The sorted order of (-7, 5, 5) is (-7, 5, 5)
The sorted order of (-1, -7, -7) is (-7, -7, -1)
The sorted order of (-1, -7, -1) is (-7, -1, -1)
The sorted order of (-1, -7, 5) is (-7, -1, 5)
The sorted order of (-1, -1, -7) is (-7, -1, -1)
The sorted order of (-1, -1, -1) is (-1, -1, -1)
The sorted order of (-1, -1, 5) is (-1, -1, 5)
The sorted order of (-1, 5, -7) is (-7, -1, 5)
The sorted order of (-1, 5, -1) is (-1, -1, 5)
The sorted order of (-1, 5, 5) is (-1, 5, 5)
The sorted order of ( 5, -7, -7) is (-7, -7, 5)
The sorted order of ( 5, -7, -1) is (-7, -1, 5)
The sorted order of ( 5, -7, 5) is (-7, 5, 5)
The sorted order of ( 5, -1, -7) is (-7, -1, 5)
The sorted order of ( 5, -1, -1) is (-1, -1, 5)
The sorted order of ( 5, -1, 5) is (-1, 5, 5)
The sorted order of ( 5, 5, -7) is (-7, 5, 5)
The sorted order of ( 5, 5, -1) is (-1, 5, 5)
The sorted order of ( 5, 5, 5) is ( 5, 5, 5)
和以前一样,Pass 4中的代码对相对位置的三个数字的所有组合进行了全面测试。如果您需要阅读三个数字然后对它们进行排序(并且您不允许使用main()
,scanf()
,printf()
以外的循环或函数,那么就这样吧 - 你在您阅读了三个值后,可以立即将printf()
语句移植到main()
中:
#include <stdio.h>
int main(void)
{
int num1, num2, num3;
if (scanf("%d%d%d", &num1, &num2, &num3) != 3)
{
fprintf(stderr, "failed to read 3 integers\n");
return 1;
}
printf("The sorted order of (%2d, %2d, %2d) is (%2d, %2d, %2d)\n",
num1, num2, num3,
(num1 * (num1 <= num2 && num1 <= num3) + /* Min1 */
num2 * (num2 < num1 && num2 <= num3) + /* Min2 */
num3 * (num3 < num1 && num3 < num2)), /* Min3 */
(num1 * (num1 >= num2 && num1 <= num3) + /* Med1 */
num2 * (num2 > num1 && num2 <= num3) + /* Med2 */
num3 * (num3 > num1 && num3 < num2) - /* Med3 */
num1 * (num1 == num2 && num1 == num3) + /* Med4 */
num1 * (num1 <= num2 && num1 >= num3) + /* Med5 */
num2 * (num2 < num1 && num2 >= num3) + /* Med6 */
num3 * (num3 < num1 && num3 > num2)), /* Med7 */
(num1 * (num1 >= num2 && num1 >= num3) + /* Max1 */
num2 * (num2 > num1 && num2 >= num3) + /* Max2 */
num3 * (num3 > num1 && num3 > num2)) /* Max3 */
);
return 0;
}
使用随机数生成器(程序名sort3-53
)进行测试得出:
$ for i in $(range 0 9); do random -n 3 10 99 | sort3-53; done
The sorted order of (66, 62, 70) is (62, 66, 70)
The sorted order of (43, 99, 23) is (23, 43, 99)
The sorted order of (20, 46, 66) is (20, 46, 66)
The sorted order of (87, 82, 19) is (19, 82, 87)
The sorted order of (63, 29, 62) is (29, 62, 63)
The sorted order of (40, 66, 15) is (15, 40, 66)
The sorted order of (17, 13, 58) is (13, 17, 58)
The sorted order of (84, 50, 11) is (11, 50, 84)
The sorted order of (60, 86, 54) is (54, 60, 86)
The sorted order of (37, 33, 96) is (33, 37, 96)
$
您可以在我使用seq
的地方使用range
。我不确定有一个标准的PRNG程序类似于我使用的random
(和写过)。显示的调用生成3个10到99之间的随机数。
这里的整个过程是荒谬的 - 但这是因为可以使用的技术条件。如果需要对三个或更多数字进行排序,请将它们放入数组中,对数组进行排序,然后打印数组。如果做不到这一点,你应该交换值来查找排序顺序;它会大大减少所需的比较次数,并且没有乘法。
答案 1 :(得分:2)
这是一个简单的解决方案,可以轻松适应任意数量的值:
#include <stdio.h>
int main() {
int n[5], o[5], s[5];
while (scanf("%d%d%d%d%d", &n[0], &n[1], &n[2], &n[3], &n[4]) == 5) {
o[0] = (n[0] > n[1]) + (n[0] > n[2]) + (n[0] > n[3]) + (n[0] > n[4]);
o[1] = (n[1] >= n[0]) + (n[1] > n[2]) + (n[1] > n[3]) + (n[1] > n[4]);
o[2] = (n[2] >= n[0]) + (n[2] >= n[1]) + (n[2] > n[3]) + (n[2] > n[4]);
o[3] = (n[3] >= n[0]) + (n[3] >= n[1]) + (n[3] >= n[2]) + (n[3] > n[4]);
o[4] = (n[4] >= n[0]) + (n[4] >= n[1]) + (n[4] >= n[2]) + (n[4] >= n[3]);
s[o[0]] = n[0];
s[o[1]] = n[1];
s[o[2]] = n[2];
s[o[3]] = n[3];
s[o[4]] = n[4];
printf("%d %d %d %d %d\n", s[0], s[1], s[2], s[3], s[4]);
}
return 0;
}
请注意,while
仅用于重复输入/排序/输出阶段。如果不允许if
或while
语句,你可以摆脱它,但是我不知道如何测试成功的scanf()
转换,但是通过初始化可以避免未定义的行为:
#include <stdio.h>
int main() {
int n0, n1, n2, n3, n4, s[5];
n0 = n1 = n2 = n3 = n4 = 0;
scanf("%d%d%d%d%d", &n0, &n1, &n2, &n3, &n4);
s[(n0 > n1) + (n0 > n2) + (n0 > n3) + (n0 > n4)] = n0;
s[(n1 >= n0) + (n1 > n2) + (n1 > n3) + (n1 > n4)] = n1;
s[(n2 >= n0) + (n2 >= n1) + (n2 > n3) + (n2 > n4)] = n2;
s[(n3 >= n0) + (n3 >= n1) + (n3 >= n2) + (n3 > n4)] = n3;
s[(n4 >= n0) + (n4 >= n1) + (n4 >= n2) + (n4 >= n3)] = n4;
printf("%d %d %d %d %d\n", s[0], s[1], s[2], s[3], s[4]);
return 0;
}
答案 2 :(得分:0)
如果你被允许使用我认为你做的int
(你怎么能存储用户输入)为什么不使用具有正确排序的预先计算的表?
#include <stdio.h>
int map[2][2] = {
{1, 0},
{0, 1}
};
int main() {
int n[2] = {7, 5};
int tmp = n[0] >= n[1];
printf("1.) %d\n", n[map[tmp][0]]);
printf("2.) %d\n", n[map[tmp][1]]);
}
更新:这似乎适用于三个数字:
#include <stdio.h>
int map[2][2][2][3] = {
{
{
{2, 1, 0},
{1, 2, 0}
},
{
{2, 0, 1},
{0, 1, 0}
}
},
{
{
{0, 0, 0},
{1, 0, 2}
},
{
{0, 2, 1},
{0, 1, 2}
}
}
};
int main() {
int n[3] = {30, 59, 100};
int tmp1 = n[0] > n[2];
int tmp2 = n[0] > n[1];
int tmp3 = n[1] > n[2];
printf("1.) %d\n", n[map[tmp1][tmp2][tmp3][0]]);
printf("2.) %d\n", n[map[tmp1][tmp2][tmp3][1]]);
printf("3.) %d\n", n[map[tmp1][tmp2][tmp3][2]]);
}