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 ...
随机推荐
- JSP中动态INCLUDE与静态INCLUDE的区别
动态INCLUDE 用法: <jsp:include page="included.jsp" flush="true" /> 说明: 它总是会检查所 ...
- Windows Phone 六、JSON序列化
JSON序列化 public class Person { public int Id { get; set; } public string Name { get; set; } public in ...
- Adobe AIR对本地文件(XML文件)的操作
引用:http://addiwang.blog.163.com/blog/static/118130772011221114230288/ Air的文件操做主要涉及两个类,FIle和FileStrea ...
- linux-bash shell学习
什么是shell?shell就相当于是计算机给我提供的一个操作系统的接口,这里说的bash shell是一种命令行方面的软件,提供给用户来操作系统.
- Qt之添加QLabel的点击事件
QLabel功能为显示了一个字符串或者图片等信息,它本身没有click信号.也就不能够响应click点击事件,有什么办法来实现来,我们可以子类化QLabel,实现MouseXXXEvent.class ...
- MySQL命令大全:MySQL常用命令手册、MySQL命令行大全、查询工具
1.连接Mysql 格式: mysql -h主机地址 -u用户名 -p用户密码 1.连接到本机上的MYSQL.首先打开DOS窗口,然后进入目录mysql\bin,再键入命令mysql -u root ...
- Objective-C基础2
16.继承 1)不能和父类定义一样的变量 2)单一继承,不支持继承多个类 3)支持多层继承子类也可以被继承 17.实例变量修饰符 public:公开的,在其他类中也可以访问 protected:受保护 ...
- YbSoftwareFactory 代码生成插件【十六】:Web 下灵活、强大的审批流程实现(含流程控制组件、流程设计器和表单设计器)
程序=数据结构+算法,而企业级的软件=数据+流程,流程往往千差万别,客户自身有时都搞不清楚,随时变化的情况更是家常便饭,抛开功能等不谈,需求变化很大程度上就是流程的变化,流程的变化会给开发工作造成很大 ...
- bzoj 1162 network
树上的区间第k小数,以前写的主席树是一直MLE的,后来看到一种在初始化的时候的优化:直接DFS这颗树,得到每个点的主席树,然后更新的时候另外对DFS序建主席树,答案加上初始每个点的主席树,这样在初始化 ...
- 2015最新德淘W家(Windeln.de)新人优惠码wcode0520,赠1000积分,可抵10欧元
德淘W家(Windeln.de)网址:www.windeln.de 德淘W家(Windeln.de)的新人优惠码wcode0520 第一次购物结账时输入wcode0520,提交订单,1000积分划入你 ...