1、
#include <stdio.h> void swap2(int *x, int *y) { int tmp; tmp = *x; *x = *y; *y = tmp; } void sort2(int *n1, int *n2, int *n3) { if(*n1 > *n2) //将前两个中最小的排在最前面 swap2(n1, n2); //swap2函数接收的实参不用使用指针运算符,因为swap2的参数是指针,n1、n2、n3分别是指向a、b、c的指针。 if(*n2 > *n3) //将后两个中的较小的排在前面 swap2(n2, n3); if(*n1 > *n2) //将两个较小的中的最小的排在前面 swap2(n1, n2); } int main(void) { int a, b, c; puts("please input three integers."); printf("a = "); scanf("%d", &a); printf("b = "); scanf("%d", &b); printf("c = "); scanf("%d", &c); sort2(&a, &b, &c); printf("sorted a: %d\n", a); printf("sorted b: %d\n", b); printf("sorted c: %d\n", c); return 0; }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。