华为OJ平台——杨辉三角的变形
import java.util.Scanner; /**
* 杨辉三角的变形
*第一行为1,后面每一行的一个数是其左上角到右上角的数的和,没有的记为0
* 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。。。
*求第x行的第一个偶数是第几个
*
*/
public class YangHui { public static void main(String[] args) {
Scanner cin = new Scanner(System.in) ;
int line = cin.nextInt() ;
cin.close(); System.out.println(run(line)) ; } /**
* 计算返回值
* @param x
* @return
*/
public static int run(int x){
if(x == 1 || x == 2){
return -1 ;
}
//每一行的第一个数为1,第二个数为n-1;第三个数为 n*(n-1)/2
if(x%2 == 1){
return 2 ;
}else if(x*(x-1)%4 == 0){
return 3 ;
}
//若前三个均不是偶数,则从第四个数开始计算,由于是对称的的,所以判断到第x行的第x个数就可以了
for(int i = 4 ; i <= x ; i++){
int res = cal(x,i) ;
if(res%2 == 0){
return i ;
}
}
return -1 ;
} /**
* 传入n,i表示第n行的第i个,返回其值,递归的方法求解
* @param n
* @param i
* @return
*/
public static int cal(int n, int i){
if(i > n){
return cal(n,2*n-i) ;
}
if(n == 2 && i > 0){
return 1 ;
}
if(i == 1){
return 1 ;
}
if(i <= 0){
return 0 ;
}
int res ;
res = cal(n-1,i) + cal(n-1,i-1) + cal(n-1,i-2) ;
return res ;
} }
华为OJ平台——杨辉三角的变形的更多相关文章
- 华为oj----iNOC产品部-杨辉三角的变形 .
此题提供三种方法,第一种,一开始就能想到的,设置一个足够大的数组存储生成的杨辉三角,然后进行判断就行,此方法参见:华为oj iNOC产品部-杨辉三角的变形 另一种方法是采用递归: 三角形的每行的个数为 ...
- 华为机试-iNOC产品部-杨辉三角的变形
题目描述 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,以下每行的每个数,是恰好是它上面的数,左上角数 ...
- [LeetCode] Pascal's Triangle II 杨辉三角之二
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3, ...
- LeetCode(119):杨辉三角 II
Easy! 题目描述: 给定一个非负索引 k,其中 k ≤ 33,返回杨辉三角的第 k 行. 在杨辉三角中,每个数是它左上方和右上方的数的和. 示例: 输入: 3 输出: [1,3,3,1] 进阶: ...
- 以杨辉三角为例,从内存角度简单分析C语言中的动态二维数组
学C语言,一定绕不过指针这一大难关,而指针最让人头疼的就是各种指向关系,一阶的指针还比较容易掌握,但一旦阶数一高,就很容易理不清楚其中的指向关系,现在我将通过杨辉三角为例,我会用四种方法从内存的角度简 ...
- hdu 5698(杨辉三角的性质+逆元)
---恢复内容开始--- 瞬间移动 Accepts: 1018 Submissions: 3620 Time Limit: 4000/2000 MS (Java/Others) Memory Limi ...
- [LeetCode] 119. Pascal's Triangle II 杨辉三角之二
Given a non-negative index k where k ≤ 33, return the kth index row of the Pascal's triangle. Note t ...
- [LeetCode] Pascal's Triangle 杨辉三角
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...
- POJ2167Irrelevant Elements[唯一分解定理 组合数 杨辉三角]
Irrelevant Elements Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 2407 Accepted: 59 ...
随机推荐
- Redis内存使用优化与存储(转)
Redis常用数据类型 Redis最为常用的数据类型主要有以下五种: String Hash List Set Sorted set 在具体描述这几种数据类型之前,我们先通过一张图了解下Redis内部 ...
- (WPF) MVVM: ComboBox Binding, XML 序列化
基本思路还是在View的Xmal里面绑定ViewModel的属性,虽然在View的后台代码中也可以实现binding,但是还是在Xmal里面相对的代码量要少一些. 此例子要实现的效果就是将一个List ...
- java.lang.ClassNotFoundException: org.eclipse.jetty.plus.webapp.EnvConfiguration
最近刚接触jetty,在myeclipse8.6中加入了一个项目,运行时就出了这个java.lang.ClassNotFoundException: org.eclipse.jetty.plus.we ...
- Android: 触屏fling/scroll/drag的区别及其详细过程
Google了一下,终于搞清了touch screen下的几种操作模式(对应的是事件). 对于一个view, 常用的操作有点击(click)和长按(long press)二种.实际上,这些操作类型是A ...
- ERP_基于Oracle ADF的定制化企业级IT系统解决方案
2014-12-31 Created By BaoXinjian
- CE_现金银行对账单的手工导入和调节(案例)
2014-07-14 Created By BaoXinjian
- PLSQL_闪回删除FlashBack Drop表误删除如何进行恢复(案例)
2014-06-25 Created By BaoXinjian
- tar.gz和rpm安装文件(转载)
from:http://bbs.chinaunix.net/thread-2277750-1-1.html Linux软件的二进制分发是指事先已经编译好二进制形式的软件包的发布形式,其优点是安装使用容 ...
- python异常类型
python2: BaseException +-- SystemExit +-- KeyboardInterrupt +-- GeneratorExit +-- Exception +-- Stop ...
- 【cl】sikuli启动不了
公司电脑:win7+64位 问题:点击sikuli_ide没有反应 卸载了,启动电脑,重新安装.