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

杨辉三角的变形

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

引用的思路是题解场另一大佬的思路原链接

#include<stdio.h>

//杨辉三角规律                                    行号    第一个偶数在该行第几个
//                    1                           1             -1
//                1   1   1                       2             -1
//            1   2   3   2   1                   3              2
//         1  3   6   7   6   3   1               4              3
//      1  4  10  16  19  16  10  4  1            5              2
//   1  5  15 30  45  51  45  30  15 5  1         6              4
// 1 6  21 40 80  126 141 126 80  40 21 6  1      7              2
//  首个偶数在该行第几个的规律: -1 -1 (2 3 2 4)···(2 3 2 4)
int main(int argc, char const *argv[])
{
    int num = 0;
    int str[4] = {2, 3, 2, 4};
    printf("%d\n", 3%2);
    while (scanf("%d", &num) != EOF)
    {
        if (num <= 2)
        {
            printf("-1\n");
            return 0;
        }
        //因为是从第三行开始进入循环,
        printf("%d\n", str[ (num+1) % 4] );
    }
    return 0;
}
全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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