当前位置:网站首页>Huawei interview question: tall and short people queue up
Huawei interview question: tall and short people queue up
2022-06-30 03:09:00 【The coolness of Si Ku Quan Shu】
subject
Now there is a group of children , They are different in height ,
We express the height of this group of children in an array of positive integers , Such as arrays {5,3,1,2,3}.
Now we want the children to line up , With “ high ”“ Short ”“ high ”“ Short ” Sequential arrangement ,
every last “ high ” The position of the children should be higher than the adjacent position or equal ;
every last “ Short ” The children in the position should be shorter than or equal to the adjacent position ;
Ask the children to move at a minimum distance , The first one from “ high ” Start to line up , Output the minimum moving distance .
for example , In the demonstration team {5,3,1,2,3} in ,{5, 1, 3, 2, 3} It's the sorting result .
{5, 2, 3, 1, 3} Although also satisfied “ high ”“ Short ”“ high ”“ Short ” Sequential arrangement ,
But children move a lot , So it's not the best result .
The definition of moving distance is as follows :
The second child moves behind the third child , The moving distance is 1,
If you move behind the fourth child , The moving distance is 2;
Input description :
Children in front of the order , Positive integer with English space :
4 3 5 7 8
notes : Children <100 individual
Output description :
The sorted children , A positive integer divided by English spaces :
4 3 7 5 8
remarks :4( high )3( Short )7( high )5( Short )8( high ),
The output result is the minimum moving distance , Only 5 and 7 Changed places , The moving distance is 1.
Example 1 :
Input
4 1 3 5 2
Output
4 1 5 2 3
Example 2 :
Input
1 1 1 1 1
Output
1 1 1 1 1
explain : Adjacent positions can be equal
Example 3 :
Input :
xxx
Output
[]
explain : Illegal parameter condition occurs , Return an empty array
Code :
#include<iostream>
#include<vector>
using namespace std;
int main(){
vector<int> cap;
int tmp;
while(cin >> tmp){
cap.push_back(tmp);
char c = cin.get();
if(c == '\n') break;
if(c != ' '){
cout << endl;
return 0;
}
}
int len = cap.size();
for(int i = 0; i < len; ++i){
if(i % 2 == 0 && i < len - 1 && cap[i] < cap[i + 1]) swap(cap[i],cap[i + 1]);
if(i % 2 == 1 && i < len - 1 && cap[i] > cap[i + 1]) swap(cap[i],cap[i + 1]);
i != 0 && cout << " ";
cout << cap[i];
}
cout << endl;
return 0;
}
边栏推荐
- 简单自定义mvc
- oracle怎么设置密码复杂度及超时退出的功能
- [oiclass] chess piece
- Raki's notes on reading paper: named entity recognition as dependency parsing
- 中断操作:AbortController学习笔记
- Welfare lottery | what are the highlights of open source enterprise monitoring zabbix6.0
- HTA introductory basic tutorial | GUI interface of vbs script HTA concise tutorial, with complete course and interface beautification
- JS 互相引用的问题
- HOOK Native API
- Note the use of export/import and class inheritance in ES6
猜你喜欢

Distributed file storage system fastdfs hands on how to do it

Auto.js学习笔记16:按项目保存到手机上,不用每次都保存单个js文件,方便调试和打包

发现mariadb数据库时间晚了12个小时

GTK interface programming (I): Environment Construction

Distributed file system fastdfs

General paging (2)

golang bilibili直播弹幕姬

How to modify and add fields when MySQL table data is large

Study diary: February 15, 2022

Compile a DLL without import table
随机推荐
An article to get you started VIM
Mysql表数据比较大情况下怎么修改添加字段
Multi card server usage
General paging (2)
The Oracle main program is deleted, but the data is on another hard disk. Can I import the data again?
简单自定义mvc
JS conversion of letters and numbers
oracle怎么设置密码复杂度及超时退出的功能
Welfare lottery | what are the highlights of open source enterprise monitoring zabbix6.0
中断操作:AbortController学习笔记
2. 成功解决 BUG:Exception when publishing, ...[Failed to connect and initialize SSH connection...
How to switch ipykernel to a different CONDA virtual environment in jupyterlab?
How do I enable assembly binding logging- How can I enable Assembly binding logging?
Sorting method of administrative route code letter + number
Shell counts all strings before the last occurrence of a string
DC/DC变换器轻载时三种工作模式的原理及优缺点
2022 underground coal mine electrical test and underground coal mine electrical simulation test
Cross domain, CORS, jsonp
Heavy attack -- ue5's open source digital twin solution
What is the metauniverse: where are we, where are we going