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.
|
|
|
|
# $C++$语法基础课$20230212$讲义
|
|
|
|
|
|
|
|
|
|
## [$AcWing$ $753$. 平方矩阵 $I$](https://www.acwing.com/problem/content/755/)
|
|
|
|
|
|
|
|
|
|
<center><img src='http://dsideal.obs.cn-north-1.myhuaweicloud.com/HuangHai/BlogImages/2023/02/d2c3c1478f9b46e9ad622e337f0c9292.png' width="480" height="360"></center>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
**题解:**
|
|
|
|
|
1. 这是一道找规律的题,一般都是思考行与列的数据关系
|
|
|
|
|
2. 从左侧看,是找出行与列的最小值
|
|
|
|
|
3. 从右侧看,上面的办法似乎行不通,可以变通一下,假设我们是从右向左看,就是$n-i+1$就可以了~
|
|
|
|
|
|
|
|
|
|
```cpp {.line-numbers}
|
|
|
|
|
#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$](https://www.acwing.com/problem/content/description/756/)
|
|
|
|
|
|
|
|
|
|
<center><img src='https://cdn.acwing.com/media/article/image/2021/11/05/114892_ec7836a13d-111.png'></center>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
**题解:**
|
|
|
|
|
1. 这是一道找规律的题,类似平方矩阵I,这类题大多通过找横纵坐标$(i/j)$的关系,得到规律
|
|
|
|
|
2. 通过观察发现,对角线$i、j$相等之处,是$1$.其他的数是由$1$往两边发散
|
|
|
|
|
3. 继续通过观察,判断(除$i、j$相等之处)的数,都是横纵坐标之差的绝对值$+1$得到,
|
|
|
|
|
|
|
|
|
|
```cpp {.line-numbers}
|
|
|
|
|
#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](https://www.acwing.com/problem/content/description/757/)
|
|
|
|
|
|
|
|
|
|
<center><img src='http://dsideal.obs.cn-north-1.myhuaweicloud.com/HuangHai/BlogImages/2023/02/f3055756d4bd5c904855d01e7b3e5715.png' width="480" height="360"></center>
|
|
|
|
|
|
|
|
|
|
**题解**:
|
|
|
|
|
1. 需要知道$2^n$在$C++$中的计算办法`n<<1`
|
|
|
|
|
2. 其它,就没有了
|
|
|
|
|
|
|
|
|
|
```cpp {.line-numbers}
|
|
|
|
|
#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;
|
|
|
|
|
}
|
|
|
|
|
```
|