Kat*_*e.P 3 c printf conditional if-statement
我有功课,我必须创建一个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
));
Run Code Online (Sandbox Code Playgroud)
我提出的一个可能的解决方案是添加,((num1 + num2 + num3) -1)因为如果其中一个语句为真(例如,如果num2是最小的,则会打印,1因为它1意味着为真).如果是假的,则打印出来0.所以我可以在技术上添加那个真实的陈述-1.所以,如果num2声明是真的,我能做到+ num2 - 1.
请注意,如果条件的计算结果为false,则结果为0; 如果是真的,1.所以你可以使用一个技巧(只要乘法和加法也不是verboten) - 对于三个不同的值,如问题所示:
printf("The smallest number is: %d ",
(num1 * (num1 <= num2 && num1 <= num3) +
num2 * (num2 <= num1 && num2 <= num3) +
num3 * (num3 <= num1 && num3 <= num2)));
Run Code Online (Sandbox Code Playgroud)
如果两个值相同且值也较小,则会出现问题.
如果你需要处理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)));
Run Code Online (Sandbox Code Playgroud)
这只是为了找到最小值; 为其他每个案例工作很快就会变得荒谬.事实上,整个练习相当愚蠢,但它也是一些课程的典型.
经过一番思考后,我认为你可以处理2或3个相同的数字(这基本上就是用户3386109在评论中所说的).
#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;
}
Run Code Online (Sandbox Code Playgroud)
输出:
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
Run Code Online (Sandbox Code Playgroud)
计算最大值而不是最小值是微不足道的; 简单地用来>代替<整个.计算中位数变得更难.我怀疑有一种比这更好的方法,但至少这是有效的.注意减去的术语 - 省略,当三个值相同时,中值加倍.
#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;
}
Run Code Online (Sandbox Code Playgroud)
输出:
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)
Run Code Online (Sandbox Code Playgroud)
和以前一样,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;
}
Run Code Online (Sandbox Code Playgroud)
使用随机数生成器(程序名称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)
$
Run Code Online (Sandbox Code Playgroud)
您可以使用seq我使用的地方range.我不确定是否有类似于random我使用的标准PRNG程序(和写过).显示的调用生成3个10到99之间的随机数.
这里的整个过程是荒谬的 - 但这是因为可以使用的技术条件.如果需要对三个或更多数字进行排序,请将它们放入数组中,对数组进行排序,然后打印数组.如果做不到这一点,你应该交换值来查找排序顺序; 它会大大减少所需的比较次数,并且没有乘法.