1021. There was a mountain in the past, and 1021 there was a mountain in the past

Source: Internet
Author: User

1021. There was a mountain in the past, and 1021 there was a mountain in the past
Description

In the past there was a mountain. The top view of the mountain was a n × n rectangle. () The lowest altitude was 1, and then the altitude rose along the ring.

Given the value of n, output the height chart of the mountain. Input Format

The input contains only one row, which is a positive integer n. Output Format

The output is the altitude chart of the mountain. Sample Input

4
Sample Output
     1     2     3     4    12    13    14     5    11    16    15     6    10     9     8     7
 

 

Shanghai Jiao Tong University acm library, address http://acm.sjtu.edu.cn/OnlineJudge/problem/1021

Google once, the existing practices on the Internet do not seem to be as good as mine, so I posted my practices ^_^.

 

The basic idea is to open up a n * n matrix, fill in a number indicating the height, and then output this matrix.

The question is how to fill in the correct number.

 

Take n = 5 as an example. Observe the number in the outermost circle and divide it into four parts,

Obviously, each part can be filled with a loop. Normally, the pseudocode that fills the entire circle is:

for i = 1 .. N-1

    map[1][i] = i

    map[i][N] = N - 1 + i

    map[N][N - i] = 2 * (N - 1) + i

    map[N - i][1]= 3 * (N - 1) + i

For non-outermost loops, record the offset of the starting filling position (including the X axis offset, Y axis offset, and fill digit offset.

Finally, for an odd side length matrix, you must fill in the maximum height (that is, the square of the side length) in the center of the matrix.

#include <iostream>#include <iomanip>#include <string.h>using namespace std;int main(){int i, j;int n;int *map;cin >> n;map = new int[n * n];memset(map, 0, sizeof(map));int h_offset = 1;int x_offset = 0;int y_offset = 0;for (int len = n - 1; len > 0; len-= 2){//a loop fills a circle, from outer to innerfor (i = 0; i < len; ++i){map[x_offset * n + (y_offset + i)] = h_offset + i;map[(x_offset + i) * n +  (n - 1 - y_offset)] = h_offset + len + i;map[(n - 1 - x_offset) * n + (n - 1 - y_offset - i)] = h_offset + 2 * len + i;map[(n - 1 - x_offset - i) * n + (y_offset)] = h_offset + 3 * len + i;}h_offset += 4 * len;x_offset ++;y_offset ++;}if(n % 2 == 1)map[n * n / 2] = n * n;for (i = 0; i < n; ++i){for (j = 0; j < n; ++j){cout << setw(6) << map[i * n + j];}cout << endl;}delete []map;return 0;}

Sixth-grade primary school mathematics Haidian xiaoyuan monkey peach

1/3 + 1/5 + 1/6 = 7/10
21/7*10 = 30 (count)
30*1/3 = 10; 30*1/5 = 6; 30*1/6 = 5 (count)
The old man and the old man have 10, 6, and 5 respectively.
(If you do not understand it, you can calculate it as: 1/3: 1/5: 1/6 =, and then get the answer (proportional relationship ))


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.