首页 > 其他 > 详细

[GeeksForGeeks] Multiply a given integer by 3.5

时间:2017-07-20 09:40:44      阅读:265      评论:0      收藏:0      [点我收藏+]

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

原文:http://www.cnblogs.com/lz87/p/7209117.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!