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平台——杨辉三角的变形的更多相关文章

  1. 华为oj----iNOC产品部-杨辉三角的变形 .

    此题提供三种方法,第一种,一开始就能想到的,设置一个足够大的数组存储生成的杨辉三角,然后进行判断就行,此方法参见:华为oj iNOC产品部-杨辉三角的变形 另一种方法是采用递归: 三角形的每行的个数为 ...

  2. 华为机试-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,以下每行的每个数,是恰好是它上面的数,左上角数 ...

  3. [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, ...

  4. LeetCode(119):杨辉三角 II

    Easy! 题目描述: 给定一个非负索引 k,其中 k ≤ 33,返回杨辉三角的第 k 行. 在杨辉三角中,每个数是它左上方和右上方的数的和. 示例: 输入: 3 输出: [1,3,3,1] 进阶: ...

  5. 以杨辉三角为例,从内存角度简单分析C语言中的动态二维数组

    学C语言,一定绕不过指针这一大难关,而指针最让人头疼的就是各种指向关系,一阶的指针还比较容易掌握,但一旦阶数一高,就很容易理不清楚其中的指向关系,现在我将通过杨辉三角为例,我会用四种方法从内存的角度简 ...

  6. hdu 5698(杨辉三角的性质+逆元)

    ---恢复内容开始--- 瞬间移动 Accepts: 1018 Submissions: 3620 Time Limit: 4000/2000 MS (Java/Others) Memory Limi ...

  7. [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 ...

  8. [LeetCode] Pascal's Triangle 杨辉三角

    Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...

  9. POJ2167Irrelevant Elements[唯一分解定理 组合数 杨辉三角]

    Irrelevant Elements Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 2407   Accepted: 59 ...

随机推荐

  1. 用C#将输入的小写字母转化为大写字母

    string A = "adsaf"; string B =""; B=A.ToUper();

  2. 黄聪:MYSQL5.6缓存性能优化my.ini文件配置方案

    使用MYSQL版本:5.6 [client] …… default-character-set=gbk default-storage-engine=MYISAM max_connections=10 ...

  3. Bugtags,产品经理的瑞士军刀

    做为设计移动应用的产品经理,每天的主要工作就是在手机上不停的体验自己的产品,发现问题.优化体验.你是否经常工作在这样的尴尬场景: 发现界面问题,将问题界面截屏传到电脑,用图片标记工具将问题标记出来,然 ...

  4. JSON.stringify()的使用--将string转换成json

    ===========================================================1. ====JSON.stringify()================== ...

  5. PHP 时区设置

    有时候使用date("Y-m-d h:i:s")时发现时间相差8小时,修改“/etc/php5/apache2/php.ini”: date.timezone = "As ...

  6. 关于position的疑惑

    我到现在也不是很清楚position的效果会是什么,或者是什么情况下需要指明为relatieve什么情况下又需要absolute呢? <div style="position:rela ...

  7. PostgreSQL在Ubuntu上安装指南

    安装环境: Ubuntu 10.04-desktop-i386 PostgreSQL 8.4 1. 安装PostgreSQL 输入如下命令 sudo apt-get install postgresq ...

  8. [kuangbin带你飞]专题十 匹配问题

        A-L 二分匹配 M-O 二分图多重匹配 P-Q 二分图最大权匹配 R-S 一般图匹配带花树 模板请自己找     ID Origin Title   61 / 72 Problem A HD ...

  9. esriSRGeoCS2Type Constants

    ArcGIS Developer Help  (Geometry)     esriSRGeoCS2Type Constants More geographic coordinate systems. ...

  10. cocos2dx 菜单按钮回调方法传参 tag传参

    .h文件 void menuCallBack(CCObject* pSender); .cpp CCMenuItemSprite* item = CCMenuItemSprite::create( m ...