码迷,mamicode.com
首页 > 编程语言 > 详细

算法题:不使用+-×/求一个数的7倍

时间:2015-08-11 23:21:23      阅读:135      评论:0      收藏:0      [点我收藏+]

标签:算法题

#include <iostream>
#include <stdlib.h>
#include <stdio.h>
using namespace std;
int Grial(int n)
{
    int a=n<<3;
    int b=a&(-n);
    int temp;
    a=a^(-n);

    while(b!=0)
    {
    b=b<<1; 
    temp=a;
    a=a^b;
    b=temp&b;
 }
return a;
}

int main()
{
    cout<<Grial(15)<<endl;
    cout<<Grial(-15)<<endl;
    cout<<Grial(5)<<endl;
    cout<<Grial(-11)<<endl;
    cout<<Grial(-13)<<endl;
    cout<<Grial(100000)<<endl;
    return 0;
}

技术分享

版权声明:本文为博主原创文章,未经博主允许不得转载。

算法题:不使用+-×/求一个数的7倍

标签:算法题

原文地址:http://blog.csdn.net/liuhuiyan_2014/article/details/47429599

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!