杨辉三角的变形【编程题】
来源:互联网 发布:让wow.js兼容ie8 编辑:程序博客网 时间:2024/06/05 19:47
题目
描述
1 1 1 1 1 2 3 2 1 1 3 6 7 6 3 11 4 10 16 19 16 10 4 1
以上三角形的数阵,第一行只有一个数1,以下每行的每个数,是恰好是它上面的数,左上角数到右上角的数,3个数之和(如果不存在某个数,认为该数就是0)。
求第n行第一个偶数出现的位置。如果没有偶数,则输出-1。例如输入3,则输出2,输入4则输出3。
输入
输入一个int整数
输出
输出返回的int值
样例输入
4
样例输出
3
代码
这题毫无思路。。。看过大神的代码。。。我可以膜拜一下了
这题用递归写的真是太赞了~
#include <iostream>using namespace std;int C(int n,int k){ if(k==1 || k==n*2-1) return 1; if (k<1 ||k>n*2-1) return 0; else return C(n-1,k-2)+C(n-1,k-1)+C(n-1,k);}int main(){ int N; cin>>N; for(int j=1; j<=2*N-1; j++) { if(C(N,j)%2==0) { cout<<j<<endl; break; } else if(j==2*N-1) cout<<"-1"<<endl; }}
第二个代码。。。
大神用栈的思想一层一层来求。。。膜拜
#include <iostream>#include <vector>#include <string>#include <set>#include <algorithm>using namespace std;int main(){ vector<int> current,next; int n; cin>>n; n--;//第一行没有偶数 current.push_back(1);//最左边的1 while(n--) { next.push_back(1); if(current.size()==1)//第二行 { next.push_back(1); } else//中间的数 { for(unsigned int i=0; i<current.size(); i++) { if(i==0) { next.push_back(current[i]+current[i+1]); } else if(i<=current.size()-2) { next.push_back(current[i]+current[i-1]+current[i+1]); } else { next.push_back(current[i]+current[i-1]); } } } next.push_back(1);//最右边的1 current.clear(); current=next; next.clear(); } unsigned int i; for(i=0; i<current.size(); i++) { if(current[i]%2==0) { cout<<i+1<<endl; break; } } if(i==current.size()) { cout<<-1<<endl; } return 0;}
0 0
- 杨辉三角的变形【编程题】
- [编程题]iNOC产品部-杨辉三角的变形
- 杨辉三角的变形
- 杨辉三角的变形
- 杨辉三角的变形
- 杨辉三角的变形
- 杨辉三角的变形
- 杨辉三角的变形
- 杨辉三角变形
- 英雄会-----杨辉三角-----杨辉三角的变形
- 庞果网之杨辉三角的变形
- 杨辉三角的变形-matlab-java
- HWOJ 杨辉三角的变形
- 【华为 OJ 】杨辉三角的变形
- 庞果网---杨辉三角变形
- 变形的杨辉三角问题的一种解法
- pongo题解之杨辉三角的变形
- pongo题解之杨辉三角的变形C#
- JavaWeb入门(四) I-O
- 第十章.巨幕页头缩略图和图文结合和警告框组件
- linux时间函数time和gettimeofday
- 关于iOS设备参数的获取
- java 字节码操作(javassist)
- 杨辉三角的变形【编程题】
- 百度地图 雷达/地理编码 功能使用
- 解决Python中格式化Dict对象时,中文为Unicode的问题
- MediaPlayer+TextureView实现小视频居中(不拉伸)播放
- dispatchkeyevent的调用机制
- protobuf
- 一个字符串在另一个字符串中出现的次数的最简方法
- DOM chapter05
- 工具类库系列(一)-StringTool