码迷,mamicode.com
首页 > 其他好文 > 详细

22:按照字典输出字符串

时间:2016-08-14 07:11:34      阅读:145      评论:0      收藏:0      [点我收藏+]

标签:

题目描述

给定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;
    }*/

}

 

22:按照字典输出字符串

标签:

原文地址:http://www.cnblogs.com/newcoder/p/5769098.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!