[TOC]
南阳OJ-No.11 时间限制:3000 ms | 内存限制:65535 KB
描述 有一个整型偶数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 2 4 6 8 10 12
java 时间124,内存1849
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 /*2017.2.1 * JDK1.7 * 奇偶数分离 * */ import java.util.Scanner; public class Main { public static void main(String[] args) throws Exception{ Scanner cin = new Scanner(System.in); int a = cin.nextInt(); //a组数据 while (a>=1) { //a组测试,则为a组循环 int x = cin.nextInt(); for (int n=1; n<=x; n++) { //遍历输出奇数 if (n%2 !=0) { System.out.print(n + " "); } } System.out.println(); for (int n=1; n<=x; n++) { //遍历输出偶数 if (n%2 == 0) { System.out.print(n + " "); } } a--; } } }
时间114,内存1849
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 /*2017.2.1 * JDK1.7 * 奇偶数分离 * */ import java.util.Scanner; public class Main { public static void main(String[] args) throws Exception{ Scanner cin = new Scanner(System.in); int a = cin.nextInt(); //a组数据 while (a>=1) { //a组测试,则为a组循环 int x = cin.nextInt(); int n = 1; while (n<=x) { if (n%2 !=0) { System.out.print(n + " "); } n++; } System.out.println(); n = 1; while (n<=x) { if (n%2 == 0) { System.out.print(n + " "); } n++; } a--; } } }
while效率要比for循环高!!!!!
时间58,内存1788
该解法来自网络,原文请参照来自安德里亚的成长:奇偶数分离
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 import java.util.Scanner; public class Main { public static void main(String[] args) { int [] src = getInt(); //读取输入 StringBuffer odds, evens; //odds用于存储奇数,evens用于存储偶数 for(int j = 0; j < src.length; j++) { odds = new StringBuffer(); evens = new StringBuffer(); for(int i = 1; i <= src[j]; i++) { //一次遍历即可获得奇偶数分离 if(isOdd(i)) { odds.append(i+" "); } else { evens.append(i+" "); } } //输出结果 System.out.println(odds.toString()); System.out.println(evens.toString()); System.out.println(); } } public static int[] getInt() { Scanner sc=new Scanner(System.in); int x=sc.nextInt(); int[] s=new int[x]; for(int i=0;i<s.length;i++){ s[i]=sc.nextInt(); } return s; } //判断是奇数 public static boolean isOdd(int i) { if(i % 2 != 0) return true; return false; } }
时间34,内存436
这个是在南阳OJ上通过结果里找的算是最优的算法了,投了一个币偷瞄了下源码,用户名Bryan,一个字,美!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 import java.util.Scanner; public class Main { public static Scanner cin = new Scanner(System.in); public static StringBuilder sb = new StringBuilder(); public static void main(String[] args) { int line = 0; int number = cin.nextInt(); for(int i=0; i<number; i++){ line = cin.nextInt(); for(int j=1; j<=line; j+=2){ sb.append(j).append(' '); } sb.append('\n'); for(int j = 2; j<=line; j+=2){ sb.append(j).append(' '); } System.out.println(sb); sb.delete(0, sb.length()); } } }
c++ 时间4,内存240
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 #include <iostream> using namespace std; int main() { int a; //记录循环次数 cin >> a; while(a>=1) { int x; cin >> x; for (int n=1; n<=x; n++) { //遍历输出奇数 if (n%2 !=0) { cout << n << " "; } } cout << endl; for (int n=1; n<=x; n++) { //遍历输出偶数 if (n%2 == 0) { cout << n << " "; } } a--; } return 0; }
时间0,内存240
最优解法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include<stdio.h> int main() { int n; scanf("%d",&n); int a; while(n--) { scanf("%d",&a); for(int i=1;i<=a;i+=2) printf("%d ",i); puts(""); for(int i=2;i<=a;i+=2) printf("%d ",i); puts(""); } }