2012届华为校园招聘上机考试题目(9月6日下午1点场)第二题及代码

菜鸟~自己写的~仅供参考~高手无视之

/

// 左右放置.cpp : 定义控制台应用程序的入口点。
//
/*给定一个数组input[] ,如果数组长度n为奇数,
则将数组中最大的元素放到 output[] 数组最中间的位置,
如果数组长度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[])*/


#include "stdafx.h"
#include <stdio.h> 
#include <iostream>
#include <stdlib.h>
#include<windows.h>
using namespace std;
void sort(int input[],int output[]);


int _tmain(int argc, _TCHAR* argv[])

    int input[] = {3, 6, 1, 9, 7,};
int wc=0;
for(wc=0;wc<5;wc++)//这个循环砸不起作用呢
{
cout<<input[wc]<<endl;
//cout<<"\n"<<endl;
}
   int output[]= {3, 6, 1, 9, 7,};
   sort(input,output);
   system("pause");
return 0;
}


void sort(int input[5],int output[5])
{
int m=5;
//cout<<m<<endl;


    int i,j,temp;
     bool exchange;//交换标志
     for(i=0;i<m-1;i++){ //最多做n-1趟排序
       exchange=FALSE; //本趟排序开始前,交换标志应为假
       for(j=m-2;j>=i;j--) //对当前无序区R[i..n]自下向上扫描
        if(input[j+1]<input[j])
{//交换记录
          temp=input[j+1]; //R[0]不是哨兵,仅做暂存单元
          input[j+1]=input[j];
          input[j]=temp;
          exchange=TRUE; //发生了交换,故将交换标志置为真
         }
       if(!exchange) //本趟排序未发生交换,提前终止算法
             break;
  //cout<<input[5]<<endl;


     } 
for(int wc1=0;wc1<5;wc1++)//只是来显示排序结果~
{
cout<<input[wc1]<<endl;
}
 
int q=m-1;
if((m%2)==0)
{
int mid=m/2;
for (int tempmid=0;tempmid<=mid;tempmid++)//注意循环语句的执行顺序
{
output[mid+tempmid]=input[q];
q--;
output[mid+tempmid]=input[q];
q--;
}
}
if((m%2)!=0)//注意循环语句的执行顺序
{
int mid=q/2;
output[mid]=input[q];
for (int tempmid=1;tempmid<=mid;tempmid++)
{
q--;
output[mid-tempmid]=input[q];
q--;
output[mid+tempmid]=input[q];
 
}
}
for(int wc=0;wc<5;wc++)
{
cout<<output[wc]<<endl;
}


}

以下为运行结果



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kobesdu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值