📜  将给定的整数乘以3.5

📅  最后修改于: 2021-05-25 04:30:58             🧑  作者: Mango

给定整数x,编写一个将x乘以3.5并返回整数结果的函数。不允许使用%,/,*。

Examples :
Input: 2
Output: 7

Input: 5
Output: 17 (Ignore the digits after decimal point)

解决方案:
1.我们可以通过将2 * x,x和x / 2相加得到x * 3.5。要计算2 * x,请将x左移1,并计算x / 2,x右移2。
下面是上述方法的实现:

C++
// C++ program to multiply
// a number with 3.5
#include 
 
int multiplyWith3Point5(int x)
{
    return (x<<1) + x + (x>>1);
}
 
/* Driver program to test above functions*/
int main()
{
    int x = 4;
    printf("%d", multiplyWith3Point5(x));
    getchar();
    return 0;
}


Java
// Java Program to multiply
// a number with 3.5
 
class GFG {
         
    static int multiplyWith3Point5(int x)
    {
        return (x<<1) + x + (x>>1);
    }
     
    /* Driver program to test above functions*/
    public static void main(String[] args)
    {
        int x = 2;
        System.out.println(multiplyWith3Point5(x));
    }
}
 
// This code is contributed by prerna saini.


Python3
# Python 3 program to multiply
# a number with 3.5
 
def multiplyWith3Point5(x):
 
    return (x<<1) + x + (x>>1)
  
 
# Driver program to
# test above functions
x = 4
print(multiplyWith3Point5(x))
 
# This code is contributed by
# Smitha Dinesh Semwal


C#
// C# Program to multiply
// a number with 3.5
using System;
 
class GFG
{
         
    static int multiplyWith3Point5(int x)
    {
        return (x<<1) + x + (x>>1);
    }
     
    /* Driver program to test above functions*/
    public static void Main()
    {
        int x = 2;
        Console.Write(multiplyWith3Point5(x));
    }
     
}
 
// This code is contributed by Sam007


PHP
> 1);
}
 
// Driver Code
$x = 4;
echo multiplyWith3Point5($x);
     
// This code is contributed by vt_m.
?>


Javascript


C
#include 
int multiplyWith3Point5(int x)
{
  return ((x<<3) - x)>>1;
}


C++
// Cpp program for above approach
#include 
using namespace std;
 
// Function to multiple number
// with 3.5
int multiplyWith3Point5(int x){
        int r = 0;
 
        // The 3.5 is 7/2, so multiply
        // by 7 (x * 7) then
        // divide the result by 2
        // (result/2) x * 7 -> 7 is
        // 0111 so by doing mutiply
        // by 7 it means we do 2
        // shifting for the number
        // but since we doing
        // multiply we need to take
        // care of carry one.
        int x1Shift = x << 1;
        int x2Shifts = x << 2;
 
        r = (x ^ x1Shift) ^ x2Shifts;
        int c = (x & x1Shift) | (x & x2Shifts)
                | (x1Shift & x2Shifts);
        while (c > 0) {
            c <<= 1;
            int t = r;
            r ^= c;
            c &= t;
        }
 
        // Then divide by 2
        // r / 2
        r = r >> 1;
        return r;
    }
   
// Driver Code
int main() {
    cout<<(multiplyWith3Point5(5));
    return 0;
}
 
// This code is contributed by rohitsingh07052.


Java
// Java program for above approach
import java.io.*;
 
class GFG
{
    
    // Function to multiple number
    // with 3.5
    static int multiplyWith3Point5(int x)
    {
        int r = 0;
 
        // The 3.5 is 7/2, so multiply
        // by 7 (x * 7) then
        // divide the result by 2
        // (result/2) x * 7 -> 7 is
        // 0111 so by doing mutiply
        // by 7 it means we do 2
        // shifting for the number
        // but since we doing
        // multiply we need to take
        // care of carry one.
        int x1Shift = x << 1;
        int x2Shifts = x << 2;
 
        r = (x ^ x1Shift) ^ x2Shifts;
        int c = (x & x1Shift) | (x & x2Shifts)
                | (x1Shift & x2Shifts);
        while (c > 0) {
            c <<= 1;
            int t = r;
            r ^= c;
            c &= t;
        }
 
        // Then divide by 2
        // r / 2
        r = r >> 1;
        return r;
    }
   
    // Driver Code
    public static void main(String[] args)
    {
        System.out.println(multiplyWith3Point5(5));
    }
}


输出
14

2.执行此操作的另一种方法是(8 * x – x)/ 2(请参见下面的代码)。感谢Ajaym提出的建议。

C

#include 
int multiplyWith3Point5(int x)
{
  return ((x<<3) - x)>>1;
}   

另一种方法:

另一种方法是将二进制乘以7,然后仅使用<<,^,&和>>将其除以2。

但是在这里我们不得不提到,只有正数可以传递给该方法。

下面是上述方法的实现:

C++

// Cpp program for above approach
#include 
using namespace std;
 
// Function to multiple number
// with 3.5
int multiplyWith3Point5(int x){
        int r = 0;
 
        // The 3.5 is 7/2, so multiply
        // by 7 (x * 7) then
        // divide the result by 2
        // (result/2) x * 7 -> 7 is
        // 0111 so by doing mutiply
        // by 7 it means we do 2
        // shifting for the number
        // but since we doing
        // multiply we need to take
        // care of carry one.
        int x1Shift = x << 1;
        int x2Shifts = x << 2;
 
        r = (x ^ x1Shift) ^ x2Shifts;
        int c = (x & x1Shift) | (x & x2Shifts)
                | (x1Shift & x2Shifts);
        while (c > 0) {
            c <<= 1;
            int t = r;
            r ^= c;
            c &= t;
        }
 
        // Then divide by 2
        // r / 2
        r = r >> 1;
        return r;
    }
   
// Driver Code
int main() {
    cout<<(multiplyWith3Point5(5));
    return 0;
}
 
// This code is contributed by rohitsingh07052.

Java

// Java program for above approach
import java.io.*;
 
class GFG
{
    
    // Function to multiple number
    // with 3.5
    static int multiplyWith3Point5(int x)
    {
        int r = 0;
 
        // The 3.5 is 7/2, so multiply
        // by 7 (x * 7) then
        // divide the result by 2
        // (result/2) x * 7 -> 7 is
        // 0111 so by doing mutiply
        // by 7 it means we do 2
        // shifting for the number
        // but since we doing
        // multiply we need to take
        // care of carry one.
        int x1Shift = x << 1;
        int x2Shifts = x << 2;
 
        r = (x ^ x1Shift) ^ x2Shifts;
        int c = (x & x1Shift) | (x & x2Shifts)
                | (x1Shift & x2Shifts);
        while (c > 0) {
            c <<= 1;
            int t = r;
            r ^= c;
            c &= t;
        }
 
        // Then divide by 2
        // r / 2
        r = r >> 1;
        return r;
    }
   
    // Driver Code
    public static void main(String[] args)
    {
        System.out.println(multiplyWith3Point5(5));
    }
}

输出
17