N阶楼梯上楼问题&旋转矩阵

  1. N阶楼梯

递归版

#include <cstdio>
#include <iostream>

using namespace std;

int ans;
void Solve(int n){
    if(n <= 0){
        if(n == 0) ans ++;
        return;
    }
    for(int i=1;i<=2;i++){
       Solve(n-i);
    }
}

int main(){
    int n;
    scanf("%d",&n);
    Solve(n);
    printf("%d\n",ans);
    return 0;
}

非递归版

#include <cstdio>
#include <iostream>

using namespace std;

int Combination(int n, int m){
    //普通做法
    int ans = 1;
    if(m > n/2) m = n-m;
    for(int i=0;i<m;i++)
        ans *= (n-i);
    for(int i=1;i<=m;i++)
        ans /= i;
    return ans;
}
int Solve(int n){
    int ans = 0;
    //n = 2x + y;
    for(int x=0;x<=n/2;x++){
         ans += Combination(n-x,x);
    }
    return ans;
}

int main(){
    int n;
    scanf("%d",&n);
    printf("%d\n",Solve(n));
    return 0;
}

2 . 旋转矩阵
分析: 题目并没有明说旋转方向,但测试数据默认为顺时针,坑爹!

略冗余版

#include <cstdio>
#include <iostream>

using namespace std;
const int N = 9;

int n;
int mat1[N][N], mat2[N][N];

void Read(int mat[][N]) {
    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
            scanf("%d", &mat[i][j]);
}
void FRead() {
    Read(mat1);
    Read(mat2);
}

bool Circle_0() {
    bool flag = true;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (mat1[i][j] != mat2[i][j]) {
                flag = false;
                break;
            }
        }
        if (!flag) break;
    }
    return flag;
}
bool Circle_90() {
    bool flag = true;
    //顺时针:第i行(从左到右)和第(n-1-i)列(从上向下)比较
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (mat1[i][j] != mat2[j][n - 1 - i]) {
                flag = false;
                break;
            }
        }
        if (!flag) break;
    }
    return flag;
}
bool Circle_180() {
    bool flag = true;
    //第i行(从左到右)和第(n - 1 - i)行(从右到左)比较
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (mat1[i][j] != mat2[n - 1 - i][n - 1 - j]) {
                flag = false;
                break;
            }
        }
        if (!flag) break;
    }
    return flag;
}
bool Circle_270() {
    bool flag = true;
    //第i行(从左到右)和第i列(从下到上)比较
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (mat1[i][j] != mat2[n - 1 - j][i]) {
                flag = false;
                break;
            }
        }
        if (!flag) break;
    }
    return flag;
}
void Judge() {
    if (Circle_0()) printf("0\n");
    else if (Circle_90()) printf("90\n");
    else if (Circle_180()) printf("180\n");
    else if (Circle_270()) printf("270\n");
    else printf("-1\n");
}
int main() {
    while (~scanf("%d", &n)) {
        FRead();
        Judge();
    }
    return 0;
}

——————————————————————————————————————————————————————–
简化版

#include <cstdio>
#include <iostream>

using namespace std;
const int N = 9;

int n;
int mat1[N][N], mat2[N][N];

void Read(int mat[][N]) {
    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
            scanf("%d", &mat[i][j]);
}
void FRead() {
    Read(mat1);
    Read(mat2);
}

void Judge() {
    int flag[4] = { 1,1,1,1 }; //标记0、90、180、270等4个方向
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (mat1[i][j] != mat2[i][j]) flag[0] = 0;
            if (mat1[i][j] != mat2[j][n - 1 - i]) flag[1] = 0;
            if (mat1[i][j] != mat2[n - 1 - i][n - 1 - j]) flag[2] = 0;
            if (mat1[i][j] != mat2[n - 1 - j][i]) flag[3] = 0;
        }
    }
    int cnt = 0;
    while (cnt < 4) {
        if (flag[cnt] == 0) cnt++;
        else {
            printf("%d\n", 90 * cnt);
            break;
        }
    }
    if (cnt == 4) printf("-1\n");
}
int main() {
    while (~scanf("%d", &n)) {
        FRead();
        Judge();
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值