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

杨辉三角的变形

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

using System;

using System.Collections.Generic;

using System.Linq;

namespace HJ53

{

internal class Program

{

static void Main(string[] args)

{

int n = int.Parse(Console.ReadLine());

int[] intArr = GetIntArr(n);

for (int i = 0; i < intArr.Length; i++)

{

if (intArr[i] % 2 == 0)

{

Console.WriteLine(i + 1);

return;

}

}

Console.WriteLine(-1);

}

static int[] GetIntArr(int n)

{

if (n <= 0)

{

return null;

}

int[][] ints = new int[n][];

ints[0] = new int[] { 1 };

for (int i = 1; i < n; i++)

{

int[] lastArr = ints[i - 1];

int length = lastArr.Length;

int[] curentArr = new int[length + 2];

int first;

int second;

int third;

for (int j = 0; j < curentArr.Length; j++)

{

if (j < 2)

{

first = 0;

}

else

{

first = lastArr[j - 2];

}

if (j == 0 || j == curentArr.Length - 1)

{

second = 0;

}

else

{

second = lastArr[j - 1];

}

if (j > curentArr.Length - 3)

{

third = 0;

}

else

{

third = lastArr[j];

}

curentArr[j] = first + second + third;

}

ints[i] = curentArr;

}

return ints.Last();

}

}

}

全部评论
题目只是找偶数的位置,不是找出第一个偶数,所以不推荐用这个方法,这个方法容易空间超限,建议用找规律法
点赞
送花
回复
分享
发布于 01-03 03:05 广东

相关推荐

1 收藏 评论
分享
牛客网
牛客企业服务