📌  相关文章
📜  在不使用除法和乘法运算符的情况下计算7n / 8

📅  最后修改于: 2021-05-25 05:46:18             🧑  作者: Mango

给定的整数,编写一个函数,计算⌈7n/8⌉(顶棚的7N / 8),而无需使用除法和乘法运算符。
强烈建议最小化您的浏览器,然后自己尝试。

方法1:
这个想法是首先使用右移按位运算运算符来计算n / 8的底数,即⌊n/8⌋。表达式n >> 3产生相同的结果。
如果我们从n减去⌊n/8⌋,我们得到⌈7n/8⌉

下面是上述想法的实现:

C++
// C++ program to evaluate ceil(7n/8)
// without using * and /
#include 
using namespace std;
 
int multiplyBySevenByEight(int n)
{
     
    // Note the inner bracket here. This is needed
    // because precedence of '-' operator is higher
    // than '<<'
    return (n - (n >> 3));
}
 
// Driver code
int main()
{
    int n = 9;
    cout << multiplyBySevenByEight(n);
    return 0;
}
 
// This code is contribited by khushboogoyal499


C
// C program to evaluate ceil(7n/8) without using * and /
#include 
 
int multiplyBySevenByEight(unsigned int n)
{
    /* Note the inner bracket here. This is needed
       because precedence of '-' operator is higher
       than '<<' */
    return (n - (n >> 3));
}
 
/* Driver program to test above function */
int main()
{
    unsigned int n = 9;
    printf("%d", multiplyBySevenByEight(n));
    return 0;
}


Java
// Java program to evaluate ceil(7n/8)
// without using * and
import java.io.*;
 
class GFG {
    static int multiplyBySevenByEight(int n)
    {
        /* Note the inner bracket here. This is needed
        because precedence of '-' operator is higher
        than '<<' */
        return (n - (n >> 3));
    }
 
    // Driver code
    public static void main(String args[])
    {
        int n = 9;
        System.out.println(multiplyBySevenByEight(n));
    }
}
 
// This code is contributed by Anshika Goyal.


Python3
# Python program to evaluate ceil(7n/8) without using * and /
 
 
def multiplyBySevenByEight(n):
 
    # Note the inner bracket here. This is needed
    # because precedence of '-' operator is higher
    # than '<<'
    return (n - (n >> 3))
 
 
# Driver program to test above function */
n = 9
print(multiplyBySevenByEight(n))
 
# This code is contributed by
# Smitha Dinesh Semwal


C#
// C# program to evaluate ceil(7n/8)
// without using * and
using System;
 
public class GFG {
 
    static int multiplyBySevenByEight(int n)
    {
        /* Note the inner bracket here.
        This is needed because precedence
        of '-' operator is higher than
        '<<' */
        return (n - (n >> 3));
    }
 
    // Driver code
    public static void Main()
    {
        int n = 9;
 
        Console.WriteLine(multiplyBySevenByEight(n));
    }
}
 
// This code is contributed by Sam007.


PHP
> 3));
}
 
// Driver Code
$n = 9;
echo multiplyBySevenByEight($n);
 
// This code is contributed by Ajit
?>


Javascript


C
// C program to evaluate 7n/8 without using * and /
#include
 
int multiplyBySevenByEight(unsigned int n)
{   
    /* Step 1) First multiply number by 7 i.e. 7n = (n << 3) -n
     * Step 2) Divide result by 8 */
   return ((n << 3) -n) >> 3;
}
 
/* Driver program to test above function */
int main()
{
    unsigned int n = 15;
    printf("%u", multiplyBySevenByEight(n));
    return 0;
}


Java
// Java program to evaluate 7n/8
// without using * and /
import java.io.*;
 
class GFG
{
 
    static int multiplyBySevenByEight(int n)
    {
        // Step 1) First multiply number
        // by 7 i.e. 7n = (n << 3) -n
        // * Step 2) Divide result by 8
        return ((n << 3) -n) >> 3;
    }
     
    // Driver program
    public static void main(String args[])
    {
         
        int n = 15;
        System.out.println(multiplyBySevenByEight(n));
    }
}
 
// This code is contributed by Anshika Goyal.


Python3
# Python3 program to evaluate 7n/8
# without using * and /
 
def multiplyBySevenByEight(n):
     
    #Step 1) First multiply number
    # by 7 i.e. 7n = (n << 3) -n
    # Step 2) Divide result by 8
    return ((n << 3) -n) >> 3;
     
# Driver code
n = 15;
print(multiplyBySevenByEight(n));
 
#this code is contributed by sam007.


C#
// C# program to evaluate 7n/8
// without using * and /
using System;
 
public class GFG {
     
    static int multiplyBySevenByEight(int n)
    {
         
        // Step 1) First multiply number
        // by 7 i.e. 7n = (n << 3) -n
        // * Step 2) Divide result by 8
        return ((n << 3) -n) >> 3;
    }
     
    // Driver program
    public static void Main()
    {
        int n = 15;
         
        Console.WriteLine(
            multiplyBySevenByEight(n));
    }
}
 
// This code is contributed by Sam007.


PHP
> 3;
}
 
    // Driver Code
    $n = 15;
    echo multiplyBySevenByEight($n);
 
// This code is contributed by anuj_67.
?>


Javascript


输出 :

8

方法2(始终与7 * n / 8匹配):
上面的方法并不总是产生与“ printf(“%u”,7 * n / 8)”相同的结果。例如,对于n = 15,表达式7 * n / 8的值为13,但以上proogram会生成14。下面是始终与7 * n / 8匹配的修改版本。这个想法是先将数字乘以7,然后在表达式7 * n / 8中除以8。

C

// C program to evaluate 7n/8 without using * and /
#include
 
int multiplyBySevenByEight(unsigned int n)
{   
    /* Step 1) First multiply number by 7 i.e. 7n = (n << 3) -n
     * Step 2) Divide result by 8 */
   return ((n << 3) -n) >> 3;
}
 
/* Driver program to test above function */
int main()
{
    unsigned int n = 15;
    printf("%u", multiplyBySevenByEight(n));
    return 0;
}

Java

// Java program to evaluate 7n/8
// without using * and /
import java.io.*;
 
class GFG
{
 
    static int multiplyBySevenByEight(int n)
    {
        // Step 1) First multiply number
        // by 7 i.e. 7n = (n << 3) -n
        // * Step 2) Divide result by 8
        return ((n << 3) -n) >> 3;
    }
     
    // Driver program
    public static void main(String args[])
    {
         
        int n = 15;
        System.out.println(multiplyBySevenByEight(n));
    }
}
 
// This code is contributed by Anshika Goyal.

Python3

# Python3 program to evaluate 7n/8
# without using * and /
 
def multiplyBySevenByEight(n):
     
    #Step 1) First multiply number
    # by 7 i.e. 7n = (n << 3) -n
    # Step 2) Divide result by 8
    return ((n << 3) -n) >> 3;
     
# Driver code
n = 15;
print(multiplyBySevenByEight(n));
 
#this code is contributed by sam007.

C#

// C# program to evaluate 7n/8
// without using * and /
using System;
 
public class GFG {
     
    static int multiplyBySevenByEight(int n)
    {
         
        // Step 1) First multiply number
        // by 7 i.e. 7n = (n << 3) -n
        // * Step 2) Divide result by 8
        return ((n << 3) -n) >> 3;
    }
     
    // Driver program
    public static void Main()
    {
        int n = 15;
         
        Console.WriteLine(
            multiplyBySevenByEight(n));
    }
}
 
// This code is contributed by Sam007.

的PHP

> 3;
}
 
    // Driver Code
    $n = 15;
    echo multiplyBySevenByEight($n);
 
// This code is contributed by anuj_67.
?>

Java脚本


输出 :

13

注意:两种方法的结果之间存在差异。方法1产生ceil(7n / 8),但方法2产生7n / 8的整数。例如,对于n = 15,第一种方法的结果是14,而第二种方法的结果是13。