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 ...
随机推荐
- linux--------------今天又遇到一个奇葩的问题,就是linux文件的权限已经是777了但是还是没有写入权限,按照下面的命令就解决了
查看SELinux状态: 1./usr/sbin/sestatus -v ##如果SELinux status参数为enabled即为开启状态 SELinux status: ...
- KEEPALIVED 双机自动切换部署备忘
1.配置文件的名字不要型错了.开始我将配置文件写成keeplive.conf,运行后也不报错,但无法看到VIP.日志里也看不到任何有价值信息.直到后来反复检查才发现可能配置文件名有问题,修正为keep ...
- android 多媒体数据库(非原创)
推荐文章:http://fzlihui.iteye.com/blog/1097952,http://www.cnblogs.com/pen-ink/archive/2011/06/02/2068410 ...
- ASCII码表(二进制 十进制 十六进制)
css里,允许使用转义字符\+ascii16进制形式; 例如: e的ascii 16进制是65,我们就写为\65 expression -> expr\65ssion 二进制 十进制 十六进制 ...
- .net之工作流工程展示及代码分享(三)数据存储引擎
数据存储引擎是本项目里比较有特色的模块. 特色一,使用接口来对应不同的数据库.数据库可以是Oracle.Sqlserver.MogoDB.甚至是XML文件.采用接口进行对应: public inter ...
- C# BackgroundWorker组件学习入门介绍
C# BackgroundWorker组件学习入门介绍 一个程序中需要进行大量的运算,并且需要在运算过程中支持用户一定的交互,为了获得更好的用户体验,使用BackgroundWorker来完成这一功能 ...
- WEB进度条控件
近段时间为了工作的需要学习了一下写自定义控件,呵呵!以前没写过,近段时间才开始研究的,昨天写了一个WEB状态条控件,可以设置进度条的百分比,也可以设置它的总数与当前的数量来自动计算百分比,可以设置颜色 ...
- SOSO街景地图 API (Javascript)开发教程(1)- 街景
SOSO街景地图 Javascript API 干什么用的? 你想在网页里嵌入个地图,就需要它了! 另外,它还支持:地点搜索.周边/附近查询.地图标注.公交/驾车路线规划.地理坐标与地址相互转换.地理 ...
- android 官方文档 JNI TIPS
文章地址 http://developer.android.com/training/articles/perf-jni.html JNI Tips JNI is the Java Native I ...
- 安装 whmcs
1.下载下来后(自己百度网盘),用scp 上传, 在网站能访问的地方新建目录,例如 whmcs 目录,解压到内 2.网站访问 http://ip/whmcs/index.php 提示需要安装 ionc ...