当前位置:网站首页>华为面试题: 高矮个子排队
华为面试题: 高矮个子排队
2022-06-30 02:55:00 【四库全书的酷】
题目
现在有一队小朋友,他们高矮不同,
我们以正整数数组表示这一队小朋友的身高,如数组{5,3,1,2,3}。
我们现在希望小朋友排队,以“高”“矮”“高”“矮”顺序排列,
每一个“高”位置的小朋友要比相邻的位置高或者相等;
每一个“矮”位置的小朋友要比相邻的位置矮或者相等;
要求小朋友们移动的距离和最小,第一个从“高”位开始排,输出最小移动距离即可。
例如,在示范小队{5,3,1,2,3}中,{5, 1, 3, 2, 3}是排序结果。
{5, 2, 3, 1, 3} 虽然也满足“高”“矮”“高”“矮”顺序排列,
但小朋友们的移动距离大,所以不是最优结果。
移动距离的定义如下所示:
第二位小朋友移到第三位小朋友后面,移动距离为1,
若移动到第四位小朋友后面,移动距离为2;
输入描述:
排序前的小朋友,以英文空格的正整数:
4 3 5 7 8
注:小朋友<100个
输出描述:
排序后的小朋友,以英文空格分割的正整数:
4 3 7 5 8
备注:4(高)3(矮)7(高)5(矮)8(高),
输出结果为最小移动距离,只有5和7交换了位置,移动距离都是1.
示例一:
输入
4 1 3 5 2
输出
4 1 5 2 3
示例二:
输入
1 1 1 1 1
输出
1 1 1 1 1
说明:相邻位置可以相等
示例三:
输入:
xxx
输出
[]
说明:出现非法参数情况,返回空数组
代码:
#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;
}
边栏推荐
- 2. successfully solved bug:exception when publishing [Failed to connect and initialize SSH connection...
- Customize the buttons of jvxetable and the usage of $set under notes
- Global and Chinese market of relay lens 2022-2028: Research Report on technology, participants, trends, market size and share
- 多卡服务器使用
- Sitelock nine FAQs
- Raki's notes on reading paper: neighborhood matching network for entity alignment
- O & M (21) make winpe startup USB flash disk
- Call collections Sort() method, compare two person objects (by age ratio first, and by name ratio for the same age), and pass lambda expression as a parameter.
- c#控制台格式化代码
- GTK interface programming (II): key components
猜你喜欢

Cross domain, CORS, jsonp

2. 成功解决 BUG:Exception when publishing, ...[Failed to connect and initialize SSH connection...

Intel-Hex , Motorola S-Record 格式详细解析

How to use vant to realize data paging and drop-down loading

What files does a CA digital certificate contain? How to view SSL certificate information?

如何在 JupyterLab 中把 ipykernel 切换到不同的 conda 虚拟环境?

Série de tutoriels cmake - 02 - génération de binaires à l'aide du Code cmake

O & M (21) make winpe startup USB flash disk

Cmake tutorial series -02- generating binaries using cmake code

中断操作:AbortController学习笔记
随机推荐
How to modify and add fields when MySQL table data is large
What is the metauniverse: where are we, where are we going
Some configuration details about servlet initial development
Hands on in-depth learning notes (XV) 4.1 Multilayer perceptron
Cmake tutorial series -05- options and variables
Enlightenment from the revocation of Russian digital certificate by mainstream CA: upgrade the SSL certificate of state secret algorithm to help China's network security to be autonomous and controlla
Idea remote debugging remote JVM debug
NLP text summary: data set introduction and preprocessing [New York Times annotated corpus]
Cross domain, CORS, jsonp
Add a custom button to jvxetable
中断操作:AbortController学习笔记
Formal and actual parameters, value passing and address passing
How to prevent phishing emails? S/mime mail certificate
Cmake tutorial series -02- generating binaries using cmake code
LeetCode 3. 无重复字符的最长子串
Unity3D UGUI强制刷新Layout(布局)组件
Summary of knowledge points about eigenvalues and eigenvectors of matrices in Chapter 5 of Linear Algebra (Jeff's self perception)
IBM WebSphere channel connectivity setup and testing
How can redis+aop customize annotations to achieve flow restriction
2. 成功解决 BUG:Exception when publishing, ...[Failed to connect and initialize SSH connection...