下一个最大的数字

我试图从用户输入中找到下一个最大的数字。如果用户给出 23,它显示输出为 32。如果有更大的数字,那么它必须打印相同的给定数字。但是如果用户给出 03显示 3 但它必须显示 30。因为它需要 03 作为八进制数。我如何更改代码以将正确的输出显示为 30?


public class Main

{

 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 = ("" + 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]);

  }

 }

}


米琪卡哇伊
浏览 103回答 4
4回答

LEATH

package com.demo;import java.util.Arrays;import java.util.Scanner;public class Demo {&nbsp; &nbsp; static void swap(char ar[], int i, int j)&nbsp;&nbsp; &nbsp; {&nbsp;&nbsp;&nbsp; &nbsp; &nbsp;char temp = ar[i];&nbsp; &nbsp; &nbsp;ar[i] = ar[j];&nbsp; &nbsp; &nbsp;ar[j] = temp;&nbsp; &nbsp; }&nbsp; &nbsp; public static void main(String[] args)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; Scanner in = new Scanner(System.in);&nbsp; &nbsp; &nbsp; //int num = in .nextInt();&nbsp; &nbsp; &nbsp; char[] chars=null;&nbsp; &nbsp; &nbsp; String numStr=in.next();&nbsp; &nbsp; &nbsp; int num= Integer.valueOf(numStr);&nbsp; &nbsp; &nbsp; if(numStr.startsWith("0")) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; chars= ("0" + num).toCharArray();&nbsp; &nbsp; &nbsp; }else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; chars= ("" + num).toCharArray();&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; int i;&nbsp; &nbsp; &nbsp; int n = chars.length;&nbsp; &nbsp; &nbsp; for (i = n - 1; i > 0; i--)&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp;if (chars[i] > chars[i - 1])&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; if (i == 0)&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;System.out.println(num);&nbsp; &nbsp; &nbsp; else {&nbsp; &nbsp; &nbsp; &nbsp;int x = chars[i - 1], min = i;&nbsp; &nbsp; &nbsp; for (int j = i + 1; j<n; j++)&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp;if (chars[j] > x && chars[j]<chars[min])&nbsp; &nbsp; &nbsp; &nbsp; min = j;&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; swap(chars, i - 1, min);&nbsp; &nbsp; &nbsp; Arrays.sort(chars, i, n);&nbsp; &nbsp; &nbsp; for (i = 0; i<n; i++)&nbsp; &nbsp; &nbsp; &nbsp; System.out.print(chars[i]);&nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; }}

侃侃尔雅

在您的main()中,仅将输入作为字符串,而不是将其作为整数。&nbsp;String num=in.next();

当年话下

您正在以整数形式从标准输入中读取数字。尝试将它们读作字符串:&nbsp; &nbsp;Scanner in = new Scanner(System.in);&nbsp; &nbsp;String num = in.nextLine();&nbsp; &nbsp;char[] chars = num.toCharArray();

MMTTMM

public static int findNextGreatestNumber(int[] arr, int k) {&nbsp; &nbsp; int delta = Integer.MAX_VALUE;&nbsp; &nbsp; int res = 0;&nbsp; &nbsp; for (int a : arr) {&nbsp; &nbsp; &nbsp; &nbsp; if (a - k > 0 && a - k < delta) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; delta = a - k;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; res = a;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return delta == Integer.MAX_VALUE ? k : res;}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java