如何在printf语句中设置条件? [英] How do I make conditions in printf statements?

查看:128
本文介绍了如何在printf语句中设置条件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做作业,必须创建一个C程序,该程序从最小到最大对5个数字进行排序。我知道如何使用函数和 if 语句(使用> = < = )。

I have homework where I have to create a C program which sorts 5 numbers from smallest to largest. I know how to easily program this using functions and if statements (using >= and <=).

但是,我只能使用 printf scanf ,所以我必须计算所有> = < = printf 中。而且我不允许使用三元运算符。

However, the catch is I am only allowed to use printf and scanf, so I have to calculate all the >= and <= inside the printf. And I'm not allowed to use the ternary operator.

我已经尝试了很长时间了。因此,我尝试仅对3个数字进行排序,但我什至无法摆脱对最小数字的排序。它只是一直打印 1

I've been trying to a long time. So I tried to sort just 3 numbers, I still can't even get past sorting the smallest number. It just keeps printing 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 代表true)。如果为假,则打印 0 。因此,我可以从技术上添加正确的语句,然后 -1 。因此,如果 num2 的陈述为真,则可以执行 + num2-1

One possible solution I have come up with is adding the ((num1 + num2 + num3) -1) because if one of the statements is true (for example if num2 is the smallest, it would print 1 because 1 means true). If it is false it prints 0. So I could technically add the statement which is true then -1. So if num2's statement is true, I could do + num2 - 1.

推荐答案

第1步:三个不同值中的最小值



请注意,如果条件的计算结果为false,结果为 0 ;如果为true,则 1 。因此,您可以使用一个窍门(只要乘法和加法也没有问题)—针对问题中所示的三个不同值:

Pass 1: Minimum of three distinct values

Note that if a condition evaluates to false, the result is 0; if true, 1. So you can use a trick (as long as multiplication and addition aren't verboten too) — for three distinct values as shown in the question:

printf("The smallest number is: %d ",
       (num1 * (num1 <= num2 && num1 <= num3) +
        num2 * (num2 <= num1 && num2 <= num3) +
        num3 * (num3 <= num1 && num3 <= num2)));

如果两个值相同且值都较小,则会出现麻烦。

There will be trouble if two values are the same and are also the smaller value.

如果需要处理5个值,则为(如注释中所述) )而不是困难。

If you need to process 5 values, it is (as noted in a comment) more tedious than difficult.

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)));

这只是为了寻找最小值;在其他每个案例中迅速解决这一问题变得荒谬。确实,整个练习相当愚蠢,但这在某些课程中也是很典型的。

This is just for finding the minimum; working it for each of the other cases quickly becomes preposterous. Indeed, the whole exercise is fairly silly, but it is also fairly typical of some courses.

经过一番思考之后,我认为您可以处理2或3个与此相同的数字(这基本上是 user3386109 中说)。

After a bit of cogitation, I think you can deal with 2 or 3 numbers the same with this (which is basically what user3386109 said in a 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



第4步:三个值的排序顺序不一定不同



计算最大值而不是最小值很简单;只需使用> 代替<
计算中位数会更加困难。我怀疑有比这更好的方法,但是至少这样做是可行的。请注意减去的项-忽略该项,并且当三个值相同时,中位数会加倍。

Pass 4: Sorted order for three values not necessarily distinct

Calculating the maximum instead of the minimum is trivial; simply use > in place of < throughout. Calculating the median turns out to be harder. I suspect there is a better way of doing it than this, but at least this works. Note the subtracted term — omit that, and the median value is doubled when the three values are the same.

#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)



第5遍:三个值的排序顺序,没有循环或函数



和以前一样,第4遍中的代码对三个数字在其相对位置的所有组合进行了彻底的测试。如果您需要先读取三个数字然后对其进行排序(并且您不允许使用 main()以外的循环或函数scanf() printf(),就这样吧-您可以移植 printf()读取三个值后立即在 main()中声明:

Pass 5: Sorted order for three values, no loops or function

As before, the code in Pass 4 does a thorough test of all combinations of three numbers in their relative positions. If you're required to read three numbers and then sort them (and you're not allowed to use loops or functions other than main(), scanf(), printf(), so be it — you can transplant the printf() statement into your main() immediately after you've read three values:

#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 )的收益率:

Testing with a random number generator (program name sort3-53) yields:

$ 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程序。显示的调用会生成3个介于10和99之间的随机数。

You can probably use seq where I use range. I'm not sure there's a standard PRNG program analogous to the random I use (and wrote). The invocation shown generates 3 random numbers between 10 and 99 inclusive.

这里的整个过程是荒谬的-但这是因为可以使用的技术上存在条件。如果需要对三个或三个以上的数字进行排序,请将它们放入数组中,对数组进行排序,然后打印该数组。失败的话,您应该交换值以找到排序的顺序。它将大大减少所需的比较次数,并且不会出现乘法运算。

The whole process here is preposterous — but that's because of the conditions placed on the techniques that can be used. If you need to sort three or more numbers, put them in an array, sort the array, and print the array. Failing that, you should swap the values to find the sorted order; it would dramatically reduce the number of comparisons needed, and there'd be no multiplications.

这篇关于如何在printf语句中设置条件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆