小编KBE*_*KBE的帖子

编写一个C函数,将一个数字向下舍入到下一个2的幂

我在接受采访时得到了以下问题:"编写一个C函数,将一个数字向下舍入到下一个2的幂".

我写了以下答案:

#include <stdio.h>

int next_pwr_of_2(int num)
{
    int tmp;

    do
    {
        num++;
        tmp=num-1;
    }
    while (tmp & num != 0);

    return num;
}

void main()
{
    int num=9;
    int next_pwr;
    next_pwr=next_pwr_of_2(num);
    printf(" %d \n",next_pwr);
}
Run Code Online (Sandbox Code Playgroud)

问题是:为什么程序do-while在达到11和10值时会退出循环?

c

11
推荐指数
2
解决办法
2万
查看次数

ng-repeat将索引值传递给函数

我需要将一个特定元素的$ index值(用ng-repeat添加)传递给javascript函数.我的代码示例:

<tr ng-repeat="cells in CouponsList.CellPhones">
<td><button  ng-click="doStuff($index+1)">{{cells.localVendorAddress}}</button></td>
Run Code Online (Sandbox Code Playgroud)

现在我添加了几个按钮,当按下一个特定按钮时,我需要将它的特定$ index发送到doStuff($ index)函数.任何的想法?

angularjs angularjs-ng-repeat angularjs-ng-click

6
推荐指数
2
解决办法
3万
查看次数

CSS计算宽度和高度值

如何使用CSS计算屏幕高度并将返回值用于宽度计算?有可能吗?

myClass{    
    height: calc(50% - 33.5px);
    width: heightReturnedValue*1.3
}
Run Code Online (Sandbox Code Playgroud)

html css html5 css3

3
推荐指数
1
解决办法
1万
查看次数

整数乘法没有*

我在其中一个网站上看到了C中的一个面试问题,你被要求编写一个函数,该函数得到2个整数,num和times,并且不使用*运算符多个,这意味着主要使用左右移位.我提出了一个有效的答案(除非有人发现了一个错误),但是有没有人有更好的方法在更好的时间或内存消耗中解决它?

这是我写的:

#include <stdio.h>

int multiply_with_shift (int num, int times)
{
   int cnt=0;
   int org_times=times;
   if((num & times)==0)
       return 0;
   else
   {
   while(times >1)   
   {
      times=  times >> 1;
      cnt++;
   }
   int val= 1;
      val= val <<cnt;              
   int sub= org_times-val;         
   int res= num << cnt;            
   for( int i=0 ; i < sub; i++)    
   {
      res+=num;
   }
      return res;
   }
}


void main()
{
    int tmp;
    tmp=multiply_with_shift(5,15);
    printf(" the answer is : %d \n", tmp);
    printf("\n");
}
Run Code Online (Sandbox Code Playgroud)

c

-1
推荐指数
1
解决办法
227
查看次数