当前位置:网站首页>Huawei machine test question -- deformation of hj53 Yang Hui triangle
Huawei machine test question -- deformation of hj53 Yang Hui triangle
2022-04-23 02:49:00 【Qingyun Xiaofan】
describe

The number matrix of the above triangle , The first line has only one number 1, Each number in each of the following lines , Is exactly the number above it 、 The number in the upper left corner and the number in the upper right corner ,3 Sum of numbers ( If there is no number , Think this number is 0).
Please n Where the first even number of the line appears . If there is no even number , The output -1. For example, the input 3, The output 2, Input 4 The output 3, Input 2 The output -1.
Data range : 1 \le n \le 10^9 \1≤n≤109
Input description :
Enter a int Integers
Output description :
Output returned int value
Example 1
Input :
4
Copy output :
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; // Left to right column subscript
int jright = colume - 2; // The right subscript column is left
int i = 0; // Row coordinates
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; // Upper left digit
int up = 0; // Number directly above
int rightup = 0; // The number in the upper right corner
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;
// }
// The first 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]; // First release the one-dimensional array
}
delete[] arr;
return j + 1;
}
}
for (int i = 0; i < row; i++) {
delete[] arr[i]; // First release the one-dimensional array
}
delete[] arr;
return -1;
}
int main()
{
int n;
std::cin >> n;
int index = Triangle(n);
std::cout << index << std::endl;
return 0;
}
版权声明
本文为[Qingyun Xiaofan]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220740303352.html
边栏推荐
- The penultimate K nodes in jz22 linked list
- Deploying sbert model based on torchserve < semantic similarity task >
- 《信息系统项目管理师总结》第七章 项目沟通管理
- mysql function函数语法
- Yes, from today on, our fans can participate in Netease data analysis training camp for free!
- 高效音乐格式转换工具Music Converter Pro
- 5W of knowledge points
- Table space capacity query and expansion of Oracle Database
- Windows MySQL 8 zip installation
- 下载正版Origin Pro 2022 教程 及 如何 激 活
猜你喜欢

Deploying sbert model based on torchserve < semantic similarity task >

Specific field information of MySQL export table (detailed operation of Navicat client)

Error installing Mongo service 'mongodb server' on win10 failed to start
![[xjtu Computer Network Security and Management] session 2 Cryptographic Technology](/img/b0/263e8dcbfeb2ce9f504a9c8eb76b07.png)
[xjtu Computer Network Security and Management] session 2 Cryptographic Technology

leangoo脑图-共享式多人协作思维导图工具分享

How can enterprises with major hazard installations ensure the completion of the digital construction task of double prevention mechanism by the end of the year

hack the box optimum靶机

Log cutting - build a remote log collection server

Leangoo brain map - shared multi person collaborative mind mapping tool

Machine learning (Zhou Zhihua) Chapter 14 probability graph model
随机推荐
Servlet template engine usage example
Intelligent agricultural management model
Actual combat of industrial defect detection project (IV) -- ceramic defect detection based on hrnet
Kubernetes study notes
Error installing Mongo service 'mongodb server' on win10 failed to start
Niuke hand speed monthly race 48 C (I can't understand the difference. It belongs to yes)
Looking for a job, writing a resume to an interview, this set of information is enough!
Hack the box optimum
Essential qualities of advanced programmers
Fashion MNIST 数据集分类训练
ROP Emporium x86_ 64 7 ~ 8 questions
First knowledge of C language ~ branch statements
How to solve the complexity of project document management?
基于多态的职工管理系统源码与一些理解
Devil cold rice 𞓜 078 devil answers the market in Shanghai and Nanjing; Communication and guidance; Winning the country and killing and screening; The purpose of making money; Change other people's op
Shell learning notes -- shell processing of output stream awk
期中汇总(概论+应用层+运输层)
When using art template inheritance, compileerror: invalid or unexpected token generated
谷雨
1215_ Hello world used by scons