团体程序设计天梯赛 L1-010. 比较大小

测试数据:

1 2 3

1 3 2

2 1 3

2 3 1

3 1 2

3 2 1

Code:

先确定最小,然后确定第二小

 1 #include <stdio.h> 
 2 #include <stdlib.h>
 3 
 4 int main()
 5 {
 6     long t,a,b,c;
 7     scanf("%ld%ld%ld",&a,&b,&c);
 8     if (a>b)
 9     {
10         t=a;
11         a=b;
12         b=t;
13     }
14     if (a>c)
15     {
16         t=a;
17         a=c;
18         c=t;
19     }
20     if (b>c)
21     {
22         t=b;
23         b=c;
24         c=t;
25     }
26     printf("%ld->%ld->%ld",a,b,c);
27     return 0;
28 }

一个比较坑的错误程序:

 1 #include <stdio.h> 
 2 #include <stdlib.h>
 3 
 4 int main()
 5 {
 6     long t,a,b,c;
 7     scanf("%ld%ld%ld",&a,&b,&c);
 8     if (a>b)
 9     {
10         t=a;
11         a=b;
12         b=t;
13     }
14     if (b>c)
15     {
16         t=b;
17         b=c;
18         c=t;
19     }
20     if (a>c)
21     {
22         t=a;
23         a=c;
24         c=t;
25     }
26     printf("%ld->%ld->%ld",a,b,c);
27     return 0;
28 }

错误结果:

2 3 1
2->1->3

3 2 1
2->1->3

原文地址:https://www.cnblogs.com/cmyg/p/8574572.html