两种排序方法
【解题思路】:
思路很简单,将接收的字符串都放到
String
数组中,利用
string
的
compareTo
方法来按
ascii
比较字符串字典序
排序,利用
string
的
length
方法来比较字符串的长度排序
import java.util.*;
import java.io.*;
public class Main{
public static void main()throws IOException{
BufferedReader re=new BufferedReader(new InputStreamReader(System.in));
int n=Integer.parseInt(re.readLine());
String[] str=new String[n];
for(int i=0;i<n;i++){
str[i]=re.readLine();
}
if(isSortZidian(str)&&isSortLength(str)){
System.out.println("both");
}else if(isSortZidian(str)){
System.out.println("lexicographically");
}else if(isSortLength(str)){
System.out.println("lengths");
}else{
System.out.println("none");
}
}
public static boolean isSortZidian(String[] str){
for(int i=0;i<str.length-1;i++){
if(str[i].compareTo(str[i+1])>0){
return false;
}
posted on
浙公网安备 33010602011771号