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

杨辉三角的变形

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);

}

/// <summary>再来一个时间找空间的 </summary>

static int[] GetIntArr(int n)

{

if (n <= 0)

{

return null;

}

int[] temp= new int[] { 1 };

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

{

int[] lastArr = temp;

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;

}

temp = curentArr;

}

return temp;

}

}

}

全部评论

相关推荐

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