微软笔试
2012-08-22 21:42
375 查看
今天好友微软笔试,把题目发给我看了,不过由于英语不好,看了很久才懂,唉,题目如下:
Here goes the question:
>
> Write a method that find the int that has the most occurred number 1 in the input int array, and write test cases that you can think.
>
> Please return me the answer no later than 20:20.
这上面的1当时理解为整形的二进制中的1,则代码如下:
#include<iostream>
using namespace std;
int onenum (unsigned int iDec)
{
int size = 0;
unsigned int temp;
int i=31;
while(i>=0)
{
temp=iDec;
temp=temp>>i;
temp=temp&1;
if (1 == temp)
{
size++;
}
i--;
}
return size;
}
int main()
{
unsigned int a=3;
int arr[] = {1,117,2,3};
int len = sizeof(arr)/sizeof(int);
int cnt[10];
int max=0,flag;
for (int i=0;i<len;i++)
{
cnt[i] = onenum(arr[i]);
if(max<cnt[i])
{
max = cnt[i];
flag = i;
}
}
cout<<"The max is "<<flag<<endl;
return 1;
}
第二次看的时候,有可能觉得出题者的意思是说整形数中1的个数,所以为此将代码改了下:
#include<iostream>
using namespace std;
int onenum (unsigned int iDec)
{
int num = 0;
while(iDec/10)
{
if(1 == iDec%10)
{
num++;
}
iDec = iDec/10;
}
if(1 == iDec%10)
{
num++;
}
return num;
}
int main()
{
unsigned int a=3;
int arr[] = {1,117,2,3};
int len = sizeof(arr)/sizeof(int);
int cnt[10];
int max=0,flag;
for (int i=0;i<len;i++)
{
cnt[i] = onenum(arr[i]);
if(max<cnt[i])
{
max = cnt[i];
flag = i;
}
}
cout<<"The max is "<<flag<<endl;
return 1;
}
Here goes the question:
>
> Write a method that find the int that has the most occurred number 1 in the input int array, and write test cases that you can think.
>
> Please return me the answer no later than 20:20.
这上面的1当时理解为整形的二进制中的1,则代码如下:
#include<iostream>
using namespace std;
int onenum (unsigned int iDec)
{
int size = 0;
unsigned int temp;
int i=31;
while(i>=0)
{
temp=iDec;
temp=temp>>i;
temp=temp&1;
if (1 == temp)
{
size++;
}
i--;
}
return size;
}
int main()
{
unsigned int a=3;
int arr[] = {1,117,2,3};
int len = sizeof(arr)/sizeof(int);
int cnt[10];
int max=0,flag;
for (int i=0;i<len;i++)
{
cnt[i] = onenum(arr[i]);
if(max<cnt[i])
{
max = cnt[i];
flag = i;
}
}
cout<<"The max is "<<flag<<endl;
return 1;
}
第二次看的时候,有可能觉得出题者的意思是说整形数中1的个数,所以为此将代码改了下:
#include<iostream>
using namespace std;
int onenum (unsigned int iDec)
{
int num = 0;
while(iDec/10)
{
if(1 == iDec%10)
{
num++;
}
iDec = iDec/10;
}
if(1 == iDec%10)
{
num++;
}
return num;
}
int main()
{
unsigned int a=3;
int arr[] = {1,117,2,3};
int len = sizeof(arr)/sizeof(int);
int cnt[10];
int max=0,flag;
for (int i=0;i<len;i++)
{
cnt[i] = onenum(arr[i]);
if(max<cnt[i])
{
max = cnt[i];
flag = i;
}
}
cout<<"The max is "<<flag<<endl;
return 1;
}
相关文章推荐
- 微软2012年实习生录取笔试题(及答…
- 微软苏州校招笔试题目(1月10日)Colorful Lecture Note的解法
- 白话经典算法系列之九 从归并排序到数列的逆序数对(微软笔试题)
- 微软笔试题 跳台阶问题
- hihoCoder 1107 Shortest Proper Prefix 微软苏州校招笔试 1月17日
- 2011微软校园招聘笔试题
- 微软笔试面试题
- 微软笔试题1:Font Size
- 微软笔试题3:Demo Day
- 微软2016校园招聘在线笔试题目-第一题 Magic Box
- Islands Travel 微软2016校园招聘笔试题
- 微软2016校园招聘笔试(第一场) Professor Q's Software & Recruitment
- 微软笔试面试题
- 微软2014校招笔试编程题
- 微软笔试面试题集锦
- 微软2014年笔试题
- 微软实习笔试题
- ACM解题总结——HihoCoder1200 (微软笔试题)
- 微软笔试题 HihoCoder#1136: Professor Q's Software 题解
- 微软笔试面试题集锦