求奇数和偶数的数量

题目

输入数字n,表示随机生成n个正整型数,然后打印出这n个数字和n个数里面奇数和偶数的数量。

输入:

   5

输出:

        Input a number:5

   2 38 3 39 65

  odd num: 3, event num: 2

 1 /*************************************************************************
 2     > File Name: judge_number.c
 3     > Author: yudongqun
 4     > Mail: qq2841015@163.com
 5     > Created Time: Mon 19 Oct 2020 05:35:25 PM CST
 6  ************************************************************************/
 7 
 8 #include <stdio.h>
 9 #include <stdlib.h>
10 #include <time.h>
11 
12 int get_odd_num(int n) {
13     int cnt = 0;
14     srand(time(0));
15     for (int i = 0; i < n; i++) {
16         int val = rand() % 100;
17         cnt += (val & 1);
18         i && printf(" ");
19         printf("%d", val);
20     }
21     printf("\n");
22     return cnt;
23 }
24 
25 int main(void) {
26     int n = 0, cnt = 0;
27     printf("Input a number:");
28     while (scanf("%d", &n) != EOF) {
29         cnt = get_odd_num(n);
30         printf("odd num: %d, event num: %d\n", cnt, n - cnt);
31         printf("Input a number:");
32     }
33 }

 

posted @ 2020-10-19 17:46  ydqun  阅读(364)  评论(0编辑  收藏  举报