1232 - C语言10.2

通过次数

0

提交次数

0

时间限制 : 1 秒 内存限制 : 32 MB

输入a、b、c三个整数,按先大后小的顺序输出a、b和c。注意请使用指针变量的方式进行比较和输出。

题目输入

三个用空格隔开的整数a、b和c。

题目输出

按先大后小的顺序输出a、b和c,用空格隔开。

请注意行尾输出换行。

输入/输出样例

输入格式

9 0 10

输出格式

10 9 0

C语言解答

#include<stdio.h>
#include<stdlib.h>
int compara(int *pa,int *pb,int *pc)
{
	int temp1,temp2,temp3;
	if(*pa>*pb&&*pb>*pc)
	{temp1=*pa;temp2=*pb;temp3=*pc;}
    if(*pa>*pc&&*pc>*pb)
	{temp1=*pa;temp2=*pc;temp3=*pb;}
	if(*pb>*pa&&*pa>*pc)
	{temp1=*pb;temp2=*pa;temp3=*pc;}
	if(*pb>*pc&&*pc>*pa)
	{temp1=*pb;temp2=*pc;temp3=*pa;}
	if(*pc>*pa&&*pa>*pb)
	{temp1=*pc;temp2=*pa;temp3=*pb;}
	if(*pc>*pb&&*pb>*pa)
	{temp1=*pc;temp2=*pb;temp3=*pa;}
	printf("%d %d %d",temp1,temp2,temp3);
	
	return 0;
}
int main()
{
	int a,b,c;
	scanf("%d%d%d",&a,&b,&c);
	compara(&a,&b,&c);
}

C++解答

#include <stdio.h>
int main() {
	void swap(int * pt1, int * pt2);
	int *p1, *p2, *p3, a, b, c;
	scanf("%d %d %d", &a, &b, &c);
	p1 = &a; p2 = &b; p3 = &c;
	if (*p1 < *p2) swap(p1, p2);
	if (*p1 < *p3) swap(p1, p3);
	if (*p2 < *p3) swap(p2, p3);
	printf("%d %d %d\n", *p1, *p2, *p3);
	return 0;
}
void swap(int * pt1, int * pt2) {
	int temp;
	temp = *pt1;
	*pt1 = *pt2;
	*pt2 = temp;
}

Java解答

import java.util.*;

public class Main{
	public static void main (String[] args) {
		Scanner in=new Scanner(System.in);
		int []a=new int [3];
		for(int i=0;i<3;i++){
			a[i]=in.nextInt();
		}
		java.util.Arrays.sort(a);
		System.out.print (a[2]+" ");
		System.out.print (a[1]+" ");
		System.out.println (a[0]);
	}
}