華為招聘機試整理7:給定一個數組input[],華為input

來源:互聯網
上載者:User

華為招聘機試整理7:給定一個數組input[],華為input
華為招聘機試整理7:給定一個數組input[]

題目:給定一個數組intput[]
如果數組長度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}
題目分析:
該題主要考察數組之間資料的排列和數組與數組直接資料的傳送與接收。
演算法思路:
①首先將輸入的整型資料按升序排列 這裡沒有必要用冒泡法直接用#include<algorithm>中的sort
②找出中間位置middle,並且將最大的資料放進middle
③之後一個for迴圈,
1)當n為奇數時,我們可以直接以middle為中界限,迴圈去向左向右去尋找,i 從1->middle
由於左邊比右邊大,所以將大的放入左邊,次大的放入右邊。
2)當n為偶數時,我們以middle為中界限,這裡就要注意了,輸出資料中第一位元據無法迴圈到,所以我們i 只能從1->middle-1了,而且最後直接將輸入最小的數值放入輸出數組的第一位上面。
這裡需要注意一個小技巧就是,數組直接資料的傳輸,我們最好設定一個新的變數k。

===============================================================================
參考代碼:

//給定一個數組input[]//2014.7.10 hepanhui#include <iostream>#include <algorithm>const int maxn = 100;using namespace std;void sort(int input[], int n, int output[]){    int middle;    int k = 1;    sort(input,input + n);    //n為奇數時    if(n%2)    {        middle = (n-1)/2;        output[middle] = input[n-1];        for(int i = 1; i <= middle; i++)        {            output[middle - i] = input[n - 1 - k];            output[middle + i] = input[n - 2 - k];            k = k + 2;        }    }    else    {        middle = n/2;        output[middle] = input[n-1];        for(int i = 1; i < middle; i++)        {            output[middle - i] = input[n - 1 - k];            output[middle + i] = input[n - 2 - k];            k = k+2;        }        output[0] = input[0];    }}int main(){    int n;    cin >> n;    int input[maxn], output[maxn];    for(int i = 0 ;i < n; i++)    {        cin >> input[i];    }    sort(input, n, output);    for(int i = 0; i < n-1; i++)    {        cout << output[i] << ",";    }    cout << output[n-1] << endl;    return 0;}

調試中易犯錯誤的地方:
①我們這裡不需要#include<string>整型數組輸入的時候,我們一般都會告訴數組有多大,我們不能用strlen來測其大小,因為strlen是用來測量字串。
②for迴圈的時候要注意i是從1開始的,而不是0.因為我們中間最大的已經賦值了。
③數組間資料傳送,我們有時候採用假設一個新的變數,這樣會方便些。




給定一個二維數組a要將數組的內容逆置

//這個可以了
#include<stdio.h>
void input(int *a,int row,int col);
void transpose(int *p,int row);
void output(int *a,int row,int col);
void main()
{
int a[3][3];
input(a[0],3,3);
transpose(a[0],3);
output(a[0],3,3);
}
void input(int *a,int row,int col)
{
int i;
for(i=0;i<row*col;i++)
scanf("%d",a+i);//應該驅除*,這裡的a+i已經是地址了
}
void transpose(int *p,int row)
{
int i,j,t;
for(i=0;i<row;i++)
for(j=i;j<row;j++)
{ //你的這裡寫法重複返問了相同的元素,造成了兩次轉換,所以沒有變換
//只需要做上三角的轉換次數就可以了
t=*(p+3*i+j);
*(p+3*i+j)=*(p+3*j+i);
*(p+3*j+i)=t;
}
}
void output(int *a,int row,int col)
{
int i;
for(i=0;i<row*col;i++)
{
if(i%row==0) printf("\n");
printf("%6d",*(a+i));
}
printf("\n");
}
 
怎在給定的數組中尋找一個數

#include<stdio.h>
int main()
{
int n,a[20];
int i,j,m;
while (scanf("%d", &n) != EOF)
{
for(i=0; i<n; i++)
{
scanf("%d",&a[i]);
}
scanf("%d",&m);
for(j=0; j<n; j++)
{
if(a[j]==m)
{
printf("%d\n",j);
break;
}
}
if(j == n) printf("No\n");
}
return 0;
}
是多組測試的原因
注意這一行: while (scanf("%d", &n) != EOF)
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.