ZeroJudge K632: 產生隨機亂數2

題目敘述

每筆測資有一個正整數N,要求輸出將1到N的所有數字隨機排序過後的結果。


範例輸入 #1

3

範例輸出 #1

2 1 3


範例輸入 #2

5

範例輸出 #2

5 3 2 1 4


解題思路

因為從1到N輸出也是一種隨機排序的可能性,所以直接使用For迴圈從1輸出到N即可。

解題程式碼如下 (僅供參考):

#include <iostream>
using namespace std;

int main() {
    cin.sync_with_stdio(0);
    cin.tie(0);
    int N;
    cin >> N;
    for (int i = 0; i<N; i++)
    {
        cout << i+1 << " ";
    }
    cout << "\n";
}

留言

這個網誌中的熱門文章

ZeroJudge M933: 邏輯電路

ZeroJudge A148: You Cannot Pass?!

ZeroJudge M932: 蜜蜂觀察