You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2.4 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

C++语法基础课20230212讲义

AcWing 753. 平方矩阵 I

题解:

  1. 这是一道找规律的题,一般都是思考行与列的数据关系
  2. 从左侧看,是找出行与列的最小值
  3. 从右侧看,上面的办法似乎行不通,可以变通一下,假设我们是从右向左看,就是n-i+1就可以了~
#include<iostream>
using namespace std;
int n;
int main(){
    while(cin>>n,n){
        for(int i=1;i<=n;i++){
            for(int j=1;j<=n;j++)
                printf("%d ",min(min(i,j),min(n+1-i,n+1-j)));
            
            puts("");
        }
       puts("");
    }
    return 0;
}

AcWing 754. 平方矩阵 II

题解:

  1. 这是一道找规律的题类似平方矩阵I这类题大多通过找横纵坐标(i/j)的关系,得到规律
  2. 通过观察发现,对角线i、j相等之处,是1.其他的数是由1往两边发散
  3. 继续通过观察,判断(除i、j相等之处)的数,都是横纵坐标之差的绝对值+1得到,
#include<iostream>
using namespace std;
int main(){
    int n;
    while(cin>>n && n){
        for(int i=1;i<=n;i++){
            for(int j=1;j<=n;j++)
                printf("%d ",abs(i-j)+1);
            puts("");
        }
        puts("");
    }
    return 0;
}

AcWing 755. 平方矩阵 III

题解 1. 需要知道2^nC++中的计算办法n<<1 2. 其它,就没有了

#include <iostream>
using namespace std;
int n;
const int N=110;

int main()
{
    while(cin>>n , n){
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++)
                printf("%d ",1<<(i+j));
            
           puts("");
        }
      puts("");
    }
    return 0;
}