YTU 3019: 螺旋方陣__YTU

來源:互聯網
上載者:User
3019: 螺旋方陣 時間限制: 1 Sec   記憶體限制: 128 MB
提交: 2   解決: 2
題目描述

以下是一個5*5階螺旋方陣。設計一個程式,輸出該形式的n*n階方陣(順時針方向旋進)。

      1    2    3    4    5    16   17   18   19    6    15   24   25   20    7    14   23   22   21    8    13   12   11   10    9

輸入

輸入一個整數n,(n>=1&&n<=20)

輸出

輸出對應的n*n階螺旋方陣。

範例輸入

5
範例輸出
    1    2    3    4    5   16   17   18   19    6   15   24   25   20    7   14   23   22   21    8   13   12   11   10    9
提示

兩個數字中間至少留有一個空格。

迷失在幽穀中的鳥兒,獨自飛翔在這偌大的天地間,卻不知自己該飛往何方……

//實驗6.1螺旋方陣#include <stdio.h>int main(){    int a,b,c,d,i,n,num = 1,array[105][105];    scanf("%d",&n);    for (a=0,b=n-1,c=0,d=n-1; a<=b; a++,b--,c++,d--)    {        for(i=c; i<=d; i++)array[a][i]=num++;        for(i=a+1; i<b; i++)array[i][d]=num++;        for(i=d; i>c; i--)array[b][i]=num++;        for(i= b; i>a; i--)array[i][c]=num++;    }    for (i=0; i<n; i++)        for (a=0; a<n; a++)            printf(a!=n-1?" %4d":" %4d\n", array[i][a]);    return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.