列印數字回環

2021-09-08 19:52:29 字數 1091 閱讀 7067

題目要求:

input a value n, then print out a n×n matrix.

example 1: input 2, output

1 24 3

example2: input 5, output

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

思路:把該輸出劃分為多個環,每乙個環上有4根線。分別為0,1,2,3號。

最外層的環每根線上有n-1個數字,每靠內一層。線上數字少2個。

計算每乙個位置(row, col)相應的環、線、線內索引。進而求出環內索引和全域性索引。在該位置列印全域性索引就可以。

**例如以下:

#include

using

namespace

std;

int get_number(

introw, 

intcol, 

intn)

else

if  

(n_1-row == loop)

else

if  

(n_1-col == loop)

else

if  

(row == loop)

intline_length = n_1 - loop * 2;

intindex_in_loop = line * line_length + index_in_line;

intbefore_loop = 4 * (n_1 - loop + 1) * loop;

return

before_loop + index_in_loop + 1;

}

void

print_square(

intn)

printf(

"\n");}

}int  

main ()

return0;}

python 列印回環數字

有這樣乙個題 輸入n,假設n 4,輸出 10 11 12 1 9 16 13 2 8 15 14 3 7 6 5 4 1 根據行來分隔,將每一行作為乙個子列表放入列表l,而子列表的元素是每一行的數字。那每個數字都對應乙個座標值 x,y 我們只需要順時針依次填數即可 2 首先建立全為0的目標矩陣 l ...

written amount 列印數字)

include include include static char digits static char tens static char magnitudes static void do one group unsigned int amount,char buffer,char magni...

lintcode 用遞迴列印數字

遞迴的方法找到從1到最大的n位整數。樣例給出n 1,返回 1,2,3,4,5,6,7,8,9 給出n 2,返回 1,2,3,4,5,6,7,8,9,10,11,99 注意用下面這種方式去遞迴其實很容易 recursion i 但是這種方式會耗費很多的遞迴空間,導致堆疊溢位。你能夠用其他的方式來遞迴使...