首页 > 代码库 > [GeeksForGeeks] Multiply a given integer by 3.5
[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
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。