結果

問題 No.401 数字の渦巻き
ユーザー しめはじめしめはじめ
提出日時 2019-07-04 11:00:59
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 1 ms / 2,000 ms
コード長 1,669 bytes
コンパイル時間 509 ms
コンパイル使用メモリ 30,308 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-19 07:33:37
合計ジャッジ時間 1,248 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 1 ms
4,348 KB
testcase_03 AC 1 ms
4,348 KB
testcase_04 AC 1 ms
4,348 KB
testcase_05 AC 1 ms
4,348 KB
testcase_06 AC 1 ms
4,348 KB
testcase_07 AC 1 ms
4,348 KB
testcase_08 AC 1 ms
4,348 KB
testcase_09 AC 1 ms
4,348 KB
testcase_10 AC 1 ms
4,348 KB
testcase_11 AC 1 ms
4,348 KB
testcase_12 AC 1 ms
4,348 KB
testcase_13 AC 1 ms
4,348 KB
testcase_14 AC 1 ms
4,348 KB
testcase_15 AC 1 ms
4,348 KB
testcase_16 AC 1 ms
4,348 KB
testcase_17 AC 1 ms
4,348 KB
testcase_18 AC 1 ms
4,348 KB
testcase_19 AC 1 ms
4,348 KB
testcase_20 AC 1 ms
4,348 KB
testcase_21 AC 1 ms
4,348 KB
testcase_22 AC 1 ms
4,348 KB
testcase_23 AC 1 ms
4,348 KB
testcase_24 AC 1 ms
4,348 KB
testcase_25 AC 1 ms
4,348 KB
testcase_26 AC 1 ms
4,348 KB
testcase_27 AC 1 ms
4,348 KB
testcase_28 AC 1 ms
4,348 KB
testcase_29 AC 1 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>
int main(void){
    int n = 0;
    int i = 0, j = 0;;
    int xpos = 0, ypos = 0; //位置
    int xstep = 1, ystep = 0;   //進む方向
    int box[30][30];  // 数字のセットを行うマス
    
    // 入力と初期化
    scanf( "%d", &n );
    for( i = 0; i < n; i++ ){
        for( j = 0; j < n; j++ ){
            box[i][j] = 0;
        }
    }

    for( i = 1; i <= n * n; i++ ){
        // 位置に数字をセット
        box[xpos][ypos] = i;
        // 向きの変更
        // 端まで来てしまったか、すでに行き先に数字がセットされているか
        if( xpos + xstep >= n || ypos + ystep >= n
            || xpos + xstep < 0 || ypos + ystep < 0
            || box[xpos + xstep][ypos + ystep] != 0 ){
            // 常に突き当ったら右に向かって曲がる
            if( xstep == 1 ){
                xstep = 0;
                ystep = 1;
            }else if( xstep == -1 ){
                xstep = 0;
                ystep = -1;
            }else if( ystep == 1 ){
                xstep = -1;
                ystep = 0;
            }else if( ystep == -1 ){
                xstep = 1;
                ystep = 0;
            }
        }
        // 次の行き先を指定する(行先の方向については上記処理で調整済み)
        xpos += xstep;
        ypos += ystep;
    }
    
    //出力
    for( ypos = 0; ypos < n; ypos++ ){
        for( xpos = 0; xpos < n; xpos++ ){
            printf( "%03d", box[xpos][ypos] );
            if( xpos == n - 1 ){
                printf( "\n" );
            }else{
                printf( " " );
            }
        }
    }

}
0