【题目链接】
ybt 1041:奇偶数判断
OpenJudge NOI 1.4 03:奇偶数判断
【题目考点】
1. if…else语句
2. 三目运算符?:
3. 判断奇数偶数
- 数a如果是奇数,则有a除以2的余数为1,即
a % 2 == 1
。 - 数a如果是偶数,则有a除以2的余数为0,即
a % 2 == 0
。
【题解代码】
解法1:用if…else语句
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin>>n;
if(n % 2 == 0)
cout<<"even";
else
cout<<"odd";
return 0;
}
解法2:用三目运算符
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin>>n;
cout<<(n % 2 == 0 ? "even":"odd");
return 0;
}