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 ...
随机推荐
- mysql 几个命令
show databases; 显示所有数据库 create database 数据库名; 创建数据库 drop database 数据库名; 删除数据库
- JS 数组迭代方法
var arr = [3,4,5,6,7,"a"]; var isNum = function(elem,index,AAA){return !isNaN(elem);} var ...
- 在虚拟机中配置FastDFS+Nginx模块
先上部署图 提示一下, ip 192.168.72.138 上面部署了两个group, 分别为 group1和g2. 另外, 同组之内的 port 要保持一致. 一.安装准备 1. #每台机器都添加两 ...
- 关于oracle数据库中进行查询的时候出现效率特别差的一种情况
下面这段代码执行效率是极慢的(根本没执行完成过): select /*+ parallel(16) */ z.small_code,trunc(hs.CREATED_AT),sum(hs.COST_S ...
- 初学软件测试之——如何使用Junit4
前几天刚刚接触软件测试,这篇文章主要是介绍使用eclipse的插件——Junit4进行单元测试.下面先介绍一下单元测试的定义:单元测试(unit testing),是指对软件中的最小可测试单元进行检查 ...
- C语言函数的声明以及函数原型
C语言代码由上到下依次执行,原则上函数定义要出现在函数调用之前,否则就会报错.但在实际开发中,经常会在函数定义之前使用它们,这个时候就需要提前声明.所谓声明(Declaration),就是告诉编译器我 ...
- Good Bye 2015 D. New Year and Ancient Prophecy
D. New Year and Ancient Prophecy time limit per test 2.5 seconds memory limit per test 512 megabytes ...
- WebService错误:使用 XSL 样式表无法查看 XML 输入
在浏览器中输入URL: 'http://localhost/test.aspx' 出现下面错误提示信息: 无法显示 XML 页. 使用 XSL 样式表无法查看 XML 输入.请更正错误然后单击 刷新 ...
- cmd常用命令符
想成为电脑高手必须掌握的八个cmd 命令 http://www.cr173.com/html/3917_1.html ping命令的其他技巧:在一般情况下还可以通过ping对方让对方返回给你的TTL值 ...
- How to convert webp to png/jpg/gif in MacOS
Environment I'm using OS X 10.11.4 and have homebrew 1.0.5 installed. Introduction I recently downlo ...