zoukankan      html  css  js  c++  java
  • 求奇数和偶数的数量

    题目

    输入数字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("
    ");
    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
    ", cnt, n - cnt);
    31         printf("Input a number:");
    32     }
    33 }
  • 相关阅读:
    JAVA日常之三
    java将字符串存入oracle的Blob中
    java连接oracle数据库
    JAVA日常之二
    JAVA日常之一
    linux日常命令之三
    linux日常命令之二
    linux日常命令之一
    Python之路【第四十篇】:django日更
    Python之路【第三十九篇】:django日更
  • 原文地址:https://www.cnblogs.com/ydqblogs/p/13841679.html
Copyright © 2011-2022 走看看