4 回答
TA贡献1936条经验 获得超6个赞
package com.demo;
import java.util.Arrays;
import java.util.Scanner;
public class Demo {
static void swap(char ar[], int i, int j)
{
char temp = ar[i];
ar[i] = ar[j];
ar[j] = temp;
}
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
//int num = in .nextInt();
char[] chars=null;
String numStr=in.next();
int num= Integer.valueOf(numStr);
if(numStr.startsWith("0")) {
chars= ("0" + num).toCharArray();
}else {
chars= ("" + num).toCharArray();
}
int i;
int n = chars.length;
for (i = n - 1; i > 0; i--)
{
if (chars[i] > chars[i - 1])
break;
}
if (i == 0)
System.out.println(num);
else {
int x = chars[i - 1], min = i;
for (int j = i + 1; j<n; j++)
{
if (chars[j] > x && chars[j]<chars[min])
min = j;
}
swap(chars, i - 1, min);
Arrays.sort(chars, i, n);
for (i = 0; i<n; i++)
System.out.print(chars[i]);
}
}
}
TA贡献1890条经验 获得超9个赞
您正在以整数形式从标准输入中读取数字。尝试将它们读作字符串:
Scanner in = new Scanner(System.in);
String num = in.nextLine();
char[] chars = num.toCharArray();
TA贡献1869条经验 获得超4个赞
public static int findNextGreatestNumber(int[] arr, int k) {
int delta = Integer.MAX_VALUE;
int res = 0;
for (int a : arr) {
if (a - k > 0 && a - k < delta) {
delta = a - k;
res = a;
}
}
return delta == Integer.MAX_VALUE ? k : res;
}
添加回答
举报