首页 > 试题广场 >

牛牛与2的幂次方(2)

[编程题]牛牛与2的幂次方(2)
  • 热度指数:1153 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
牛牛特别喜欢数字7,他想知道,一个数减去7后是否刚好是2的幂次方,不过他不知道该怎么做,所以他想请你帮忙。
给定一个数字n,如果该数减去7后是2的幂次方,返回"YES",否则,返回"NO"。
示例1

输入

9

输出

"YES"

说明

9-7=2,是2的幂次方。 

备注:
n能整除2就不断除以2,最后看n是不是被除成1了
import java.util.*;


public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 给定一个数字n,如果该数减去7后是2的幂次方,返回"YES",否则,返回"NO"。
     * @param n int整型 代表题目中的n
     * @return string字符串
     */
    public String solve (int n) {
        // write code here
        n -= 7;
        while(n % 2 == 0) n /= 2;
        return n == 1? "YES": "NO";
    }
}

发表于 2021-04-23 09:16:27 回复(0)
/**
 * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
 *
 * 给定一个数字n,如果该数减去7后是2的幂次方,返回"YES",否则,返回"NO"。
 * @param n int整型 代表题目中的n
 * @return string字符串
 */
char* solve(int n ) {
    // write code here
    if (((n - 7) & (n - 8)) == 0) {
        return "YES";
    } else {
        return "NO";
    }
}
发表于 2023-10-23 21:03:57 回复(0)
/**
 * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
 *
 * 给定一个数字n,如果该数减去7后是2的幂次方,返回"YES",否则,返回"NO"。
 * @param n int整型 代表题目中的n
 * @return string字符串
 */
char* solve(int n ) {
    // write code here
    for(int i=0;pow(2,i)<=n-7;i++)
    {
        if((n-7)==pow(2,i))
            return "YES";
    }
    return "NO";
}
发表于 2021-09-06 14:41:12 回复(1)
class Solution:
    def solve(self , n ):
        n-=7
        while n%2==0&nbs***bsp;n==1:
            if n==1:
                return 'YES'
            n//=2
        return 'NO'

发表于 2021-06-15 20:50:31 回复(0)
func solve( n int ) string {
    i:=n-7
    for i>2{
        if i%2==0{
            i=i/2
        }else {
            return "NO"
        }
    }
    return "YES"
}
发表于 2021-06-04 15:08:49 回复(0)
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
# 给定一个数字n,如果该数减去7后是2的幂次方,返回"YES",否则,返回"NO"。
# @param n int整型 代表题目中的n
# @return string字符串
#
class Solution:
    def solve(self , n ):
        # write code here
        i=0
        while 2**i<=(n-7):
            if 2**i == n-7:
                return "YES"
                break
            else:
                i+=1
        return "NO"
                
发表于 2021-04-22 20:34:59 回复(0)
import java.util.*;


public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 给定一个数字n,如果该数减去7后是2的幂次方,返回"YES",否则,返回"NO"。
     * @param n int整型 代表题目中的n
     * @return string字符串
     */
    public String solve (int n) {
        // write code here
        n -= 7;
        while(n > 2){
            if(n % 2 != 0)
                return "NO";
            n >>= 1;
        }
        return "YES";
    }
}

发表于 2021-04-08 16:02:47 回复(0)
class Solution {
public:
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 给定一个数字n,如果该数减去7后是2的幂次方,返回"YES",否则,返回"NO"。
     * @param n int整型 代表题目中的n
     * @return string字符串
     */
    string solve(int n) {
        // write code here
        n-=7;
        if(n<=0) return "NO";
        return n==(n&(-n)) ?"YES":"NO";
    }
};

发表于 2021-04-06 17:31:43 回复(0)
n-=7
        while n%2==0 or n==1:
            if n==1:
                return 'YES'
            n=int(n/2)
        return 'NO'
发表于 2021-03-26 14:38:19 回复(0)
536870919  这个数减7是2的幂次方吗?



发表于 2021-03-08 15:27:53 回复(1)
public String solve (int n) {
        // write code here
        n = n-7;
        if(Integer.toBinaryString(n).lastIndexOf("1") == 0){
             return "YES";   
        }else{
            return "NO";
        }
    }

发表于 2021-02-25 16:24:18 回复(0)
import java.util.*;


public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 给定一个数字n,如果该数减去7后是2的幂次方,返回"YES",否则,返回"NO"。
     * @param n int整型 代表题目中的n
     * @return string字符串
     */
    public String solve (int n) {
        // write code here
        n = n-7;
        return (n&(n-1))==0?"YES":"NO";
    }
}

发表于 2021-02-03 21:52:32 回复(0)

问题信息

难度:
12条回答 1602浏览

热门推荐

通过挑战的用户