Java8 Stream API HDU简单题集锦

来源:互联网 发布:7.62*63步枪子弹数据 编辑:程序博客网 时间:2024/05/18 00:47

本文以HDU入门题为基础,使用JAVA8新特性提交,不少题目是为了用而用之....


HDU2026

Problem Description
输入一个英文句子,将每个单词的第一个字母改成大写字母。
 

Input
输入数据包含多个测试实例,每个测试实例是一个长度不超过100的英文句子,占一行。
 

Output
请输出按照要求改写后的英文句子。
 

Sample Input
i like acmi want to get an accepted
 

Sample Output
I Like AcmI Want To Get An Accepted


String title(String s) {String result = "";char c = s.charAt(0);if ('a' <= c && c <= 'z') {result += (char) (c - 'a' + 'A');} else {result += c;}return result + s.substring(1);}void solve() {while (in.hasNext()) {String result =   Arrays.asList(in.nextLine().split(" ")).stream().filter(word -> word != null && word.trim().length() != 0).map(word -> title(word)).collect(Collectors.joining(" "));            out.println(result) ;}out.flush();}

hdu2072

Problem Description
lily的好朋友xiaoou333最近很空,他想了一件没有什么意义的事情,就是统计一篇文章里不同单词的总数。下面你的任务是帮助xiaoou333解决这个问题。
 

Input
有多组数据,每组一行,每组就是一篇小文章。每篇小文章都是由小写字母和空格组成,没有标点符号,遇到#时表示输入结束。
 

Output
每组只输出一个整数,其单独成行,该整数代表一篇文章里不同单词的总数。
 

Sample Input
you are my friend#
 

Sample Output
4

void solve() {while(true){String line = in.nextLine() ;if("#".equals(line)){break ; }long sum = Arrays.stream(line.split(" "))     .filter(s -> !(s == null || s.trim().length() == 0))     .distinct().count() ;    out.println(sum) ;    }out.flush() ; }

hdu2024


Problem Description
输入一个字符串,判断其是否是C的合法标识符。
 

Input
输入数据包含多个测试实例,数据的第一行是一个整数n,表示测试实例的个数,然后是n行输入数据,每行是一个长度不超过50的字符串。
 

Output
对于每组输入数据,输出一行。如果输入数据是C的合法标识符,则输出"yes",否则,输出“no”。
 

Sample Input
312ajffi8x_aff ai_2
 

Sample Output
noyesno 

String judge(String word){for(char c : word.toLowerCase().toCharArray()){if(!(('a' <= c && c <= 'z') || (c == '_') || ('0' <= c && c <= '9'))){return "no" ;}}if('0' <= word.charAt(0) && word.charAt(0) <= '9'){return "no" ;}return "yes" ;}void solve() {List<String> words = new ArrayList<String>() ;int t = in.nextInt() ;while (t-- > 0) {words.add(in.nextLine()) ;}words.stream()     .map(word -> judge(word))     .forEach(word -> out.println(word)) ;out.flush();}

hdu 1556

Problem Description
N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的“小飞鸽"牌电动车从气球a开始到气球b依次给每个气球涂一次颜色。但是N次以后lele已经忘记了第I个气球已经涂过几次颜色了,你能帮他算出每个气球被涂过几次颜色吗?
 

Input
每个测试实例第一行为一个整数N,(N <= 100000).接下来的N行,每行包括2个整数a b(1 <= a <= b <= N)。
当N = 0,输入结束。
 

Output
每个测试实例输出一行,包括N个整数,第I个数代表第I个气球总共被涂色的次数。
 

Sample Input
31 12 23 331 11 21 30
 

Sample Output
1 1 13 2 1

void solve() {while (true) {int n = in.nextInt();if (n == 0) {break;}int[] cnt = new int[n + 2];Arrays.fill(cnt , 0) ;for(int i = 0 ; i < n ; i++){cnt[in.nextInt()]++ ;cnt[in.nextInt()+1]-- ;}for(int i = 1 ; i <= n ; i++){cnt[i] += cnt[i-1] ;}String result = IntStream.of(Arrays.copyOfRange(cnt , 1 , 1+n))                 .mapToObj(String::valueOf)     .collect(Collectors.joining(" ")) ;out.println(result) ;}out.flush() ;}

hdu2000

Problem Description
输入三个字符后,按各字符的ASCII码从小到大的顺序输出这三个字符。
 

Input
输入数据有多组,每组占一行,有三个字符组成,之间无空格。
 

Output
对于每组输入数据,输出一行,字符中间用一个空格分开。
 

Sample Input
qweasdzxc
 

Sample Output
e q wa d sc x z

void solve() {while(in.hasNext()){String result = Arrays.stream(in.next().split(""))      .sorted()      .collect(Collectors.joining(" ")) ;out.println(result) ;    }out.flush() ; }

hdu2008 

Problem Description
统计给定的n个数中,负数、零和正数的个数。
 

Input
输入数据有多组,每组占一行,每行的第一个数是整数n(n<100),表示需要统计的数值的个数,然后是n个实数;如果n=0,则表示输入结束,该行不做处理。
 

Output
对于每组输入数据,输出一行a,b和c,分别表示给定的数据中负数、零和正数的个数。
 

Sample Input
6 0 1 2 3 -1 05 1 2 3 4 0.50
 

Sample Output
1 2 30 0 5

void solve() {while(true){int n = in.nextInt() ;if(n == 0){break ;}Double[] num = new Double[n] ;for(int i = 0 ; i < n ; i++){num[i] = in.nextDouble(); }Map<Double, List<Double>> cnt = Arrays.stream(num).collect(   Collectors.groupingBy(i -> {   if(i < 0) return 0.0 ;   else if(i == 0) return 1.0 ;   else return 2.0 ;   } )) ;out.print(Optional.ofNullable(cnt.get(0.0)).map(List::size).orElse(0)) ;out.print(" " + Optional.ofNullable(cnt.get(1.0)).map(List::size).orElse(0)) ;out.println(" " + Optional.ofNullable(cnt.get(2.0)).map(List::size).orElse(0)) ;}out.flush() ; }


hdu2003

Problem Description
求实数的绝对值。
 

Input
输入数据有多组,每组占一行,每行包含一个实数。
 

Output
对于每组输入数据,输出它的绝对值,要求每组数据输出一行,结果保留两位小数。
 

Sample Input
123-234.00
 

Sample Output
123.00234.00

void solve() {List<Double> num = new ArrayList<Double>() ;while(in.hasNext()){num.add(in.nextDouble()) ;}num.stream().map(Math::abs)   .forEach(i -> out.printf("%.2f%n" , i)) ;out.flush() ; }

hdu2025

Problem Description
对于输入的每个字符串,查找其中的最大字母,在该字母后面插入字符串“(max)”。
 

Input
输入数据包括多个测试实例,每个实例由一行长度不超过100的字符串组成,字符串仅由大小写字母构成。
 

Output
对于每个测试实例输出一行字符串,输出的结果是插入字符串“(max)”后的结果,如果存在多个最大的字母,就在每一个最大字母后面都插入"(max)"。
 

Sample Input
abcdefgfedcbaxxxxx
 

Sample Output
abcdefg(max)fedcbax(max)x(max)x(max)x(max)x(max)

void solve() {while(in.hasNext()){String[] word = in.next().split("") ;String max = Arrays.stream(word)     .filter(s -> !(s == null || s.trim().length() == 0))     .sorted(Comparator.reverseOrder())     .limit(1)     .collect(Collectors.joining()) ;String reslut = Arrays.stream(word)      .map(s ->  max.equals(s) ?  s + "(max)" : s)      .collect(Collectors.joining()) ;    out.println(reslut) ;    }out.flush() ; }


hdu2006

Problem Description
给你n个整数,求他们中所有奇数的乘积。
 

Input
输入数据包含多个测试实例,每个测试实例占一行,每行的第一个数为n,表示本组数据一共有n个,接着是n个整数,你可以假设每组数据必定至少存在一个奇数。
 

Output
输出每组数中的所有奇数的乘积,对于测试实例,输出一行。
 

Sample Input
3 1 2 34 2 3 4 5
 

Sample Output
315
 
void solve() {while(in.hasNext()){int n = in.nextInt() ;int[] num = new int[n] ;for(int i = 0 ; i < n ; i++){num[i] = in.nextInt() ;}int reslut = Arrays.stream(num)       .filter(i -> i % 2 == 1)       .reduce(1 , (a , b) -> a*b) ;    out.println(reslut) ;    }out.flush() ; }

hdu2017
Problem Description
对于给定的一个字符串,统计其中数字字符出现的次数。
 

Input
输入数据有多行,第一行是一个整数n,表示测试实例的个数,后面跟着n行,每行包括一个由字母和数字组成的字符串。
 

Output
对于每个测试实例,输出该串中数值的个数,每个输出占一行。
 

Sample Input
2asdfasdf123123asdfasdfasdf111111111asdfasdfasdf
 

Sample Output
69
void solve() {int t = in.nextInt() ;while(t-- > 0){long cnt = Arrays.stream(in.next().split(""))     .filter(i -> '0' <= i.charAt(0) && i.charAt(0) <= '9')     .count() ;out.println(cnt) ;          }out.flush() ; }

hdu2020
Problem Description
输入n(n<=100)个整数,按照绝对值从大到小排序后输出。题目保证对于每一个测试实例,所有的数的绝对值都不相等。
 

Input
输入数据有多组,每组占一行,每行的第一个数字为n,接着是n个整数,n=0表示输入数据的结束,不做处理。 
 

Output
对于每个测试实例,输出排序后的结果,两个数之间用一个空格隔开。每个测试实例占一行。
 

Sample Input
3 3 -4 24 0 1 2 -30
 

Sample Output
-4 3 2-3 2 1 0
void solve() {while(true){int n = in.nextInt() ;if(n == 0){break ;}Integer[] num = new Integer[n] ;for(int i = 0 ; i < n ; i++){num[i] = in.nextInt() ;}String result = Arrays.stream(num)      .sorted((i,j) -> {return Integer.compare(Math.abs(j) , Math.abs(i)) ;})       .map(String::valueOf)      .collect(Collectors.joining(" ")) ;out.println(result) ;    }out.flush() ; }

hdu2081

Problem Description
大家都知道,手机号是一个11位长的数字串,同时,作为学生,还可以申请加入校园网,如果加入成功,你将另外拥有一个短号。假设所有的短号都是是 6+手机号的后5位,比如号码为13512345678的手机,对应的短号就是645678。
现在,如果给你一个11位长的手机号码,你能找出对应的短号吗?
 

Input
输入数据的第一行是一个N(N <= 200),表示有N个数据,接下来的N行每一行为一个11位的手机号码。
 

Output
输出应包括N行,每行包括一个对应的短号,输出应与输入的顺序一致。
 

Sample Input
21351234567813787654321
 

Sample Output
645678654321
void solve() {int t = in.nextInt() ;List<String> num = new ArrayList<String>(t+1) ;while(t-- > 0){num.add(in.next()) ;    }num.stream()   .map(s -> "6" + s.substring(s.length()-5))           .forEach(out::println) ;out.flush() ; }









原创粉丝点击