數組、二維數組、函數

1、數組(續)

遍歷:逐個操作數組中的所有元素
#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        int a[10] = {1,2,3,4,5,6,7,8,9,0};
        //正向遍歷
        for (int i = 0; i < 10; i++)
        {
            if (i % 3 == 0)
                printf("%d ", a[i]);
        }
        printf("\n");
        //反向遍歷
        for (int i = 9; i>= 0; i--)
        {
            printf("%d ", a[i]);
        }
        printf("\n");

        //從鍵盤輸入一個整數,如12325,判斷這個整數是否有重複的數字
        int x;
        NSLog(@"請輸入一個整數:");
        scanf("%d", &x);

        BOOL bs[10] = {NO};
        do
        {
            int n = x % 10;
            if (bs[n] == YES)
            {
                NSLog(@"數字%d重複了", n);
                break;
            }
            bs[n] = YES;
            x /= 10;
        }while (x);
        if (x == 0)
        {
            NSLog(@"沒有重複的數字");
        }
    }
    return 0;
}
判斷一個整數是否有重複數字
數組亂序
#import <Foundation/Foundation.h>

int main()
{
    @autoreleasepool {
        int a[10] = {0,1,2,3,4,5,6,7,8,9};
        for (int i = 0; i < 10; i++)
        {
            int x = rand() % (10 - i - 1) + i + 1;
            int temp = a[i];
            a[i] = a[x];
            a[x] = temp;
        }
        for (int i = 0; i < 10; i++)
        {
            printf("%d ", a[i]);
        }
        printf("\n");
    }
    return 0;
}
數組元素重複
#import <Foundation/Foundation.h>

int main()
{
    @autoreleasepool {
        int a[10] = {1,2,3,4,5,6,7,8,9,0};
        BOOL flag = NO;
        int i = 0;
        for (; i < 10; i++)
        {
            int j = i + 1;
            for (; j < 10; j++)
            {
                if (a[i] == a[j])
                {
                    flag = YES;
                    NSLog(@"第%d個元素與第%d個元素重複了", i + 1, j + 1);
                    //break;
                }
            }
//            if (j < 10)
//            {
//                break;
//            }
        }
        if (flag == NO)
        {
            NSLog(@"沒有重複的元素");
        }
    }
    return 0;
}
雙色球
#import <Foundation/Foundation.h>

int main()
{
    @autoreleasepool {
        srand((unsigned)time(0));

        int blue = rand() % 16 + 1;
        int red[6];
        for (int i = 0; i < 6; i++)
        {
            red[i] = rand() % 33 + 1;
            for (int j = 0; j < i; j++)
            {
                if (red[i] == red[j])
                {
                    i--;
                    break;
                }
            }
        }
        printf("藍:%d\n", blue);
        printf("紅:");
        for (int i = 0; i < 6; i++)
        {
            printf("%d ", red[i]);
        }
        printf("\n");
    }
    return 0;
}

2、二維數組

二維數組是多個一維數組的集合
        int a[3][4];
        a[0][1] = 10;
        a[1][2] = 20;
        a[2][3] = 30;
二維數組的初始化
#import <Foundation/Foundation.h>

int main()
{
    @autoreleasepool {
        int a[3][3] = {{1,2,3},{4,5,6},{7,8,9}};
        for (int i = 0; i < 3; i++)
        {
            for (int j = 0; j < 3; j++)
            {
                printf("%d ", a[i][j]);
            }
            printf("\n");
        }

        int b[][3] = {{1,2,3},{4,5,6},{7,8,9}};

        printf("------\n");
        int c[4][3] = {{1,2,3},{4,5},{6},{7,8,9}};
        for (int i = 0; i < 4; i++)
        {
            for (int j = 0; j < 3; j++)
            {
                printf("%d ", c[i][j]);
            }
            printf("\n");
        }

        int d[2][3] = {0};

        printf("------\n");
        int e[3][4] = {1,2,3,4,5,6,7,8,9};
        for (int i = 0; i < 3; i++)
        {
            for (int j = 0; j < 4; j++)
            {
                printf("%d ", e[i][j]);
            }
            printf("\n");
        }
    }
    return 0;
}
二維數組的數組名
#import <Foundation/Foundation.h>

int main()
{
    @autoreleasepool {
        int a[3][4];
        NSLog(@"%p", a);
        NSLog(@"%p", &a[0][0]);
        NSLog(@"%p", a[0]);

        NSLog(@"%p", a[1]);
        NSLog(@"%p", &a[1][0]);

        NSLog(@"%p", a[2]);
        NSLog(@"%p", &a[2][0]);
    }
    return 0;
}
二維數組與二維表
#import <Foundation/Foundation.h>

int main()
{
    @autoreleasepool {
        /*-----------
         1      1       1
         2      4       8
         3      9       27
         4      16      64
         ...    ...     ...
         n      n^2     n^3
         ------------*/
        int n;
        NSLog(@"請輸入表的行數");
        scanf("%d", &n);

        int table[n][3];
        for (int i = 0; i < n; i++)
        {
            table[i][0] = i;
            table[i][1] = i * i;
            table[i][2] = i * i * i;
        }

        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < 3; j++)
            {
                printf("%d\t", table[i][j]);
            }
            printf("\n");
        }

        int x;
        NSLog(@"請輸入你要查詢的數字:");
        scanf("%d", &x);

        NSLog(@"%d的平方是:%d,立方是:%d", x, table[x][1], table[x][2]);
    }
    return 0;
}

3、函數

能完成一定功能的代碼塊
語法
    函數頭:返回值類型、函數名、參數
    函數體
函數的聲明
函數的調用(實參有四種形式)
#import <Foundation/Foundation.h>

//寫一個函數,判斷參數x和y是否都落在0~n的區間內
BOOL check1(int x, int y, int n)
{
    return x >= 0 && x <= n && y >= 0 && y <= n;
}

//寫一個函數,判斷一個整數是否爲素數
BOOL isPrimer(int x)
{
    if (x < 2)
    {
        return NO;
    }
    for (int i = 2; i < x; i++)
    {
        if (x % i == 0)
        {
            return NO;
        }
    }
    return YES;
}

int main()
{
    @autoreleasepool {
        BOOL in = check1(3, 5, 4);
        NSLog(@"%d", in);

        NSLog(@"%d", isPrimer(17));
        NSLog(@"%d", isPrimer(10));

        for (int i = 0; i < 100; i++)
        {
            if (isPrimer(i))
            {
                printf("%d ", i);
            }
        }
        printf("\n");
    }
    return 0;
}

思考練習:

有一個班,有n個學生,成績單如下
No. C OC Foundation UIKit Total
0 60 70 80 90 300
1 90 85 70 60 305
2 95 80 65 45 285

總計 245 235 215 195
要求:首先隨機生成每個學生的各科成績,然後計算每個學生的總分,最後計算每門課(總計)的總分
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
解析:

#import <Foundation/Foundation.h>

int main()
{
    @autoreleasepool {
        int number;
        NSLog(@"請輸入學生的人數:");
        scanf("%d", &number);

        int scores[number][6];
        for (int i = 0; i < number; i++)
        {
            scores[i][0] = i;//學號
            int sum = 0;
            for (int j = 1; j <= 4; j++)
            {
                scores[i][j] = rand() % 101;//4科成績
                sum += scores[i][j];
            }
            scores[i][5] = sum;
        }

        printf("---------------------\n");
        printf("No.\tC\tOC\tFou\tUI\tTotal\n");
        printf("---------------------\n");
        for (int i = 0; i < number; i++)
        {
            for (int j = 0; j < 6; j++)
            {
                printf("%d\t", scores[i][j]);
            }
            printf("\n");
        }
        printf("---------------------\n");
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章