美罗的面试题

1. 交换整数变量a,b的值(不考虑溢出,不利用中间变量)

    Answer:a=a+b,b=a-b,a=a-b

2. 字符数组array,长度L,将此串中所有字符A去除

 Answer:

 char* p = &array[0];
 char *q;
 q = p;                    // p指向新数组的最后一个字符,q指向当前要判断是否为A的字符
 while ( q <= &array[0] +L )
 { 
    if ( *q != 'A')
    {
       *p = *q;
       p++;
     }
    q++;
 }
 

3. 字符串按照从小到大顺序排列(eg: char *str = "257abcd.....") ,给定一个字符(eg: char chr = 'k'),写出最优(考虑时间和空间)的查找此字符串的算法,并写出时间复杂度。
  
char * p = &str[0];
while ( *p != "/0")
{
   ..............(忘记了,此题做的不好)
}

后来请教了一下老邓,应该用二分法查找, 复杂度为log2 N
(联想:N个节点的二叉树,其层数为 log2 N 的取上界整数,eg: N=3,应当有2层,log23取上界整数为2)

4. 设计一个Dictionary的类

5. X is an integer , suppose X+1 and X-1 are prime numbers , to prove: X must be divided by 6.

Answer:

**: if x can't be divided by 6 and x is larger than 6 then x can't be divided by both 2 also 3 ,then here comes the probably situations: 1>x%2=1 ,x%3=1 in this case x+1%2=0,x-1%3=0。 2>x%2=1 ,x%3=2 in this case x+1%2=0,x+1%3=0。 3>x%2=0 ,x%3=1 in this case x-1%3=0。 4>x%2=0 ,x%3=2 in this case x+1%3=0。 5>x%2=1 ,x%3=0 in this case x-1%2=0。 and given x>=6 so x+1 and x-1 can't both be prime numbers which confilcts the given assumption.  


  我后来是用反证做的:假设x不能被6整除,那么a) x不能被2整除; b) x不能被3整除;对于a) x+1跟x-1就是偶数了,与条件矛盾; 对于b),也就是1) x=3N+1 或者2) x=3N+2 ,对于1)x-1=3N 非prime(此处产生了老沈提到的那个4排除在外的原因:对N的要求应该是大于1的自然数)对于2) x+1=3N+3=3(N+1) 非prime 。综上,x不能被6整除时无法满足x-1和x+1同时为质。得证。
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值