Adobe--Without using /,% ,+and * operators. write a function to check whether a number is divisible by 3 or nor?
2012-04-15 00:17
597 查看
题目:
不使用/,%,+和*,如何判断一个数能否被3整除
解答:
关键提示:
如果n的二进制末位为0,那么n和n>>1同时被3整除或者不整除
如果n的二进制末位为1,那么n和(n>>1)-1同时被3整除或者不整除
下面摘抄一段别人对上面提示的解释:
n的二进制末尾为0 为偶数 假设n=2k;又因为 n能被3整除 所以 n=2*3K=6k;那么 n右移1位 相当于除以2 那么 n=n/2;n=3k ;3K一定会整除3; (不能整除 同理可得 即 n=3k+1);
如果n二进制末尾为1,那么是奇数 n=2k+1;又因为n可以被3整除,那么n=3*(2k+1); n右移1位,再减去1.就等于 3k,3k必然能被3整除(不能整除 同理可得)
摘自:http://page.renren.com/601284443/note/819990632
不使用/,%,+和*,如何判断一个数能否被3整除
解答:
关键提示:
如果n的二进制末位为0,那么n和n>>1同时被3整除或者不整除
如果n的二进制末位为1,那么n和(n>>1)-1同时被3整除或者不整除
bool IsTimesOf3(int n) { int lastPosition; if (n < 0) n = - n; while (n > 0) { lastPosition = n & 1; // 0 or 1 n >>= 1; n = n - lastPosition; // -0 or -1 } return (n == 0); // yes when n%3 == 0 ,else here n < 0 }
下面摘抄一段别人对上面提示的解释:
n的二进制末尾为0 为偶数 假设n=2k;又因为 n能被3整除 所以 n=2*3K=6k;那么 n右移1位 相当于除以2 那么 n=n/2;n=3k ;3K一定会整除3; (不能整除 同理可得 即 n=3k+1);
如果n二进制末尾为1,那么是奇数 n=2k+1;又因为n可以被3整除,那么n=3*(2k+1); n右移1位,再减去1.就等于 3k,3k必然能被3整除(不能整除 同理可得)
摘自:http://page.renren.com/601284443/note/819990632
相关文章推荐
- How To Check Whether Physical Standby is in Sync with the Primary or Not?
- leetCode Given an integer (signed 32 bits), write a function to check whether it is a power of 4
- Write a program to convert string to number without using library function。
- Write an Efficient Method to Check if a Number is Multiple of 3(快速判断3的倍数)
- How to Check Whether Oracle Binary/Instance is RAC Enabled and Relink Oracle Binary in RAC (文档 ID 28
- How to check if a Number is Positive or Negative in Java - Interview Question
- This function has none of Deterministic,no sql,or reads sql data in its declaration and binary logging is enabled(you *might* want to use the less safe log_bin_trust_function_creators variable
- How to Check Whether Oracle Binary/Instance is RAC Enabled and Relink Oracle Binary in RAC (文档 ID 28
- How To Read and Write BLOB Data by Using ADO.NET with Visual C# .NET
- CareerCup Find whether a given number is a perfect square or not.(Addition and Subtraction only)
- Given any integer 0 <= n <= 10000 not divisible by 2 or 5, some multiple of n is a number which in d
- FW - check whether tree is balanced or not - find the max and min of one tree- 2013年12月18日13:12:04
- How to add, modify, or delete registry subkeys and values by using a registration entries (.reg) file
- How to write an application that supports the Fast User Switching feature by using Visual Basic .NET or Visual Basic
- How to Check Whether Oracle Binary/Instance is RAC Enabled and Relink Oracle Binary in RAC
- How to Check Whether Oracle Binary/Instance is RAC Enabled and Relink Oracle Binary in RAC (文档 ID 28
- Write an Efficient Method to Check if a Number is Multiple of 3
- Using partition by to write alternative SQL to get effdt and effseq items
- Given two strings s and t, write a function to determine if t is an anagram of s.
- How to check if a journal is EI, SCI, ISI, SCIE or SSCI and get its Impact Factor