微软面试题【2】
来源:互联网 发布:淘宝有哪些好吃的零食 编辑:程序博客网 时间:2024/05/22 05:30
求下面函数的返回值(微软)
int func(int x){int count = 0;while(x){count++;x = x & (x-1);}return (count);}
自己补充的完整代码:
#include<iostream>using namespace std;int func(int x){int count = 0;while(x){count++;x = x & (x-1);}return (count);}int main(){int x = 0;cout<<"Input: ";cin>>x;x = func(x);cout<<"Return: "<<x<<endl; return 0;}
运行结果:(思路:将x转化为2进制,看含有的1的个数)
注: 每执行一次x = x&(x-1),会将x用二进制表示时最右边的一个1变为0,因为x-1将会将该位(x用二进制表示时最右边的一个1)变为0。
即:x = 1111, x-1 = 1110, 则 x&(x-1) = 1100.
- 微软面试题 2
- 微软面试题【2】
- 微软面试题2
- 微软面试题2 .
- 微软面试题 07012012[2]
- 微软面试题 07022012 [2]
- 微软面试题[1-2]
- 微软面试-微软面试题(2)
- 微软面试题汇总(2)
- 微软面试题汇总(2)
- 微软的面试题(答案)(2)
- 微软面试题程序(2)
- c++练习2 微软面试题
- 一道微软面试题
- 微软面试题
- 微软面试题
- 微软面试题(转载)
- 微软面试题
- linux下SVN配置笔记(转)
- Brief Intro to Key Value Coding
- struts2 url访问的参数
- ArcGISServer Flex API 内嵌饼状图
- LOL秘籍
- 微软面试题【2】
- js模拟点击那点事
- Linux PS 命令 详解(转)
- 第一个Android程序--Hello World
- Android 线程消息循环机制
- Ubuntu下HBase安装过程
- WatiN系列之二 解析Watin
- memcache和redis本质区别在哪里--汇总
- java web项目整体异常处理机制