PAT基础编程题目-6-12 判断奇偶性
PAT基础编程题目-6-12 判断奇偶性
题目详情
解答
C语言版
#include <stdio.h>
int even(int n);
int main()
{
int n;
scanf("%d", &n);
if (even(n))
printf("%d is even.\n", n);
else
printf("%d is odd.\n", n);
return 0;
}
int even(int n)
{
return n % 2 == 0 ? 1 : 0; // 偶数返回1,奇数返回0
}
C++版
#include<iostream>
int even(int n);
int main()
{
int n;
std::cin >> n;
if (even(n))
std::cout << n << " is even.\n";
else
std::cout << n << " is odd.\n";
return 0;
}
int even(int n)
{
return n % 2 == 0 ? 1 : 0;
}
Java版
public class Main{
private static int even(int n) {
return n % 2 == 0 ? 1 : 0;
}
public static void main(String[] args) {
int n = 0;
Scanner scanner = new Scanner(System.in);
if(scanner.hasNext()) {
n = scanner.nextInt();
}
scanner.close();
if (even(n) == 1)
System.out.println(n+" is even.");
else
System.out.println(n+" is odd.");
}
}
创作不易,喜欢的话加个关注点个赞,谢谢谢谢谢谢!