题解 | Even? Odd?-牛客假日团队赛13D题

Even? Odd?

https://ac.nowcoder.com/acm/contest/1082/D

题目描述

Bessie's cruel second grade teacher has assigned a list of N (1 <= N <= 100) positive integers I (1 <= I <= 106010^{60}1060) for which Bessie must determine their parity (explained in second grade as 'Even... or odd?'). Bessie is overwhelmed by the size of the list and by the size of the numbers. After all, she only learned to count recently.
Write a program to read in the N integers and print 'even' on a single line for even numbers and likewise 'odd' for odd numbers.
POINTS: 25

输入描述:

* Line 1: A single integer: N
* Lines 2..N+1: Line j+1 contains IjI_jIj, the j-th integer to determine even/odd

输出描述:

* Lines 1..N: Line j contains the word 'even' or 'odd', depending on the parity of

示例1

输入

1024 
5931 
输出
even
odd
说明
Two integers: 1024 and 5931
1024 is eminently divisible by 2; 5931 is not

解答

思路:此题是判断一个数是不是偶数,但是这个数比较大(不大于10的6次方),可以选择用字符串读取该数字,然后判断最后一个字符的奇偶性就可以了;
代码:
#include <bits/stdc++.h>
 
using namespace std;
 
int main()
{
    int n;
    char num[100];
    scanf("%d", &n);
    for(int i= 0; i < n; i++){
        scanf("%s", num);
        int len = strlen(num) - 1;
        if((num[len]- '0')% 2 == 0)
            printf("even\n");
        else
            printf("odd\n");
 
    }
    return 0;
}


来源:xiydang
全部评论

相关推荐

AAA不喝拿铁:校友好,开投就完事了!要准备面试的话更建议刷codetop,hot100有些题并不是面试常考题。另外想看刷题路线的可以看我的帖子,有讲怎么刷leetcode,除此之外可以看看我根据真实面经整理得到的最全(高/中/低频)面试题,加油
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务