题解 | #杨辉三角的变形#

杨辉三角的变形

https://www.nowcoder.com/practice/8ef655edf42d4e08b44be4d777edbf43

#include <iostream>
using namespace std;
int count(int n) { //求第n行数字个数
    return 2 * n - 1;
}
int num(int n, int i) { //求第n行第i个数的值
    if (i > count(n)) return 0;
    if (i == 1) return 1;
    if (i == 2) return n - 1;
    if (i == 3) return n * (n - 1) / 2;
    if (i > 3) return num(n - 1, i) + num(n - 1, i - 1) + num(n - 1, i - 2);
    return -1;
}
int oushu(int n) { //寻找第n行第一个偶数的位置
    for (int i = 1; i <= count(n); i++)
        if (num(n, i) % 2 == 0) return i;
    return -1;
}
int main() {
    int a, b;
    cin >> a;
    b = oushu(a);
    cout <<  b << endl;
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务