problemscpp
A collection of my answers to algorithm problems in c++.
函数
pat::b::b1050 命名空间参考

1050 螺旋矩阵 更多...

函数

int main (istream &cin, ostream &cout)
 
 TEST (b1050, case1)
 

详细描述

1050 螺旋矩阵

函数说明

◆ main()

int pat::b::b1050::main ( istream &  cin,
ostream &  cout 
)

在文件 pat.cpp1635 行定义.

1635 {
1636 int N;
1637 cin >> N;
1638 int m = N;
1639 int n = 1;
1640 for(int i = 1; i * i <= N; ++i) {
1641 if(N % i == 0) {
1642 n = i;
1643 m = N / i;
1644 }
1645 }
1646 vector matrix(m, vector(n, 0));
1647 vector<int> vec(N);
1648 for(int i = 0; i < N; i++) {
1649 cin >> vec[i];
1650 }
1651 sort(vec.rbegin(), vec.rend());
1652 int direction = 0;//0 右 1 下 2 左 3 上
1653 int x = 0;
1654 int y = 0;
1655 int current = 0;
1656 matrix[0][0] = vec[current];
1657 --N;
1658 while(N != 0) {
1659 int next_x = x;
1660 int next_y = y;
1661 switch(direction) {
1662 case 0:
1663 next_y += 1;
1664 break;
1665 case 1:
1666 next_x += 1;
1667 break;
1668 case 2:
1669 next_y -= 1;
1670 break;
1671 case 3:
1672 next_x -= 1;
1673 break;
1674 }
1675 if(next_x < 0 || next_x >= m || next_y < 0 || next_y >= n || matrix[next_x][next_y] != 0) {
1676 direction += 1;
1677 direction %= 4;
1678 } else {
1679 matrix[next_x][next_y] = vec[++current];
1680 x = next_x;
1681 y = next_y;
1682 N--;
1683 }
1684 }
1685 for(int i = 0; i < m; i++) {
1686 for(int j = 0; j < n; j++) {
1687 cout << matrix[i][j];
1688 if(j != n - 1) {
1689 cout << ' ';
1690 }
1691 }
1692 if(i != m - 1) {
1693 cout << endl;
1694 }
1695 }
1696 return 0;
1697 }
const int N
Definition: acwing.h:146
int vec[100010]
Definition: pat.cpp:5095

引用了 acwing::acwing2019::N , 以及 pat::a::a7_2::vec.

被这些函数引用 TEST().

◆ TEST()

pat::b::b1050::TEST ( b1050  ,
case1   
)

在文件 pat_test.cpp765 行定义.

765 {
766 istringstream in("12\n37 76 20 98 76 42 53 95 60 81 58 93\n");
767 auto out = ostringstream();
768 main(in, out);
769 const auto ans = out.str();
770 ASSERT_EQ("98 95 93\n42 37 81\n53 20 76\n58 60 76", out.str());
771 }
int main(int argc, char **argv)
Definition: main.cpp:5

引用了 main().