[GeeksForGeeks] Multiply a given integer by 3.5

Posted Push your limit!

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[GeeksForGeeks] Multiply a given integer by 3.5相关的知识,希望对你有一定的参考价值。

Given an integer x, write a method that multiplies x with 3.5 and returns the integer result. You are not allowed to use %, /, or *.

Examples:  input 2, output 7; input 5, output 17

 

Solution. Use left shift and right shift operators.

 1 public class Solution {
 2     public int multiply3point5(int x){
 3         return (x << 1) + x + (x >> 1);
 4     }
 5     public static void main(String[] args){
 6         Solution sol = new Solution();
 7         assert sol.multiply3point5(2) == 7;
 8         assert sol.multiply3point5(5) == 17;
 9     }
10 }

 

以上是关于[GeeksForGeeks] Multiply a given integer by 3.5的主要内容,如果未能解决你的问题,请参考以下文章

为啥这段代码在 leetcode 运行良好,但在 geeksforgeeks 出现分段错误?

GeeksForGeeks 翻译计划

geeksforgeeks@ Equal to product (Binary Search)

geeksforgeeks@ Maximum Index (Dynamic Programming)

c_cpp 键盘打字 - GeeksforGeeks

geeksforgeeks@ Minimum sum partition (Dynamic Programming)