zoukankan      html  css  js  c++  java
  • 22:按照字典输出字符串

    题目描述

    给定n个字符串,请对n个字符串按照字典序排列。 

    输入描述:输入第一行为一个正整数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

    package prctice01;
    
    import java.util.Collection;
    import java.util.Collections;
    import java.util.Iterator;
    import java.util.Scanner;
    import java.util.Vector;
    /*题目描述
    给定n个字符串,请对n个字符串按照字典序排列。 
    输入描述:输入第一行为一个正整数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*/
    public class DictionaryWord {
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            Scanner in = new Scanner(System.in);
            int count = in.nextInt();
            Vector<String> vector = new Vector<String>();
            for(int i = 0;i<count;i++)
            {
                vector.add(in.next());
            }
            Collections.sort(vector);
            Iterator iterator = vector.iterator();
            while(iterator.hasNext())
                System.out.println(iterator.next());
        }
            /*
            int i = 0;
            String[] array = new String[10];
            while(i<count)
            {
                array[i] = in.next();
                i++;
            }
            String[] result = sort(array,count);
            for(int j =0;j<count;j++)
            {
                System.out.println(result[j]);
            }
        }
        private static String[] sort(String[] array,int n)
        {
            String temp = "";
            for(int i = 0;i<n;i++)
            {
                for(int j = n-1;j>i;j--)
                {
                    if(array[j].compareTo(array[j-1]) < 0)
                    {
                        temp = array[j];
                        array[j] = array[j-1];
                        array[j-1] = temp;
                    }
                }
            }
            return array;
        }*/
    
    }
  • 相关阅读:
    IL指令详细
    读懂IL代码就这么简单(三)完结篇
    读懂IL代码就这么简单(二)
    读懂IL代码就这么简单(一)
    在 .NET Core Logging中使用 Trace和TraceSource
    使用JWT创建安全的ASP.NET Core Web API
    闭环思维
    一行代码,百万人民币打水漂
    网络接口库函数mpr.dll动态库
    使用MSF利用永恒之蓝漏洞远程控制主机——直接使用MSF即可RCE,我++,在docker里没有完成,GG!
  • 原文地址:https://www.cnblogs.com/newcoder/p/5769098.html
Copyright © 2011-2022 走看看