Introduction to the competition-variable exchange

Problem:
Input two integers a and b, exchange their values, and then output.

Sample input:

824 16

Sample output:

16 824


The first type: with the help of variables

#include "stdio.h"
int main()
{
    
    
    int a, b, temp;
    scanf("%d%d", &a, &b);
    temp = b;
    b = a;
    a = temp;
    printf("%d %d\n", a, b);
}

The second type: without the help of variables

int main()
{
    
    
    int a, b;
    scanf("%d%d", &a, &b);
    a = a + b;
    b = a - b;
    a = a - b;
    printf("%d %d\n", a, b);
}

For this, you can use two simple numbers as an example to understand~.


The third type: direct output

int main()
{
    
    
    int a, b;
    scanf("%d%d", &a, &b);
    printf("%d %d\n", b, a);
}

Although this question is very simple, you can understand a truth. The contest is to examine the programmer’s problem-solving ability and does not care about the method used. For this question, this is the most suitable. The idea of ​​the contest is to findOptimal solution, It is better than who can solve the problem, rather than looking more advanced than who wrote the program.


Guess you like

Origin blog.csdn.net/mjh1667002013/article/details/113283632