iOS第4天數組排序

來源:互聯網
上載者:User

標籤:

#import <Foundation/Foundation.h>int main(int argc, const char * argv[]) {    //建立5個整型變數    //對器 ctrl + i//    int a = 1;//    int b = 2;//    int c = 3;//    int d = 4;//    int f = 5;//    //    int n1 = 0;//    int n2 = 0;//    int n3 = 0;//    int n4 = 0;//    int n5 = 0;//    int n6 = 0;//    int n7 = 0;//    int n8 = 0;//    int n9 = 0;//    int n10 = 0;    //數組    /*     本質: 快速定義多個相同資料類型的變數     定義: 資料類型 數組名[數組元素個數] = {值1, 值2, ..., 值n};     移動:cmd alt + []     數組的確定:定義出來的是相同資料類型的變數     //注意1:最後一個數組元素的下標是:數組元素的個數 -1     //注意2:c語言,不會檢測數組下標越界     */    //    short array1[10] = {1,2,3,5};    //    char array2[10] = {‘a‘,‘b‘};    //    int c[5] = {0};    //    int  d[] = {9, 5, 2, 7};//在沒有給定數組元素個數的時候,可以按照該模式,此時一定要寫輕搓所有的初值    //    //建立 float ,short,char 類型的數組,元素各十個//    int array[10] = {1,2,3,4,5,6,7,8,9,10};//    for (int i = 0 ; i < 10 ;i++)//    {//        printf("array[%d] = %d",  i, array[i]);//    }    //定義20個元素    /*    int a[10] = {0};    int b[10] = {0};    int c[10] = {0};    for (int i = 0 ; i < 10; i++) {        a[i] = arc4random() % (40 - 20 + 1) + 20;                printf("a%d \n",a[i]);      }    for (int i = 0; i < 10; i++) {        b[i] = a[i];        printf("b = %d \n", b[i]);    }    for (int i = 0; i < 10 ; i++) {        c[i] = a[i] + b[i];    }     *///    int a[10] = {0};//    int max = 0;//    for (int i = 0; i < 10  ; i++) {//        a[i] = arc4random() % (40 - 20 +1) + 20;//        //        //        //        printf("%d ",a[i]);//        max = max > a[i] ? max : a[i];//    }//    printf("最大值是%d",max);            //冒泡排序    /*    int a[5] = {38,24,18,29,10};    //5個元素第1趟比較了4次,第2趟比較了3次,第3趟比較了2次,第4次比較1次        int tem = 0;    //這裡的i<4不是5防止數組元素越界    //這裡是走了一趟,找到了最大值    //外層迴圈控制比較的趟數    //外層迴圈 -1可減可不減,-1是為了提高程式的效率   for (int j = 0; j < 5 - 1; j++) {    //內層迴圈控制比較的次數    //內層迴圈,-1必須減,目的:防止數組下標越界    //內層迴圈,-j可減可不減,-j是為了提高效率        for (int i = 0; i < 5 - 1 -j ; i++) {            if (a[i] > a[i+1]) {                //如果前面的大於後面的交換                tem = a[i];                a[i] = a[i+1];                a[i+1] = tem;            }        }    }    for (int i = 0; i < 5; i++) {        printf("%d ",a[i]);    }     */    /*    //啟動並執行快速鍵cmd+R;    //定義十個元素的整型數組,[10, 30]隨機數,升序排列,然後輸出    int  a[10] = {0};    int temp = 0;    for (int i = 0; i < 10; i++) {        a[i] = arc4random() % (30 - 10 +1) + 10;    }//    for (int i = 0; i < 10 - 1;  i++) {//        for (int j = 0; j < 10 - 1 - i;  j++) {//            if (a[j] > a[j+1]) {//                temp = a[j];//                a[j] = a[j+1];//                a[j+1] = temp;//            }//        }//    }    for (int i = 0; i < 10 - 1; i++) {        for (int j = 0; j < 10 - 1 - i; j++) {            if (a[j] < a[j + 1]) {                temp = a[j];                a[j] = a[j + 1];                a[j + 1] = temp;            }        }    }    for (int i = 0; i < 10 ; i++) {        printf("%d ",a[i]);    }     */    //字元數組    //定義數組    /*    char charArray[7] = {‘n‘, ‘s‘, ‘d‘, ‘b‘, ‘b‘, ‘b‘, ‘s‘};    char str[] = "iPhone";//字串,末尾存在隱含字元\0    printf("%s \n",str);    //訪問單個    printf("%c \n", charArray[0]);    for (int i = 0; i < 7; i++) {        printf("%c ",charArray[i]);    }      */    //字串    //字串是字元數組,但是字元數組不一定是字串.    //%s 從字串第一個字元開始輸出,直到遇到 \0 結束輸出    //srrlen 求的是字串看到的長度    //1.計算字串長度的函數    char string1[] = "iPad";     int length = (int)strlen(string1);    printf("%d \n", length);    //2.字串拷貝函數: strcpy(), 將後一個字串的內容,拷貝給前一個字串.    //注意:後一個字串的長度,不能超過前一個字串    char string2[] = "iPod";    // char  string3[]  = "iPone";    strcpy(string1, string2);    printf("%s\n",string1);    printf("%s\n",string2);    //3.字串的拼接函數: strcat(),將後一個字串拼接到前一個字串的後面(從前一個字串的\0位置開始拼接)    strcat(string2, string2);    printf("%s\n",string2);    //4.字串比較函數: strcmp(),前一個字串,與後一個字串,逐個進行,直到尋找到不相等的字元結束;返回的是,前一個字串與後一個字串不相等字串之間的差值(前一個字串中的字元,減去後一個字串中的字元    int result = strcmp(string1 , string2);    printf("%d \n", result);    return 0;}

 

iOS第4天數組排序

聯繫我們

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