an interview question(2)
感觉现在好多面试题还是很注重基础的,今天面试时就遇到这题,回来一查后才知道此题是国内某著名通信公司的一道机试题:)
给定一个数组input[ ],如果数组长度n为奇数,则将数组中最大的元素放到
数组最中间的位置,如果数组长度n为偶数,则将数组中最大的元素放到 output[
] 数组中间两个位置偏右的那个位置上,然后再按从大到小的顺序,依次在第一个位置的两边,按照一左一右的顺序,依次存放剩下的数。
例如:input[] = {3, 6, 1, 9, 7}
output[] = {3, 7, 9, 6, 1}
input[]
= {3, 6, 1, 9, 7, 8}
output[] = {1, 6, 8, 9, 7, 3}
void sort (int input[ ],int n,int output[ ])
{
}
这题思路应该是怎样的呢:其实应该不难,首先先做下从小到大的排序,然后把最大的放中间,然后依次从大到小的往两边放是不是就OK了呢?
have a try!
#include <iostream>
using namespace std; #define N 1000 void bubbleSort(int a[],int len)
{
int i,j,temp;
for(i = ;i < len-;i++)
for(j = i+;j < len;j++)
{
if(a[i] > a[j])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
//for(i = 0;i < len;i++)
//cout << a[i] << " ";
} void sort(int input[], int len, int output[])
{ int mid = len/;
int left = mid - ;
int right = mid + ; bubbleSort(input,len);//先数据冒泡排序 int index = len - ;
output[mid] = input[index--];
while(index >= )
{
output[left--] = input[index--];
//index--;
output[right++] = input[index--];
//index--;
}
for(index = ;index < len;index++)
cout << output[index] << " ";
} int main()
{
int arr[] = {,,,,,};
int destArr[N];
sort(arr,,destArr);
bubbleSort(arr,);//输入数据冒泡排序
cout << endl; int arr1[] = {,,,,,,,,};
int destArr1[N];
sort(arr1,,destArr1); return ;
}
an interview question(2)的更多相关文章
- an interview question(1)
声明:本文为博主原创文章,未经博主允许不得转载. 以下是英文翻译: warnning: Copyright!you can't reprint this blog when you not get b ...
- Core Java Interview Question Answer
This is a new series of sharing core Java interview question and answer on Finance domain and mostly ...
- shit LeetCode interview Question
shit LeetCode interview Question https://leetcode.com/interview/1/ 有点晕,啥意思,没太明白,到底是要按什么排序呀? 去掉 标识符 不 ...
- JavaScript interview Question - Create a Array with two papameters without using loop!
JavaScript interview Question - Create a Array with two papameters without using loop! JavaScript - ...
- An interview question from MicroStrategy
去年校招时的一道面试题,觉得蛮有意思,贴出来. Question: Spy start at a, during an interval he moves |b| to right when b &g ...
- an interview question(4)
版权声明:本文为博主原创文章,未经博主允许不得转载. 写这篇博客前请让博主先吐糟下自己的PC. i3+2G内存+开了一上午=C盘剩下0字节+打开VS2012花了半个小时+一晚上的心情不好 吐槽完PC, ...
- an interview question(3)
最近看了些C面试题顺便复习一下C语言,现贴一些出来和大家分享. #include <stdio.h> void main () { ,,,,};--------- *(ptr++)+=; ...
- Interview Question
HDS(11.16.2015): How to design an non-stop website like Google or Amazon? What design patterns are y ...
- Amazon Interview Question: Design an OO parking lot
Design an OO parking lot. What classes and functions will it have. It should say, full, empty and al ...
随机推荐
- spring4 离线doc和api(自制)
spring mvc api 文档(英文版) 链接: https://pan.baidu.com/s/1c25Ml4 密码: ufb9 spring MVC 学习文档(英文版) 链接: https:/ ...
- ssh+expect批量分发
Expect安装 [root@web02 scripts]# yum install expect SSH密钥生成 [root@web02 scripts]# ssh-keygen -t dsa ...
- 浅谈HTTPS和SSL/TLS协议的背景和基础
相关背景知识要说清楚HTTPS协议的实现原理,至少要需要如下几个背景知识.大致了解几个基础术语(HTTPS.SSL.TLS)的含义大致了解HTTP和TCP的关系(尤其是"短连接"和 ...
- android手机和ios手机的分辨率
Android手机目前常见的分辨率 1.1 手机常见分辨率: 4:3 VGA 640*480 (Video Graphics Array) QVGA 320*240 (Quarter VGA ...
- 【原创】我所理解的自动更新-外网web服务器配置
ClientDownload和ClientUpdate共享渠道配置信息: channel-0.php //以appstore的渠道为例 <?php define('APPNAME', 'TOKE ...
- android下面使用SurfaceView+ mediaPlayer播放视频
final SurfaceView surfaceView = new SurfaceView(StartupActivity.this); StartupActivity.this.mediaPla ...
- 终端更新ubuntu系统
1.sudo apt-get update 2. sudo apt-get dist-upgrade
- 学习笔记:MySQL操作初步
对数据库的操作:SQL语言 一:SQL:Structured Query Language,结构化查询语言! 二:DDL:Data Definition Language,数据定义语言 三:DML:D ...
- html与Android——webView
1 <html> 2 <head> 3 <title>myHtml.html</title> 4 5 <meta http-equiv=" ...
- jquery checkbox 实现单选
最近在用javascript的时候发现网上实现checkbox单选的代码都已经过时了. 用着几年前的代码发现根本不行了 原因是jquery api已经更改 http://api.jquery.com/ ...