考研机试——比较奇偶个数

题库链接:https://blog.csdn.net/weixin_43905586/article/details/112438701

测试链接:https://www.nowcoder.com/practice/188472f474d5421cb8218b8ad561023b?tpId=67&tqId=29636&tPage=1&ru=/kaoyan/retest/1005&qru=/ta/bupt-kaoyan/question-ranking

用时:5分钟


分享名言一句

  • One day, when you look back on your journey, you will find that the years of constant struggle were the most beautiful life.——Freud

  • 有一天,当回顾自己走过的路时,你会发现这些奋斗不息的岁月,才是最美好的人生。——弗洛伊德


时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 64M,其他语言128M 

本题知识点: 数学 模拟

 算法知识视频讲解

题目描述

第一行输入一个数,为n,第二行输入n个数,这n个数中,如果偶数比奇数多,输出NO,否则输出YES。

输入描述:

输入有多组数据。
每组输入n,然后输入n个整数(1<=n<=1000)。

输出描述:

如果偶数比奇数多,输出NO,否则输出YES。

示例1

输入

5
1 5 2 4 3

输出

YES

鄙人拙见

  1. 直接对奇数或偶数计数
  2. 用总数减去即可得另一种数的个数
  3. 比较俩个数的大小,输出即可

C++参考代码1.0:

#include <iostream>
#include<bits/stdc++.h>
using namespace std;

int main()
{
    int n;
    scanf("%d",&n);
    int num[n];
    for(int i = 0; i < n; i++)
    {
        scanf("%d",&num[i]);
    }
    int odd_count = 0;
    for(int i = 0; i < n; i++)
    {
        if(num[i] % 2 != 0)
        {
            odd_count++;
        }
    }
    int even_count;
    even_count = n - odd_count;
    if(even_count > odd_count)
    {
        printf("NO");
    }
    else
    {
        printf("YES");
    }
    return 0;
}

尾言:作者水平有限,读者思维无限,若有细节错误请见谅,若有好的想法,欢迎评论区留言。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值