请编写函数void fun(int x,int pp[],int *n),它的功能是:求出能被x所整除且不是偶数的各整数, 并按从小到大的顺序放在pp所指的数组中,这些除数的个数通过形参n返回

/*请编写函数void fun(int x,int pp[],int n),它的功能是:求出能被x所整除且不是偶数的各整数,
并按从小到大的顺序放在pp所指的数组中,这些除数的个数通过形参n返回。
/

#include <stdio.h>
void fun(int x, int pp[], int *n) {
    int count = 0;
    for (int i = 1; i <= 100; i++) {
        if (i % x == 0 && i % 2 != 0) {
            pp[count] = i;
            count++;
        }
    }

    for (int i = 0; i < count-1; i++) {
        for (int j = 0; j < count-i-1; j++) {
            if (pp[j] > pp[j+1]) {
                int temp = pp[j];
                pp[j] = pp[j+1];
                pp[j+1] = temp;
            }
        }
    }
    *n = count;
}

int main() {
    int arr[100];
    int n;
    int x =0;
    printf("please input one integer\n");
    scanf("%d",&x);
    fun(x, arr, &n);
    printf("Numbers divisible by %d and not even: \n", x);
    for (int i = 0; i < n; i++) {
        printf("%d ", arr[i]);
    }
    printf("\n");
    printf("Number of elements: %d\n", n);

    return 0;
}

posted on 2024-06-09 21:55  wessf  阅读(19)  评论(0编辑  收藏  举报