华为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 ...
随机推荐
- CXF超时设置
转自: http://peak.iteye.com/blog/1285211 http://win.sy.blog.163.com/blog/static/9419718620131014385644 ...
- (一)java的由来
java的诞生:每一次设计语言的革新都是为了解决先前语言所遇到的不能解决的问题,B语言导致C语言的诞生,C语言演变成C++,java则继承了这两种语言的大部分特性.java最初的推动力是为了找到一种能 ...
- C#属性访问器
属性的访问器包含与获取或设置属性有关的可执行语句.访问器声明可以包含 get 访问器或 set 访问器,或者两者均包含.声明采用下列形式之一:get {}set {} get 访问器get 访问器体与 ...
- Ruby1.9.3-下载网络图片至本地,并按编号保存。
#本程序功能:下载网络图片至本地,并按编号保存. #使用Ruby1.9.3在winxp_sp3下编写. require 'nokogiri' require 'open-uri' #以下 根据网址解析 ...
- 在单线程中你最好使用ArrayList而不是Vector
<java核心技术卷一>571页上提到Vector类的所有方法都是同步的.可以由两个线程安全地访问同一个Vector对象.显然,如果可以确定我们不会在多个线程中对这个数组进行操作的话,我们 ...
- 获取mysql数据表中的列名
select COLUMN_NAME from information_schema.columns where table_name='table_name' DESCRIBE table_name ...
- Java注解处理器--annotation学习四
Java中的注解(Annotation)是一个很神奇的东西,特别现在有很多Android库都是使用注解的方式来实现的.一直想详细了解一下其中的原理.很有幸阅读到一篇详细解释编写注解处理器的文章.本文的 ...
- ArrayList,Vector线程安全性测试
import java.util.ArrayList; import java.util.List; //实现Runnable接口的线程 public class HelloThread implem ...
- OperationResult
public class OperationResult<T> { private readonly ConcurrentDictionary<string, T> _valu ...
- java获取本机IP地址和MAC地址的方法
// 获取ip地址 public static String getIpAddress() { try { Enumeration<NetworkInterface> allNetInte ...