//第一题 好理解 import java.util.Arrays; import java.util.Scanner; public class didi { public static void main(String[] args) { Scanner in=new Scanner(System.in); String str=in.next(); if(str.length()==0||str==null){ System.out.println(); return; } char ch[]=str.toCharArray(); System.out.println(fun(ch)); } public static char [] fun(char ch[]) { int len=ch.length; char ch1[]=new char[len]; for(int i=0;i<len;i++){ ch1[i]=ch[i]; } Arrays.sort(ch1); System.out.println(ch1); if(ch.equals(ch1)){ return ch; } int n1=0,n2=0; char cc=' '; for(int i=0;i<len;i++){ if(ch[i]==ch1[i]){ continue; }else{ n1=i; cc=ch1[i]; break; } } for(int i=len-1;i>n1;i--){ if(cc==ch[i]){ n2=i; break; } } change(ch, n1, n2); return ch; } public static void change(char ch[],int i,int j) { char temp=ch[i]; ch[i]=ch[j]; ch[j]=temp; } }
全部评论
(2) 回帖