Software Design Engineer Questions[评:知名公司笔试]

stars_625 2008-07-15 10:24:41
1.Write a function that would: sort an integer array in increasing order.

//example: “3 2 5” => “2 3 5”
void sort(int *a, int len)
{
int i;
int *tmp;

for(len--; len>0; len--)
{
for(i=0; i<len; i++)
{
if(*(a+i)>*(a+i+1))
{
*tmp = *(a+i);
*(a+i) = *(a+i+1);
*(a+i+1) = *tmp;
}
}
}
}

冒泡排序

2.Write a function that would: remove extra spaces in a string without using buffer. (i.e. only keep one space for multiple contiguous spaces)

//example: “a b c” => “a b c”
void m_remove(char *s)
{
char *tmp;
char *spaces = " ";

tmp = strstr(s, spaces);
while(tmp)
{
strcpy(tmp,tmp+1);
tmp = strstr(s, spaces);
}
}

char strstr(const char *s1,const char *s2)
扫描字符串s2,并返回第一次出现s1的位置

char strcpy(char *dest,const char *src)
将字符串src复制到dest

3.Write a function that would: return the number of ones for an integer when it is written in binary format

//example: if a is 5 (101), return 2; if a is 8 (1000), return 1

int count(int a)
{
int len = 0;
int one_num = 0;

len = sizeof(a); /* 求出int字节数,提高系统兼容性 */
len = len << 3; /* 乘八,占用二进制位数 */

while(len--)
{
if(a & 0x01)one_num++; /* 判a的第0位是否为1 */
a >>= 1; /* a左移一位 */
}
return one_num;
}
/* 总体思想是将整形转换成二位制,进行位判断 */

...全文
102 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

69,373

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧