字符串数组排序问题

5:字符串数组排序问题

总时间限制: 

1000ms

 

内存限制: 

65536kB

描述

给定一组字符串,按指定的排序方式输出这些字符串。排序可是自然顺序(inc)、自然逆序(dec)、忽略大小写顺序(ncinc)、忽略大小写逆序(ncdec)等。

输入

输入有多行,第一行为一个表明排序方式的字符串见题面,第二行为字符串的数目。
其余各行每行一个字符串,字符串中间可能空格,前后也可能有空格,但前后的空格要忽略。

输出

输出也有多行,按指定的顺序输出输入的字符串。

样例输入

ncdec
3
Hello World!
You're right!
haha! you're wrong!

样例输出

You're right!
Hello World!
haha! you're wrong!

来源

JP06

 

这个题太气人了,编译器啊

import java.io.IOException;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.Scanner;

public class Main {
	
	 static Scanner in = new Scanner(System.in);
	 static int MAXN=10000+100;
    static void dfs(long p,int n)
     {
  
    	return ;
     }
	public static void main(String args[])throws IOException  {

//		int n=in.nextInt();
//		int m=in.nextInt();
//		int[] a=new int[n+1];
//	   
//		for(int i=1;i<=n;i++)
//		{
//			a[i]=in.nextInt();
//		}
		
		 String s;
		 s = in.nextLine();
		 char[] c = s.toCharArray();
		 int len=s.length();
		 LinkedList<String> l=new LinkedList<String>();
		 int n=in.nextInt();
		 in.nextLine();
		 for(int i=1;i<=n;i++)
		 {
			 String a=in.nextLine();
			 
			 //System.out.print(a);
			 l.add(a);
		 }
		 if(s.equals("inc"))
		 {
			 Collections.sort(l);
			  
			 for(String t:l)
			 {
				 System.out.println(t);
			 }
		 }
		 else if(s.equals("dec"))
		 {
			 Collections.sort(l);
			 Collections.reverse(l);
		     for(String t:l)
			 {
				 System.out.println(t);
			 }

		 }
		 else if(s.equals("ncinc"))
		 {
			 Collections.sort(l, new Comparator<String>(){
			public int compare(String str1, String str2)
		   {
				     str1=str1.toUpperCase();
					str2=str2.toUpperCase();
			return str1.compareTo(str2);
		   }
			 }
		);
			 
			 for(String t:l)
			 {
				 System.out.println(t);
			 }
		 }
		 else if(s.equals("ncdec"))
		 {
			 Collections.sort(l, new Comparator<String>(){
					public int compare(String str1, String str2)
				   {
						     str1=str1.toUpperCase();
							str2=str2.toUpperCase();
					return str1.compareTo(str2);
				   }
					 }
				);
			Collections.reverse(l);
			 for(String t:l)
			 {
				 System.out.println(t);
			 }
		 }
		
	}
	
 
	
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值