题目描述
输入a、b、c三个整数,按先大后小的顺序输出a、b和c。注意请使用指针变量的方式进行比较和输出。
输入
三个用空格隔开的整数a、b和c。
输出
按先大后小的顺序输出a、b和c,用空格隔开。
请注意行尾输出换行。
样例输入
9 0 10
样例输出
10 9 0
提出问题:C++写的这俩函数为什么在codeup上面运行显示答案错误?我还看了其他人的资料,没发现毛病
#include <iostream>
#include <math.h>
#include <string.h>
#include <cstdio>
using namespace std;
void compare(int* a,int* b,int* c){
//c<b<a
int temp=0;
if(*a<*b){
temp=*a;
*a=*b;
*b=temp;
}
if(*b<*c){
temp=*b;
*b=*c;
*c=temp;
}
if(*a<*c){
temp=*a;
*a=*c;
*c=temp;
}
}
int main() {
int a=0,b=0,c=0;
scanf("%d %d %d",&a,&b,&c);
compare(&a,&b,&c);
printf("%d %d %d\n",a,b,c);
return 0;
}
#include <iostream>
#include <math.h>
#include <string.h>
using namespace std;
void compare(int* a,int* b){
//大的放在第一个参数
int temp=0;
if(*a<*b){
temp=*a;
*a=*b;
*b=temp;
}
}
int main() {
int a=0,b=0,c=0;
scanf("%d %d %d",&a,&b,&c);
compare(&a,&b);
compare(&b,&c);
compare(&a,&c);
printf("%d %d %d\n",a,b,c);
return 0;
}