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 ...
随机推荐
- Leetcode: Sort Transformed Array
Given a sorted array of integers nums and integer values a, b and c. Apply a function of the form f( ...
- 记得初学JS时候练个九九乘法表都写的要死要活
还记得当初刚接触JS时候,看到视频中老师写了个九九乘法表,觉得好神奇,可是自己在下面动手写了半天还是有各种问题,甚是懊恼啊.今又看到园子里有关于乘法表的博文,出于对过去的不舍与缅怀,遂重写一遍. &l ...
- Java Timer定时器时,每次重复执行了两次任务的解决方案
web.xml监听配置 com.numenzq.mc.service.impl.TimerListener TimerListener类 public class TimerListener impl ...
- SQL中exists和in比较
in 和exists in是把外表和内表作hash 连接,而exists 是对外表作loop 循环,每次loop 循环再对内表进行查询. 一直以来认为exists 比in 效率高的说法是不准确的.如果 ...
- 老贼博客php教程从零学习PHP开始写作,顺祝新同事快乐!
随笔是不是这样写的,好似是吧! 老贼博客php教程从零学习PHP开始写作,顺祝新同事快乐! 谢谢支持,点赞!
- WEB进度条控件
近段时间为了工作的需要学习了一下写自定义控件,呵呵!以前没写过,近段时间才开始研究的,昨天写了一个WEB状态条控件,可以设置进度条的百分比,也可以设置它的总数与当前的数量来自动计算百分比,可以设置颜色 ...
- C#抽象方法和虚拟方法理解
http://www.cnblogs.com/denylau/archive/2010/04/11/1709573.html http://www.cnblogs.com/naariah/archiv ...
- openlayers 3监听地图分辨率变化事件
map.getView().on('change:resolution',checkZoom);//checkZoom为调用的函数 function checkZoom() { // alert(&q ...
- 查找树ADT
通过二叉查找树实现排序的例程 /** * 无论排序的对象是什么,都要实现Comparable接口 * * @param <T> */ public class BinaryNode< ...
- Linux中tcpdump的编译和使用
tcpdump官网:http://www.tcpdump.org/ 转载于:http://www.cnblogs.com/hzl6255/p/6147985.html 目录 1. 介绍 2. 编译 2 ...