牛客网 两种排序

由于题目比较简单,主要是对排序的应用,算法思路基本是算是清晰。

 1 import java.io.BufferedReader;
 2 import java.io.IOException;
 3 import java.io.InputStreamReader;
 4 /*********         牛客网--两种排序           **********/
 5 import java.util.Arrays;
 6 
 7 public class Demo7{
 8     
 9     
10     public static void main(String[] args) throws NumberFormatException, IOException{
11 
12         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
13         int n = Integer.parseInt(br.readLine());
14         boolean dectionary_flag = true;//字典排序标记
15         boolean length_flag = true;       //长度排序标记
16         String[] ss = new String[n];   //输入的字符串数组
17         int[] s = new int[n];           //对应字符串数组的每个字符床的长度数组
18         for(int i = 0; i < n; i++){       //输入
19             ss[i] = br.readLine();
20             s[i] = ss[i].length();
21         }
22         
23         String[] ss1 = ss.clone();       //复制一个数组ss1,保留ss数组的原始状态
24         int[] s1 = s.clone();           //复制一个数组s1 , 保留s数组的原始状态
25         Arrays.sort(ss);               //对ss数组进行字典排序
26         Arrays.sort(s);                   //对s数组升序排序
27         for(int i = 0; i < n; i++){        /* 判断原始数组是否为字典排序*/
28             if(!ss1[i].equals(ss[i])) {
29                 dectionary_flag = false;
30                 break;
31             }
32         }
33         for(int i = 0; i < n; i++){/*判断原始数组是否为长度排序*/
34             if(s1[i] != s[i]) {
35                 length_flag = false;
36                 break;
37             }
38         }
39         /*输出*/
40         if(dectionary_flag && length_flag) {
41             System.out.println("both");
42         }else if(dectionary_flag){
43             System.out.println("lexicographically");
44         }else if(length_flag) {
45             System.out.println("lengths");
46         }else {
47             System.out.println("none");
48         }
49         
50  }
51 }

猜你喜欢

转载自www.cnblogs.com/Thomas-Wang/p/10170539.html