0 Comparison of the basic C language

Source: Internet
Author: User

We often encounter problems with the size of several numbers. In particular, compare the size of the two number exchange, but someone through if (a>b) [printf ("%d", a)} else {printf ("%d", B)} to avoid the steps of the digital exchange, but that is not suitable for comparing multiple numbers, In the C language we usually use the comparison to exchange values to sort the numbers by size. Swapping is like swapping the different liquids in two cups, which empty cups are needed to fill the first cup of liquid, then pour the second CUP of liquid into the first empty cup, and pour the liquid from the Cup into the Second Cup. So the liquid in the two cups is exchanged with each other. A simple code description.

#include <stdio.h>
int main ()
{
int a,b,t;
printf ("Enter two numbers, they will be arranged from a small size:");
scanf ("%d,%d", &a,&b);
if (a>b) {
T=a;
A=b;
b=t;
}
printf ("%d,%d\n", A, b);
return 0;
}

You can also change the relationship to find the number from large to small, mainly the meaning of the variable T to be introduced. One more column to compare the size of three numbers and output.

    • #include <stdio.h>
    • int main ()
    • {
    • int a,b,c,t;
    • printf ("Please enter 3 integers");
    • printf ("They will output from small to large:");
    • scanf ("%d,%d,%d", &a,&b,&c);
    • if (a>b) {
    • T=a;
    • A=b;
    • b=t;
    • }
    • if (a>c)
    • {
    • T=a;
    • A=c;
    • c=t;
    • }
    • if (b>c)
    • {
    • T=b;
    • B=c;
    • c=t;
    • }
    • printf ("a=%d,b=%d,c=%d", a,b,c);
    • return 0;
    • }

Also available in if_else form

#include <stdio.h>
int main ()
{
int a,b,c;
printf ("Please enter 3 integers");
printf ("will output Maximum number:");
scanf ("%d,%d,%d", &a,&b,&c);
if (a<b) {
if (b<c)
printf ("max=%d\n", c);
else{
printf ("max=%d\n", b);
}
}
else{
if (a<c) {
printf ("max=%d", c);
}
else{
printf ("max=%d", a);
}
}
return 0;
}

Or the three-mesh operator.

#include <stdio.h>
int main ()
{
int A,b,c,temp,max;
printf ("Please enter 3 integers");
printf ("will output Maximum number:");
scanf ("%d,%d,%d", &a,&b,&c);
temp= (a>b) a:b;
max= (temp>c) temp:c;
printf ("The maximum number of 3 integers is%d\n", Max);
return 0;
}

It will be clearer to see the problem through a variety of divergent thinking. The same maximum value of four numbers is similar.

#include <stdio.h>
int main ()
{
int t,a,b,c,d;
printf ("Enter 4 integers, they will be from a small large output:");
scanf ("%d,%d,%d,%d", &a,&b,&c,&d);
printf ("a=%d,b=%d,c=%d,d=%d", a,b,c,d);
if (a>b) {
T=a;
A=b;
b=t;
}
if (a>c) {
T=a;
A=c;
c=t;
}
if (a>d) {
T=a;
A=d;
d=t;
}
if (b>c) {
T=b;
B=c;
b=t;
}
if (b>d) {
T=b;
B=d;
d=t;
}
if (c>d) {
T=c;
C=d;
d=t;
}
printf ("Order as follows: \ n");
printf ("%d%d%d%d\n", a,b,c,d);
return 0;
}

You can also use the three-mesh operator to find the most value, depending on your needs.

#include <stdio.h>
int main ()
{
int a,b,c,d,temp,max,end;
printf ("Please enter 4 integers");
printf ("They will output the maximum value:");
scanf ("%d,%d,%d,%d", &a,&b,&c,&d);
temp= (a>b) a:b;
max= (temp>c) temp:c;
End= (max>d) max:d;
printf ("The largest number is%d\n", end);
return 0;
}

If you still want to ask for more numbers, it is certainly not possible to use the current method, which requires an algorithm to help.

0 Comparison of the basic C language

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.