ACM中奇偶数分离的java答案例子

奇偶数分离

时间限制: 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");
			
		}
		
		
	}
	
	
}

 
    

猜你喜欢

转载自blog.csdn.net/u012999325/article/details/79113816