给定一个浮点数,请检查它是偶数还是奇数。
我们可以通过将整数的最后一位除以2来检查整数是偶数还是奇数,但是在浮点数的情况下,我们不能仅通过将其最后一位除以2来检查给定的数字是偶数还是奇数。例如,100.70是一个奇数,但最后一位可被2整除。
例子 :
Input : 100.7
Output : odd
Input : 98.8
Output : even
Input : 100.70
Output : odd
Trailing 0s after dot do not matter.
方法 :
- 我们开始从其最低有效位开始遍历一个数字,直到得到一个非零数字或“。”。
- 如果数字可被2整除,则偶数为奇数
- 如果是 ‘。’比这意味着要遍历数字的小数部分,现在我们可以通过将数字除以2来确定它是0还是非零数字,以检查数字是偶数还是奇数。
下面是上述方法的实现:
C++
// CPP program to check whether given floating
// point number is even or odd
#include
using namespace std;
// Function to check even or odd.
bool isEven(string s)
{
int l = s.length();
// Loop to traverse number from LSB
bool dotSeen = false;
for (int i = l - 1; i >= 0; i--) {
// We ignore trailing 0s after dot
if (s[i] == '0' && dotSeen == false)
continue;
// If it is '.' we will check next digit and it
// means decimal part is traversed.
if (s[i] == '.') {
dotSeen = true;
continue;
}
// If digit is divisible by 2
// means even number.
if ((s[i] - '0') % 2 == 0)
return true;
return false;
}
}
// Driver Function
int main()
{
string s = "100.70";
if (isEven(s))
cout << "Even";
else
cout << "Odd";
return 0;
}
Java
// Java program to check whether given floating
// point number is even or odd
import java.util.*;
import java.lang.*;
public class GfG {
// Function to check even or odd.
public static boolean isEven(String s1)
{
int l = s1.length();
char[] s = s1.toCharArray();
// Loop to traverse number from LSB
boolean dotSeen = false;
for (int i = l - 1; i >= 0; i--) {
// We ignore trailing 0s after dot
if (s[i] == '0' && dotSeen == false)
continue;
// If it is '.' we will check next digit and it
// means decimal part is traversed.
if (s[i] == '.') {
dotSeen = true;
continue;
}
// If digit is divisible by 2
// means even number.
if ((s[i] - '0') % 2 == 0)
return true;
return false;
}
return false;
}
// Driver function
public static void main(String argc[])
{
String s = "100.70";
if (isEven(s))
System.out.println("Even");
else
System.out.println("Odd");
}
}
/* This code is contributed by Sagar Shukla */
Python3
# Python 3 program to check
# whether given floating
# point number is even or odd
# Function to check
# even or odd.
def isEven(s) :
l = len(s)
# Loop to traverse
# number from LSB
dotSeen = False
for i in range(l - 1, -1, -1 ) :
# We ignore trailing
# 0s after dot
if (s[i] == '0'
and dotSeen == False) :
continue
# If it is '.' we will
# check next digit and it
# means decimal part is
# traversed.
if (s[i] == '.') :
dotSeen = True
continue
# If digit is
# divisible by 2
# means even number.
if ((int)(s[i]) % 2 == 0) :
return True
return False
# Driver Function
s = "100.70"
if (isEven(s)) :
print("Even")
else :
print("Odd")
# This code is contributed
# by Nikita Tiwari.
C#
// C# program to check whether given floating
// point number is even or odd
using System;
public class GfG {
// Function to check even or odd.
public static bool isEven(string s1)
{
int l = s1.Length;
// char[] s = s1.toCharArray();
// Loop to traverse number from LSB
bool dotSeen = false;
for (int i = l - 1; i >= 0; i--) {
// We ignore trailing 0s after dot
if (s1[i] == '0' && dotSeen == false)
continue;
// If it is '.' we will check next
// digit and it means decimal part
// is traversed.
if (s1[i] == '.') {
dotSeen = true;
continue;
}
// If digit is divisible by 2
// means even number.
if ((s1[i] - '0') % 2 == 0)
return true;
return false;
}
return false;
}
// Driver function
public static void Main()
{
string s1 = "100.70";
if (isEven(s1))
Console.WriteLine("Even");
else
Console.WriteLine("Odd");
}
}
/* This code is contributed by Vt_m */
PHP
= 0; $i--)
{
// We ignore trailing
// 0s after dot
if ($s[$i] == '0' && $dotSeen == false)
continue;
// If it is '.' we will
// check next digit and it
// means decimal part is
// traversed.
if ($s[$i] == '.')
{
$dotSeen = true;
continue;
}
// If digit is divisible by 2
// means even number.
if (($s[$i] - '0') % 2 == 0)
return true;
return false;
}
}
// Driver Code
$s = "100.70";
if (isEven($s))
echo "Even";
else
echo "Odd";
// This code is contributed by aj_36
?>
Javascript
输出 :
odd
时间复杂度: O(| s |)
辅助空间: O(1)