博客 / 詳情

返回

[C語言]指針保存輸入的9個數中所有的奇數並輸出

1、題目

編寫一個函數,利用指針實現保存輸入的9個數中所有的奇數並輸出,若沒有奇數則輸出NO。

要求:

  • 1、不在子函數中輸出。
  • 2、不能修改指定的子函數void GetOdd(int a[], int b, int m)

示例:

輸入:1 4 7 2 5 8 3 6 9
輸出:1 7 5 3 9

輸入:2 2 2 2 2 2 2 2 2
輸出:NO

2、完整代碼

#include<stdio.h>
#define N 10

int a[N];
int res[N];

void GetOdd(int a[], int* b, int* m)
{
    *b = 0;
    for (int i = i = 0; i < 9; ++i)
    {
        if (a[i] % 2 == 1)
        {
            m[*b] = a[i];
            *b = *b + 1;
        }
    }
}
int main()
{
    int cnt;
    for (int i = 0; i < 9; ++i)
        scanf_s("%d", &a[i]);
    GetOdd(a, &cnt, res);
    if (cnt == 0)
        printf("NO");
    else
        for (int i = 0; i < cnt; ++i)
            printf("%d ", res[i]);
    return 0;
}

3、截圖

在這裏插入圖片描述

在這裏插入圖片描述

user avatar nixidezuqiu 頭像 graycat 頭像
2 位用戶收藏了這個故事!

發佈 評論

Some HTML is okay.