[南陽OJ-No.11]奇偶數分離|有一個整型偶數n(2

南陽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

/*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

/*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

該解法來自網絡,原文請參照
來自安德里亞的成長:奇偶數分離

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,一個字,美!

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

#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
最優解法

#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("");
 }
}        
發佈了29 篇原創文章 · 獲贊 39 · 訪問量 8萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章