1-14字串的连接最长路径查找
题目描述
输入描述:
输入第一行为一个正整数n(1≤n≤1000),下面n行为n个字符串(字符串长度≤100),字符串中只含有大小写字母。
输出描述:
数据输出n行,输出结果为按照字典序排列的字符串。
输入例子:
9 cap to cat card two too up boat boot
输出例子:
boat boot cap card cat to too two up
代码:
import java.util.*;
public class Main {
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
sc.nextLine();
String [] sa=new String[n];
for (int i=0;i<n;i++){
sa[i]=sc.nextLine();
}
Arrays.sort(sa);
for (int i=0;i<n;i++){
System.out.println(sa[i]);
}
}
}
收获:(1)建立长度为n字符串数组并输入
String [] sa=new String[n];
for (int i=0;i<n;i++){
sa[i]=sc.nextLine();
(2)nextInt()方法之后在缓冲区中留下了“\r”,然后nextLine()方法再去缓冲区找数据的时候首先看到了“\r”,然后就把这个“\r”扫描接收进来,并在缓冲区内清除掉。
所以这就为什么sa[0]什么都没有。所以在int n=sc.nextInt();和sa[i]=sc.nextLine();之间加上sc.nextLine();可避免。
或者 这样String s=sc.nextLine();
int n=Integer.parseInt(s);
Integer.parseInt(String)是将String转化为int
(3)数组排序:
1. 数字排序 int[] intArray = new int[] { 4, 1, 3, -23 };
Arrays.sort(intArray);
输出: [-23, 1, 3, 4]
2. 字符串排序,先大写后小写 String[] strArray = new String[] { "z", "a", "C" };
Arrays.sort(strArray);
输出: [C, a, z]
3. 严格按字母表顺序排序,也就是忽略大小写排序 Case-insensitive sort
Arrays.sort(strArray, String.CASE_INSENSITIVE_ORDER);
输出: [a, C, z]
4. 反向排序, Reverse-order sort
Arrays.sort(strArray, Collections.reverseOrder());
输出:[z, a, C]
5. 忽略大小写反向排序 Case-insensitive reverse-order sort
Arrays.sort(strArray, String.CASE_INSENSITIVE_ORDER);
Collections.reverse(Arrays.asList(strArray));
输出: [z, C, a]