返回题解分享
讨论 / 题解分享/ 帖子详情

蛇形填数(结果填空) - 题解

//
// Created by 30945 on 2024/4/12.
//非常感谢up提供的教程,之前学了半天没明白这个题,看了up的解法一个小时就彻底拿下这类题了
#include<bits/stdc++.h>

#define int long long
#define endl '\n'

using namespace std;

signed main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int m = 3, n = 3, num = 0;
    int cnt = m + n - 1;//确定是哪一斜排

    for (int i = 1; i < cnt; i++) {//把前i-1排有多少个数记录下来
        num += i;
    }
    //确定那一排是从上往斜下还是从下往斜上填
    if (cnt % 2 == 0) {
        //偶数是从上往斜下填
        int row = 1;
        while (row <= m) {
            num++;
            row++;
        }
    } else {
        int row = cnt;
        while (row >= m) {
            num++;
            row--;
        }
    }

    cout << num << endl;
    return 0;
}
0 回复 0 转发 2 喜欢 0 阅读
回复 (0)
默认 最新
暂无回复,快来抢沙发!