華為的一道題

來源:互聯網
上載者:User

看了別人的解法沒有看懂,自己寫的

int A[nSize],其中隱藏著若干0,其餘非0整數,寫一個函數int Func(int* A, int nSize),使A把0移至後面,非0整數移至 
數組前面並保持有序,傳回值為原資料中第一個元素為0的下標。(儘可能不使用輔助空間且考慮效率及異常問題,注釋規範且給出設計思路)

// 0再前非零在後.cpp : Defines the entry point for the console application.//#include "stdafx.h"void swap(int *a, int *b){int temp;temp=*a;*a=*b;*b=temp;}void fun1(int a[], int n){int i;int j;int first=0;for (i=0;i<n;i++){for (j=i+1;j<n;j++){if (a[i]==0&&a[j]!=0){first++;if (first==1){printf("第一個不為零的下標是:%d\n",j);}swap(&a[i], &a[j]);}}}}int main(int argc, char* argv[]){int a[7]={0,0,2,0,1,5,3};fun1(a,7);for (int i=0;i<6;i++){printf("%d ", a[i]);}return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.