给定一个正数n,编写一个函数isMultipleof5(int n),如果n是5的倍数,则返回true,否则返回false。 您不允许使用%和/运算符 . 方法1(重复从n中减去5) 运行一个循环,当n大于0时,从循环中的n中减去5。循环终止后,检查n是否为0。如果n变成0,那么n是5的倍数,否则不是。
C++
#include <iostream> using namespace std; // assumes that n is a positive integer bool isMultipleof5 ( int n) { while ( n > 0 ) n = n - 5; if ( n == 0 ) return true ; return false ; } // Driver Code int main() { int n = 19; if ( isMultipleof5(n) == true ) cout << n << " is multiple of 5" ; else cout << n << " is not a multiple of 5" ; return 0; } // This code is contributed by SHUBHAMSINGH10 |
C
#include<stdio.h> // assumes that n is a positive integer bool isMultipleof5 ( int n) { while ( n > 0 ) n = n - 5; if ( n == 0 ) return true ; return false ; } // Driver Code int main() { int n = 19; if ( isMultipleof5(n) == true ) printf ( "%d is multiple of 5" , n); else printf ( "%d is not a multiple of 5" , n); return 0; } |
JAVA
// Java code to check if a number // is multiple of 5 without using // '/' and '%' operators class GFG { // assumes that n is a positive integer static boolean isMultipleof5 ( int n) { while (n > 0 ) n = n - 5 ; if (n == 0 ) return true ; return false ; } // Driver Code public static void main(String[] args) { int n = 19 ; if (isMultipleof5(n) == true ) System.out.printf( "%d is multiple of 5" , n); else System.out.printf( "%d is not a multiple of 5" , n); } } // This code is contributed by Smitha DInesh Semwal |
Python3
# Python code to check # if a number is multiple of # 5 without using / and % # Assumes that n is a positive integer def isMultipleof5(n): while ( n > 0 ): n = n - 5 if ( n = = 0 ): return 1 return 0 # Driver Code i = 19 if ( isMultipleof5(i) = = 1 ): print (i, "is multiple of 5" ) else : print (i, "is not a multiple of 5" ) # This code is contributed # by Sumit Sudhakar |
C#
// C# code to check if a number // is multiple of 5 without using / // and % operators using System; class GFG { // assumes that n is a positive integer static bool isMultipleof5 ( int n) { while (n > 0) n = n - 5; if (n == 0) return true ; return false ; } // Driver Code public static void Main() { int n = 19; if (isMultipleof5(n) == true ) Console.Write(n + " is multiple of 5" ); else Console.Write(n + " is not a multiple of 5" ); } } // This code is contributed by nitin mittal. |
PHP
<?php // assumes that n is a positive integer function isMultipleof5 ( $n ) { while ( $n > 0 ) $n = $n - 5; if ( $n == 0 ) return true; return false; } // Driver Code $n = 19; if ( isMultipleof5( $n ) == true ) echo ( "$n is multiple of 5" ); else echo ( "$n is not a multiple of 5" ); // This code is contributed by nitin mittal. ?> |
Javascript
<script> // JavaScript program for the above approach // assumes that n is a positive integer function isMultipleof5 (n) { while (n > 0) n = n - 5; if (n == 0) return true ; return false ; } // Driver Code let n = 19; if (isMultipleof5(n) == true ) document.write(n + " is multiple of 5" ); else document.write(n + " is not a multiple of 5" ); </script> |
输出:
19 is not a multiple of 5
时间复杂性: O(n/5)
辅助空间: O(1)
方法2(转换为字符串并检查最后一个字符) 将n转换为字符串,并检查字符串的最后一个字符。如果最后一个字符是“5”或“0”,则n是5的倍数,否则不是。
C++
#include <bits/stdc++.h> using namespace std; // Assuming that integer takes 4 bytes, there // can be maximum 10 digits in a integer # define MAX 11 bool isMultipleof5( int n) { char str[MAX]; int len = strlen (str); // Check the last character of string if ( str[len - 1] == '5' || str[len - 1] == '0' ) return true ; return false ; } // Driver Code int main() { int n = 19; if ( isMultipleof5(n) == true ) cout << n << " is multiple of 5" <<endl; else cout << n << " is not multiple of 5" <<endl; return 0; } // This code is contributed by SHUBHAMSINGH10 |
C
#include<stdio.h> #include<stdlib.h> #include<string.h> // Assuming that integer takes 4 bytes, there // can be maximum 10 digits in a integer # define MAX 11 bool isMultipleof5( int n) { char str[MAX]; int len = strlen (str); // Check the last character of string if ( str[len - 1] == '5' || str[len - 1] == '0' ) return true ; return false ; } // Driver Code int main() { int n = 19; if ( isMultipleof5(n) == true ) printf ( "%d is multiple of 5" , n); else printf ( "%d is not a multiple of 5" , n); return 0; } |
JAVA
// Assuming that integer // takes 4 bytes, there // can be maximum 10 // digits in a integer class GFG { static int MAX = 11 ; static boolean isMultipleof5( int n) { char str[] = new char [MAX]; int len = str.length; // Check the last // character of string if (str[len - 1 ] == '5' || str[len - 1 ] == '0' ) return true ; return false ; } // Driver Code public static void main(String[] args) { int n = 19 ; if ( isMultipleof5(n) == true ) System.out.println(n + " is multiple " + "of 5" ); else System.out.println(n + " is not a " + "multiple of 5" ); } } // This code is contributed by mits |
Python3
# Assuming that integer # takes 4 bytes, there # can be maximum 10 # digits in a integer MAX = 11 ; def isMultipleof5(n): s = str (n); l = len (s); # Check the last # character of string if (s[l - 1 ] = = '5' or s[l - 1 ] = = '0' ): return True ; return False ; # Driver Code n = 19 ; if (isMultipleof5(n) = = True ): print (n, "is multiple of 5" ); else : print (n, "is not a multiple of 5" ); # This code is contributed by mits |
C#
using System; // Assuming that integer // takes 4 bytes, there // can be maximum 10 // digits in a integer class GFG { static int MAX = 11; static bool isMultipleof5( int n) { char [] str = new char [MAX]; int len = str.Length; // Check the last // character of string if (str[len - 1] == '5' || str[len - 1] == '0' ) return true ; return false ; } // Driver Code static void Main() { int n = 19; if ( isMultipleof5(n) == true ) Console.WriteLine( "{0} is " + "multiple of 5" , n); else Console.WriteLine( "{0} is not a " + "multiple of 5" , n); } } // This code is contributed by mits |
PHP
<?php // Assuming that integer // takes 4 bytes, there // can be maximum 10 // digits in a integer $MAX = 11; function isMultipleof5( $n ) { global $MAX ; $str = (string) $n ; $len = strlen ( $str ); // Check the last // character of string if ( $str [ $len - 1] == '5' || $str [ $len - 1] == '0' ) return true; return false; } // Driver Code $n = 19; if (isMultipleof5( $n ) == true ) echo "$n is multiple of 5" ; else echo "$n is not a multiple of 5" ; // This code is contributed by mits ?> |
Javascript
<script> // Assuming that integer // takes 4 bytes, there // can be maximum 10 // digits in a integer MAX = 11; function isMultipleof5(n) { str = Array(n).fill( '' ); var len = str.length; // Check the last // character of string if (str[len - 1] == '5' || str[len - 1] == '0' ) return true ; return false ; } // Driver Code var n = 19; if ( isMultipleof5(n) == true ) document.write(n + " is multiple " + "of 5" ); else document.write(n + " is not a " + "multiple of 5" ); // This code is contributed by Amit Katiyar </script> |
输出:
19 is not a multiple of 5
时间复杂性: O(1)
辅助空间: O(最大值)
感谢Baban_Rathore提出这种方法。 方法3(将最后一位设置为0,并使用浮点技巧) 在两种情况下,数字n可以是5的倍数。当n的最后一位是5或10时。如果设置了n的二进制等价物中的最后一位(当最后一位为5时,可能是这种情况),那么我们使用n<<=1乘以2,以确保如果数字是5的倍数,那么最后一位为0。一旦我们这样做了,我们的工作就是检查最后一个数字是否为0,这可以通过使用浮点和整数比较技巧来实现。
C++
#include <iostream> using namespace std; bool isMultipleof5( int n) { // If n is a multiple of 5 then we // make sure that last digit of n is 0 if ( (n & 1) == 1 ) n <<= 1; float x = n; x = ( ( int )(x * 0.1) ) * 10; // If last digit of n is 0 then n // will be equal to (int)x if ( ( int )x == n ) return true ; return false ; } // Driver Code int main() { int i = 19; if ( isMultipleof5(i) == true ) cout << i << " is multiple of 5" ; else cout << i << " is not a multiple of 5" ; getchar (); return 0; } // This code is contributed by shubhamsingh10 |
C
#include<stdio.h> bool isMultipleof5( int n) { // If n is a multiple of 5 then we // make sure that last digit of n is 0 if ( (n & 1) == 1 ) n <<= 1; float x = n; x = ( ( int )(x * 0.1) ) * 10; // If last digit of n is 0 then n // will be equal to (int)x if ( ( int )x == n ) return true ; return false ; } // Driver Code int main() { int i = 19; if ( isMultipleof5(i) == true ) printf ( "%d is multiple of 5" , i); else printf ( "%d is not a multiple of 5" , i); getchar (); return 0; } |
JAVA
// Java code to check if // a number is multiple of // 5 without using / and % class GFG { static boolean isMultipleof5( int n) { // If n is a multiple of 5 // then we make sure that // last digit of n is 0 if ((n & 1 ) == 1 ) n <<= 1 ; float x = n; x = (( int )(x * 0.1 )) * 10 ; // If last digit of n is // 0 then n will be equal // to (int)x if (( int )x == n) return true ; return false ; } // Driver Code public static void main(String[] args) { int i = 19 ; if (isMultipleof5(i) == true ) System.out.println(i + "is multiple of 5" ); else System.out.println(i + " is not a " + "multiple of 5" ); } } // This code is contributed // by mits |
Python3
# Python code to check # if a number is multiple of # 5 without using / and % def isMultipleof5(n): # If n is a multiple of 5 then we # make sure that last digit of n is 0 if ( (n & 1 ) = = 1 ): n << = 1 ; x = n x = ( ( int )(x * 0.1 ) ) * 10 # If last digit of n is 0 # then n will be equal to x if ( x = = n ): return 1 return 0 # Driver Code i = 19 if ( isMultipleof5(i) = = 1 ): print (i, "is multiple of 5" ) else : print (i, "is not a multiple of 5" ) # This code is contributed # by Sumit Sudhakar |
C#
// C# code to check if // a number is multiple of // 5 without using / and % class GFG { static bool isMultipleof5( int n) { // If n is a multiple of 5 // then we make sure that // last digit of n is 0 if ((n & 1) == 1) n <<= 1; float x = n; x = (( int )(x * 0.1)) * 10; // If last digit of n is // 0 then n will be equal // to (int)x if (( int )x == n) return true ; return false ; } // Driver Code public static void Main() { int i = 19; if (isMultipleof5(i) == true ) System.Console.WriteLine(i + "is multiple of 5" ); else System.Console.WriteLine(i + " is not a " + "multiple of 5" ); } } // This code is contributed // by mits |
PHP
<?php function isMultipleof5( $n ) { // If n is a multiple of 5 // then we make sure that // last digit of n is 0 if (( $n & 1) == 1 ) $n <<= 1; $x = $n ; $x = ((int)( $x * 0.1)) * 10; // If last digit of n // is 0 then n will be // equal to (int)x if ( (int)( $x ) == $n ) return true; return false; } // Driver Code $i = 19; if ( isMultipleof5( $i ) == true ) echo "$i is multiple of 5" ; else echo "$i is not a multiple of 5" ; // This code is contributed by mits ?> |
Javascript
<script> // JavaScript code to check if // a number is multiple of // 5 without using / and % function isMultipleof5( n) { // If n is a multiple of 5 then we // make sure that last digit of n is 0 if ( (n & 1) == 1 ) n <<= 1; x = ( (int)(x * 0.1) ) * 10; // If last digit of n is 0 then n // will be equal to (int)x if ( (int)(x == n)) return true ; return false ; } // Driver Code i = 19; if ( isMultipleof5 == true ) document.write( i + " is multiple of 5" ); else document.write( i + " is not a multiple of 5" ); // This code is contributed by simranarora5sos </script> |
输出:
19 is not a multiple of 5
时间复杂性: O(1)
辅助空间: O(1)
多亏darkprince提出了这种方法。 如果您发现上述代码/算法不正确,请写下评论,或者寻找其他方法来解决相同的问题。