Blue Bridge Cup Serpentine Filling

Insert picture description here
Insert picture description here
At the beginning, we made it together, but later found that the most direct law is the number sequence ai a_iai(The value of the i-th row and the i-th column) and ai a_iaiThe difference is an arithmetic series.
So an = 2 n (n − 1) + 1 a_n=2n(n-1)+1an=2 n ( n1)+1 , the process is as follows

Staggered addition method
a2-a1=4
a3-a2=8
a4-a3=12
·······
an-an-1=4(n-1)
add up to get an=a1+(4+8+… +4(n-1))

Answer: 761

#include<iostream>
using namespace std;
int main(){
    
    
    int n;
    cin>>n;
    cout<<2*n*(n-1)+1;
    return 0;
}
#include<iostream>
using namespace std;
int sum(int n){
    
    //计算1+2+...+n
    if(n==1)
        return 1;
    return sum(n-1)+n;
}
int main(){
    
    
    int n;
    // cout<<sum(6);
    cin>>n;
    cout<<sum(2*n-2)+1+(n-1);
    return 0;
}

Guess you like

Origin blog.csdn.net/Supreme7/article/details/114629849