|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
正确代码: 请注意代码2的注释,害人啊,害我检查了好久....
- #include <stdio.h>
- void swap(int *p1, int *p2, int *p3);
- void main()
- {
- int a, b, c, *pu1, *pu2, *pu3;
- scanf("%d%d%d", &a, &b, &c);
- pu1 = &a;
- pu2 = &b;
- pu3 = &c;
- swap(pu1, pu2, pu3);
- printf("%d > %d > %d\n", *pu1, *pu2, *pu3);
- }
- void swap(int *p1, int *p2, int *p3)
- {
- int temp;
- if(*p1 < *p2)
- {
- temp = *p1;
- *p1 = *p2;
- *p2 = temp;
- }
- if(*p1 < *p3)
- {
- temp = *p1;
- *p1 = *p3;
- *p3 = temp;
- }
- if(*p2 < *p3)
- {
- temp = *p2;
- *p2 = *p3;
- *p3 = temp;
- }
- }
复制代码
错误代码:
- #include <stdio.h>
- void swap(int *p1, int *p2, int *p3);
- void main()
- {
- int a, b, c, *pu1, *pu2, *pu3;
- scanf("%d%d%d", &a, &b, &c);
- pu1 = &a;
- pu2 = &b;
- pu3 = &c;
- swap(pu1, pu2, pu3);
- printf("%d > %d > %d\n", *pu1, *pu2, *pu3);
- }
- void swap(int *p1, int *p2, int *p3)
- {
- int temp;
- if(*p1 < *p2)
- {
- temp = *p1;
- *p1 = *p2;
- *p2 = temp;
- }
- if(*p2 < *p3) // if (*p2 < *p3)与if (*p1 < *p3)交换一下还会出现错误????
- {
- temp = *p2;
- *p2 = *p3;
- *p3 = temp;
- }
- if(*p1 < *p3)
- {
- temp = *p1;
- *p1 = *p3;
- *p3 = temp;
- }
- }
复制代码
|
|