当前位置:网站首页>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
边栏推荐
- 5W of knowledge points
- 接口请求时间太长,jstack观察锁持有情况
- Linux redis - redis database caching service
- The problem of removing spaces from strings
- The penultimate K nodes in jz22 linked list
- The express project changes the jade template to art template
- Log cutting - build a remote log collection server
- 魔王冷饭||#078 魔王答上海、南京行情;沟通指导;得国和打杀筛选;赚钱的目的;改变别人看法
- Hack the box optimum
- C语言 171. 最近回文数
猜你喜欢
![[hcip] detailed explanation of six LSAS commonly used by OSPF](/img/31/3b92d42d16a056bf9db9e24471cefd.jpg)
[hcip] detailed explanation of six LSAS commonly used by OSPF

windows MySQL8 zip安装

16、 Anomaly detection

Servlet template engine usage example

C language 171 Number of recent palindromes

Fashion MNIST 数据集分类训练

Looking for a job, writing a resume to an interview, this set of information is enough!

Target narak

php+mysql对下拉框搜索的内容修改

Day 3 of learning rhcsa
随机推荐
Log4j知识点记录
Intelligent agricultural management model
[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
JZ35 复杂链表的复制
C语言 171. 最近回文数
JDBC JDBC
TypeScript(1)
基于ele封装下拉菜单等组件
The shell monitors the depth of the IBM MQ queue and scans it three times in 10s. When the depth value exceeds 5 for more than two times, the queue name and depth value are output.
Error installing Mongo service 'mongodb server' on win10 failed to start
Modification du contenu de la recherche dans la boîte déroulante par PHP + MySQL
How big the program development of single chip microcomputer project can be, it represents your level of knocking code
ele之Table表格的封装
JDBC JDBC
高效音乐格式转换工具Music Converter Pro
Android high-level interview must ask: overall business and project architecture design and reconstruction
MySQL complex query uses temporary table / with as (similar to table variable)
JVM运行时数据区(一)
能做多大的单片机项目程序开发,就代表了你的敲代码的水平
C language 171 Number of recent palindromes