奇偶數分離
時間限制:3000 ms | 內存限制:65535 KB
難度:1
- 描述
- 有一個整型偶數n(2<= n <=10000),你要做的是:先把1到n中的所有奇數從小到大輸出,再把所有的偶數從小到大輸出。
- 輸入
- 第一行有一個整數i(2<=i<30)表示有 i 組測試數據;
每組有一個整型偶數n。 - 輸出
- 第一行輸出所有的奇數
第二行輸出所有的偶數
- 樣例輸入
-
2
10
14
- 樣例輸出
-
1 3 5 7 9
2 4 6 8 10
1 3 5 7 9 11 13
2 4 6 8 10 12 14
package problem;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Scanner;
public class Main {
public static void main (String [] args) {
Scanner input = new Scanner(System.in);
int a;
a=input.nextInt();
Collection<Integer> col = new ArrayList<Integer>();
for(int i = 0;i<a;i++) {
int in;
in = input.nextInt();
col.add(in);
}
for(Integer out:col) {
Collection<Integer> col1=new ArrayList<Integer>();
Collection<Integer> col2=new ArrayList<Integer>();
for(Integer i = 1;i<=out;i++) {
if ((i%2)==0) {
col1.add(i);
}else {
col2.add(i);
}
}
for(Integer out1:col2) {
System.out.print(out1+" ");
}
System.out.println();
for(Integer out1:col1) {
System.out.print(out1+" ");
}
System.out.println("\r");
}
}
}
-