其他分享
首页 > 其他分享> > Blog02——关于C语言函数返回多值问题

Blog02——关于C语言函数返回多值问题

作者:互联网

今天在刷PAT的C语言练习题的时候遇到一个题,本打算用C函数分别返回3数组合成的最大值和最小值,但是最终返回的最大值出现问题!

初始代码如下:

 1 # include <stdio.h>
 2 # include <stdlib.h>
 3 
 4 int compare(int a,int b,int c) {
 5     int max,min,middle,value1,value2;
 6     max = (a>=b)?(a>=c?a:c):(b>=c?b:c);
 7     min = (a<=b)?(a<=c?a:c):(b<=c?b:c);
 8     if (a >= b) {
 9         if (a <= c) {
10             middle = a;
11         }else {
12             if (b >= c) {
13                 middle = b;
14             }else {
15                 middle = c;
16             }
17         }
18     }else {
19         if (b <= c) {
20             middle = b;
21         }else {
22             if (a <= c) {
23                 middle = c;
24             }else {
25                 middle = a;
26             }
27         }
28     }
29     value1 = max * 100 + middle * 10 + min;
30     value2 = min * 100 + middle * 10 + max;
31     return value1,value2;
32 }
33 int main() {
34     int n,i = 1,ge,shi,bai,value1,value2;
35     scanf("%d",&n);
36     int max,min,middle;
37     while (n != 495) {
38         bai = n / 100;
39         shi = (n % 100) / 10;
40         ge = (n % 100) % 10;
41         // 得到组合数
42         value1,value2 = compare(bai,shi,ge);
43         printf("%d %d",value1,value2);
44         break;
45 //        printf("%d: %d - %d = %d\n",i,value1,value2,value1-value2);
46 //        n = value1 - value2;
47         i += 1;
48     }
49     return 0;
50 } 

调试过程发现max、min以及middle得出没有任何问题!而且将compare函数里面的return语言换成printf语句得出的value1和value2也是正确的!

 

 后面我就意识到C语言函数可能不像python那样可以直接返回多值!于是打算用引用来解决一下这个问题!

 1 # include <stdio.h>
 2 # include <stdlib.h>
 3 
 4 void compare(int &a,int &b,int &c) {
 5     int max,min,middle,value1,value2;
 6     max = (a>=b)?(a>=c?a:c):(b>=c?b:c);
 7     min = (a<=b)?(a<=c?a:c):(b<=c?b:c);
 8     if (a >= b) {
 9         if (a <= c) {
10             middle = a;
11         }else {
12             if (b >= c) {
13                 middle = b;
14             }else {
15                 middle = c;
16             }
17         }
18     }else {
19         if (b <= c) {
20             middle = b;
21         }else {
22             if (a <= c) {
23                 middle = c;
24             }else {
25                 middle = a;
26             }
27         }
28     }
29     a = max;
30     b = middle;
31     c = min;
32 }
33 int main() {
34     int n,i = 1,ge,shi,bai,value1,value2;
35     scanf("%d",&n);
36     int max,min,middle;
37     while (n != 495) {
38         bai = n / 100;
39         shi = (n % 100) / 10;
40         ge = (n % 100) % 10;
41         // 得到组合数
42         compare(bai,shi,ge);
43         value1 = bai * 100 + shi * 10 + ge;
44         value2 = ge * 100 + shi * 10 + bai;
45         printf("%d: %d - %d = %d\n",i,value1,value2,value1-value2);
46         n = value1 - value2;
47         i += 1;
48     }
49     return 0;
50 } 

现在测试得到的结果就是正确的了,当然也可以用指针来解决!毕竟引用是弱化的指针,引用都可以,指针自然行!

 

标签:多值,include,min,int,max,C语言,Blog02,middle,else
来源: https://www.cnblogs.com/lures/p/14509359.html