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

杨辉三角的变形

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

#include <bits/stdc++.h>

using namespace std;

//找规律
//分析思路:
//                                          1
//                                     1    1    1
//                                1    2    3      2      1
//                           1    3    6     7     6      3    1
//                      1    4    10   16    19    16    10    4    1
//                  1   5    15   30   45    51    45    30    15    5    1
//              1   6   21   50   90   126   141   126   90    50    21   6    1
//          1   7   28  77
//      1   8   36  112
//  1   9   51  156
//  10 
//通过以上的数据分析规律为 {-1,-1,2,3,2,4,2,3,2,4,...}

void process(int n, int& res){
    if(n == 1 || n == 2) {
        res = -1;
        return;
    }

    /*//构建普通的杨辉三角
    vector<vector<int>> matrix(n + 1, vector<int>(n + 1, 0)); // //二维数组
    for(int i = 0; i <= n; i++){
        matrix[i].resize(i + 1); //第i行共有i+1个元素
        matrix[i][0] = 1; matrix[i][i] = 1; //首尾都是1
        
        for(int j = 1; j < i; j++){ //
            matrix[i][j] = matrix[i - 1][j - 1] + matrix[i - 1][j];
        }
    }*/
    
    int myInt[]={4, 2, 3, 2};
    res=myInt[(n - 2) % 4]; //
    
    return;
    
}

int main(){
    int n = 0;
    while(cin >> n){
        int res = 0;
        process(n, res);
        
        cout << res << endl;
    }
    
    return 0;
}
华为题库题解 文章被收录于专栏

牛客华为题库的题解

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务