L1-010 比较大小 - java

题目解析

判断三个数的大小 并从小到大 按指定格式输出

解题思路

比较大小

代码

运用类库

import java.io.*;
import java.util.*;

public class Main
{

	public static void main(String[] args)
	{
		int n = 3;
		int shu[] = new int[n + 10];
		for (int i = 1; i <= n; i++)
			shu[i] = sc.nextInt();
		Arrays.sort(shu, 1, n + 1);

		for (int i = 1; i <= n; i++)
		{
			if (i != 1)
				out.print("->");
			out.print(shu[i]);
		}

		out.flush();
		out.close();
	}

	static Scanner sc = new Scanner(System.in);
	static PrintWriter out = new PrintWriter(System.out);
}


每次枚举两个数 进行

import java.io.*;
import java.util.*;

public class Main
{

	public static void main(String[] args)
	{
		int a = sc.nextInt();
		int b = sc.nextInt();
		int c = sc.nextInt();

		if (a > b)
		{
			int temp = a;
			a = b;
			b = temp;
		}

		if (a > c)
		{
			int temp = a;
			a = c;
			c = temp;
		}

		if (b > c)
		{
			int temp = b;
			b = c;
			c = temp;
		}

		out.println(a + "->" + b + "->" + c);

		out.flush();
		out.close();
	}

	static Scanner sc = new Scanner(System.in);
	static PrintWriter out = new PrintWriter(System.out);
}


团体程序设计天梯赛-练习集-java

赞赏