算法编程题笔记

博客主要探讨了如何将输入的正整数数组连接成一个数,并找出所有可能排列中最小的一个。通过具体例子32,231,博主展示了如何得到最小排列23132。" 84938006,8259497,C++控制结构详解:if、switch与循环,"['C++编程', '控制流', '程序设计']
摘要由CSDN通过智能技术生成

算法编程题:

输入一个正整数数组,将它们连接起来排成一个数,输出能排出的所有数字中最小的一个

输入:

32,231

输出:

23132

import java.util.Arrays;
import java.util.Scanner;

public class Main7
{
	public static void main(String[] args)
	{
		Scanner cin = new Scanner(System.in);
		// 读取输入,直到没有整型数据可读
		// while(cin.hasNextInt())
		// while (cin.hasNextLine())
		while (cin.hasNextLine())
		{
			String s = cin.nextLine();
			String[] str = sort(s);
			if (str.length > 0)
			{
				System.out.println(Arrays.toString(str));
			}
		}
	}

	public static String[] sort(String s)
	{
		String[] arr = s.split(",");
		while (true)
		{
			boolean ok = true;
			for (int j = 0; j < arr.length - 1; j++)
			{

				if (!checkSwap(arr[j], arr[j + 1]))
				{
					String tmp = "";
					tmp = arr[j];
					arr[j] = arr[j + 1];
					arr[j + 1] = tmp;
					ok = false;
					Syste
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值