不使用除法和乘法运算符计算7n/8

给定一个整数,编写一个计算⌈7n/8⌉ ( 天花板 不使用除法和乘法运算符。 我们强烈建议您尽量减少浏览器数量,并首先自己尝试。

null

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

以下是上述理念的实施:

C++

// C++ program to evaluate ceil(7n/8)
// without using * and /
#include <iostream>
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 contributed by khushboogoyal499


C

// C program to evaluate ceil(7n/8) without using * and /
#include <stdio.h>
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

<?php
// PHP program to evaluate ceil
// (7n/8) without using * and
function multiplyBySevenByEight( $n )
{
// Note the inner bracket here.
// This is needed because
// precedence of '-' operator
// is higher than '<<'
return ( $n - ( $n >> 3));
}
// Driver Code
$n = 9;
echo multiplyBySevenByEight( $n );
// This code is contributed by Ajit
?>


Javascript

<script>
// JavaScript program to evaluate ceil(7n/8) without using * and /
function 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 */
let n = 9;
document.write(multiplyBySevenByEight(n));
// This code is contributed by Surbhi Tyagi.
</script>


输出:

8

时间复杂性: O(1)

辅助空间: O(1)

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

C++

// C++ program to evaluate 7n/8 without using * and /
#include<iostream>
using namespace std;
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;
cout<< " " << multiplyBySevenByEight(n);
return 0;
}
// This code is contributed by shivanisinghss2110


C

// C program to evaluate 7n/8 without using * and /
#include<stdio.h>
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

<?php
// PHP program to evaluate 7n/8
// without using * and /
function 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;
echo multiplyBySevenByEight( $n );
// This code is contributed by anuj_67.
?>


Javascript

<script>
// Javascript program to evaluate 7n/8
// without using * and /
function 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
var n = 15;
document.write(multiplyBySevenByEight(n));
// This code is contributed by shikhasingrajput
</script>


输出:

13

时间复杂性: O(1)

辅助空间: O(1)

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

幸亏 纳伦德拉·康拉尔卡 感谢你提出这种方法。 本文由 拉杰夫 。如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请发表评论

© 版权声明
THE END
喜欢就支持一下吧
点赞6 分享