本文主要是介绍6-1 Duplicated Words (20分),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
java旧题复习
6-1 Duplicated Words (20分)
This program reads a lot of words, in which may be duplicated words. The program picks out all the duplicated ones and sorts the remainders in a descendent order.
函数接口定义:
public static ArrayList pick(ArrayList a);
a is the ArrayList to be parsed and returns the result as an ArrayList.
裁判测试程序样例:
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Scanner;public class Main {/* 请在这里填写答案 */public static void main(String[] args) {ArrayList<String> lst = new ArrayList<>();Scanner in = new Scanner(System.in);while ( in.hasNext() ) {lst.add(in.next());}lst = pick(lst);for ( String x:lst) {System.out.print(x+" ");}System.out.println();in.close();}
}
输入样例:
this is a test at this Zhejiang University
输出样例:
this test is at a Zhejiang University
ans:
public static ArrayList<String> pick(ArrayList<String> a){ArrayList<String> list=new ArrayList<>();for(int i=0;i<a.size();i++) {if(list.contains(a.get(i))==false) {list.add(a.get(i));}}Collections.sort(list);Collections.reverse(list);return list;}
这篇关于6-1 Duplicated Words (20分)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!