List Filtering the list
时间:2020-09-27 16:11:30
收藏:0
阅读:47
Write a program that reads the list of integer numbers separated by spaces from the standard input and then remove all numbers with even indexes (0, 2, 4, and so on).
After that, the program should output the resulting sequence in the reverse order.
Report a typo
Sample Input 1:
1 2 3 4 5 6 7
Sample Output 1:
6 4 2
Sample Input 2:
1 2
Sample Output 2:
2
Sample Input 3:
7 6 -5 -4 -3 2 1
Sample Output 3:
2 -4 6
import java.util.List;
import java.util.Scanner;
class Main {
public static void main(String... args) {
Scanner sc = new Scanner(System.in);
List<String> out = List.of(sc.nextLine().split("\\s+"));
for (int i = out.size() - 1; i > -1; i--) {
if (i % 2 != 0) {
System.out.printf("%s ", out.get(i));
}
}
}
}
原文:https://www.cnblogs.com/longlong6296/p/13739184.html
评论(0)