当前位置:网站首页>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
边栏推荐
- [if you want to do a good job, you must first use its tools] Guide for downloading and using paper editing and document management (endnote, latex, jabref, overflow) resources
- Kubernetes study notes
- Push data from onenet cloud platform to database
- 【Hcip】OSPF常用的6种LSA详解
- PIP install shutil reports an error
- Implementation of distributed scenario business operation log (based on redis lightweight)
- [XJTU computer network security and management] Lecture 2 password technology
- Rhcsa day 3 operation
- Difference between relative path and absolute path (often asked in interview)
- If MySQL / SQL server judges that the table or temporary table exists, it will be deleted
猜你喜欢
Linux redis - redis ha sentinel cluster construction details & redis master-slave deployment
Practical combat of industrial defect detection project (II) -- steel surface defect detection based on deep learning framework yolov5
php+mysql對下拉框搜索的內容修改
windows MySQL8 zip安装
Jupyter for local and remote access to ECS
[XJTU计算机网络安全与管理]第二讲 密码技术
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
Flink stream processing engine system learning (III)
Practice of industrial defect detection project (III) -- Based on FPN_ PCB defect detection of tensorflow
Download the genuine origin Pro 2022 tutorial and how to activate it
随机推荐
Classification and regression tree of machine learning
Step principle of logical regression in machine learning
php+mysql对下拉框搜索的内容修改
leetcode 烹飪料理
mysql function函数语法
MySQL complex query uses temporary table / with as (similar to table variable)
定了,今日起,本号粉丝可免费参与网易数据分析培训营!
Niuke hand speed monthly race 48 C (I can't understand the difference. It belongs to yes)
Flink stream processing engine system learning (I)
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
Log cutting - build a remote log collection server
Codeforces round 784 (Div. 4) (a - H)
基于Scrum进行创新和管理
The interface request takes too long. Jstack observes the lock holding
Log4j knowledge point record
First knowledge of C language ~ branch statements
Navicat failed to connect to Oracle Database: cannot load OCI DLL, 87: instant client package is
Solve the problem that the registered Google email Gmail mobile number cannot be used for verification
本地远程访问云服务器的jupyter
Domestic lightweight Kanban scrum agile project management tool