当前位置:网站首页>华为机试题——HJ53 杨辉三角的变形
华为机试题——HJ53 杨辉三角的变形
2022-04-22 07:40:00 【青云 --小凡】
描述

以上三角形的数阵,第一行只有一个数1,以下每行的每个数,是恰好是它上面的数、左上角数和右上角的数,3个数之和(如果不存在某个数,认为该数就是0)。
求第n行第一个偶数出现的位置。如果没有偶数,则输出-1。例如输入3,则输出2,输入4则输出3,输入2则输出-1。
数据范围: 1 \le n \le 10^9 \1≤n≤109
输入描述:
输入一个int整数
输出描述:
输出返回的int值
示例1
输入:
4
复制输出:
3
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <iostream>
#include <string.h>
#include <malloc.h>
#include <sstream>
#include <algorithm>
#include <cstring>
#include <map>
#include <set>
#include <vector>
#include <list>
int Triangle(int num)
{
if(num == 1 || num == 2)
{
return -1;
}
int colume = (2 * num -1) / 2 + 2;
int row = num;
// int (*arr)[row] = new int[row][colume];
int **arr = new int*[row];
for(int j = 0; j < row; ++j)
{
arr[j] = new int[colume];
}
int jleft = 0; //从左向右的列下标
int jright = colume - 2; //从右向左的列下标
int i = 0; //行坐标
int j = 0;
for(i = row -1; i >= 0; --i)
{
for(j = jleft; j <= jright; ++j)
{
arr[i][j] = 0;
}
jleft++;
jright--;
}
arr[0][row - 1] = 1;
// for(int i = 0; i < row; ++i)
// {
// for(int j = 0; j < colume; ++j)
// {
// std::cout << arr[i][j] << " ";
// }
// std::cout << std::endl;
// }
for(int i = 0; i < row; ++i)
{
for(int j = 0; j < colume; ++j)
{
int tempi = i;
int tempj = j;
if(tempi == 0 && tempj == colume / 2)
{
break;
}
int leftup = 0; //左上角数字
int up = 0; //正上方数字
int rightup = 0; //右上角数字
if((tempi - 1 >= 0) && (tempj - 1 >= 0))
{
leftup = arr[tempi - 1][tempj - 1];
}
if(tempi - 1 >= 0)
{
up = arr[tempi - 1][tempj];
}
if((tempi - 1 >= 0) && (tempj + 1 <= colume))
{
rightup = arr[tempi - 1][tempj + 1];
}
arr[i][j] = leftup + up + rightup;
}
}
// for(int i = 0; i < row; ++i)
// {
// for(int j = 0; j < colume; ++j)
// {
// std::cout << arr[i][j] << " ";
// }
// std::cout << std::endl;
// }
//第4
for(int j = 0; j < colume; ++j)
{
int find = arr[row - 1][j];
if(find != 0 && find % 2 == 0)
{
for (int i = 0; i < row; i++) {
delete[] arr[i]; // 先释放掉一维数组
}
delete[] arr;
return j + 1;
}
}
for (int i = 0; i < row; i++) {
delete[] arr[i]; // 先释放掉一维数组
}
delete[] arr;
return -1;
}
int main()
{
int n;
std::cin >> n;
int index = Triangle(n);
std::cout << index << std::endl;
return 0;
}
版权声明
本文为[青云 --小凡]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_41765969/article/details/124333664
边栏推荐
- Flutter 判断网络可用性
- js学习笔记
- Variant quick platoon: find the largest number of top k
- What do you know about the well-known public DNS servers in China
- Golang learning, pointer, loop control, correlation
- oh-my-notepro
- BLDC双闭环(速度PI+电流PI)simulink仿真模型
- Cr doped strontium titanate Cr: SrTiO3 crystal substrate | NaCl < 111 > 10x10x2 0mm1sp crystal substrate | Al2O3 sapphire crystal substrate | Qiyue biology
- pycharm终端pip安装Error: “pip”项识别为 cmdlet、函数、脚本文件或可运行程序的名称
- SmartTabLayout 简介
猜你喜欢

Under the new retail development trend, how to operate and promote the social e-commerce platform?

Golang学习,指针,循环控制相关

nacos基础(3):OPEN API配置管理测试与关闭nacos服务

MaterialApp

Flutter ListView 加载更多

oh-my-notepro

What do you know about the well-known public DNS servers in China

Flutter GridView Demo

云计算学习2——keystone组件运维和测试

国内知名公共 DNS 服务器居然有你知道哪些
随机推荐
MaterialApp
sql需求处理篇-统计指定某年中有多少个周一至周日
tensorflow使用笔记
spark sql 获取数组某index处元素
用OnLayoutChangeListener的方法解决getTop=0的问题
进程和线程
shell脚本中ps -ef查询进程PID一直返回异常
js学习笔记
LeetCode_ 118. Yanghui triangle_ Dynamic programming_ Int * * learning
只有服务器,没有域名,怎么部署网站?
2-15 find the combination number
Layer1扩容:分片和可组合性
【论文阅读】【3d目标检测】pvgnet
nacos基础(3):OPEN API配置管理测试与关闭nacos服务
tf.keras.layers.InputLayer函数
pyqt5使用内置数据库sqllite
PDH光端机4路E1+4路百兆以太网 4路2M光端机 FC单纤20公里 机架式
Copula函数初了解
pycharm终端pip安装Error: “pip”项识别为 cmdlet、函数、脚本文件或可运行程序的名称
Redis entry required