当前位置:网站首页>华为面试题: 高矮个子排队
华为面试题: 高矮个子排队
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;
}
边栏推荐
- CMake教程系列-04-编译相关函数
- GTK interface programming (I): Environment Construction
- Global and Chinese market for defense network security 2022-2028: Research Report on technology, participants, trends, market size and share
- CMake教程系列-02-使用cmake代碼生成二進制
- (graph theory) connected component (template) + strongly connected component (template)
- Study diary: February 15, 2022
- 三层交换机和二层交换机区别是什么
- HTA introductory basic tutorial | GUI interface of vbs script HTA concise tutorial, with complete course and interface beautification
- Raki's notes on reading paper: Leveraging type descriptions for zero shot named entity recognition and classification
- mysqldump原理
猜你喜欢

Distributed file storage system fastdfs hands on how to do it

A quick look at the statistical data of 23 major cyber crimes from 2021 to 2022

Linear algebra Chapter 4 Summary of knowledge points of linear equations (Jeff's self perception)

并发请求下如何防重复提交

Wechat applet page Jump and parameter transfer

Cross domain, CORS, jsonp

自定义JvxeTable的按钮及备注下$set的用法

Federal learning: dividing non IID samples by Dirichlet distribution

FDA mail security solution

Intel hex, Motorola S-Record format detailed analysis
随机推荐
CMake教程系列-05-选项及变量
Add a custom button to jvxetable
Pytoch learning (II)
How to use vant to realize data paging and drop-down loading
CMake教程系列-04-编译相关函数
Use of Arthas
迅为恩智浦iTOP-IMX6开发平台
Global and Chinese market of relay lens 2022-2028: Research Report on technology, participants, trends, market size and share
怎么使用Vant实现数据分页和下拉加载
Linear algebra Chapter 4 Summary of knowledge points of linear equations (Jeff's self perception)
Ffmpeg source code
002 color classification
Série de tutoriels cmake - 02 - génération de binaires à l'aide du Code cmake
Redis+AOP怎么自定义注解实现限流
原生JS怎么生成九宫格
Mysql提取表字段中的字符串
Raii memory management
快速排序、聚簇索引、寻找数据中第k大的值
Raki's notes on reading paper: Leveraging type descriptions for zero shot named entity recognition and classification
What is the difference between a layer 3 switch and a layer 2 switch