Blue Bridge Cup 2020 11th C Language Group B Provincial Competition Exercise Problem Solution-Exercise C. Serpentine Filling in Numbers

Daily Questions (96)

The 11th Lanqiao Cup C Language Group B Provincial Competition Exercise C: Serpentine Filling in Numbers (10')

Insert picture description here
The C++ code is as follows:
(test code)

#include<iostream>
using namespace std;

int main()
{
    
    
	int a[500][500] = {
    
    0};
	a[1][1] = 1;
	
	for(int i = 2; i <= 200; i += 2)
	{
    
    	//向右 
		int j = i;
		a[1][j] = a[1][j - 1] + 1;
		//向左下
		int k;
		for(k = 2; k <= i; k++)
		{
    
    
			a[k][--j] = a[k - 1][j + 1] + 1;
		} 
		//向下
		a[i + 1][j] = a[i][j] + 1; 
		//向右上
		int m;
		int u = 1;
		for(m = i; m >= 1; m--)
		{
    
    
			a[m][++j] = a[m + 1][u++] + 1; 
			
		}
	}
	
	for(int i = 1; i <= 20; i++)
	{
    
    
		for(int j = 1; j <= 20; j++)
		{
    
    
			cout << a[i][j] << ' ';
		}
		cout << '\n';
	}
	cout << a[20][20] << endl;
	return 0;
}

The results of the operation are as follows:
Insert picture description here
Exam room code:

#include<iostream>
using namespace std;

int main()
{
    
    
	int a[500][500] = {
    
    0};
	a[1][1] = 1;
	
	for(int i = 2; i <= 200; i += 2)
	{
    
    	//向右 
		int j = i;
		a[1][j] = a[1][j - 1] + 1;
		//向左下
		int k;
		for(k = 2; k <= i; k++)
		{
    
    
			a[k][--j] = a[k - 1][j + 1] + 1;
		} 
		//向下
		a[i + 1][j] = a[i][j] + 1; 
		//向右上
		int m;
		int u = 1;
		for(m = i; m >= 1; m--)
		{
    
    
			a[m][++j] = a[m + 1][u++] + 1; 
			
		}
	}
	
//	for(int i = 1; i <= 20; i++)
//	{
    
    
//		for(int j = 1; j <= 20; j++)
//		{
    
    
//			cout << a[i][j] << ' ';
//		}
//		cout << '\n';
//	}
	cout << a[20][20] << endl;
	return 0;
}

Operation result:
Insert picture description here
So the answer is: 761

The second method:

The code is a hard simulation, once to the lower left, once to the upper right...

#include <bits/stdc++.h>
using namespace std;
int a[50][50],cnt=1;
int main()
{
    
    
    for(int i = 1 ; i <= 40; i++)
	{
    
    
        if(i % 2==1 )
		{
    
    
            for(int x = i, y = 1; x >= 1 && y <= i; x--, y++)
                a[x][y] = cnt++;
        }
        else
		{
    
    
            for(int x = 1, y = i; x <= i && y >= 1; x++, y--)
                a[x][y] = cnt++;
        }
    }
    printf("%d\n", a[20][20]);
	return 0;
}

If you like my article, please remember three consecutive times, like and follow the collection, every like and every one of your attention and every collection will be my infinite motivation on the way forward! ! ! ↖(▔▽▔)↗Thank you for your support, the next issue will be more exciting! ! !

Guess you like

Origin blog.csdn.net/qq_44631615/article/details/113802952