设为首页 加入收藏

TOP

NYOJ 46-最少乘法次数(数论)
2015-11-21 00:57:36 来源: 作者: 【 】 浏览:1
Tags:NYOJ 46- 最少 乘法 次数 数论

?

思路:可以化成二进制来求解,结果是最高位的位数-1+最高位后面1的个数。例如:对于3,它的二进制代码为11,就是用这个最高位(2-1)加上后面的1的个数(1个)。

用最高位1的目的是他能代表了转化的次数,因为2+2=4,4+4=8 8+8=16........

?

#include 
  
   
#include 
   
     #include 
    
      #include 
     
       #include 
      
        #include 
       
         #include 
        
          #include 
         
           #include 
          
            #include 
           
             #include
             using namespace std; typedef long long LL; const int inf=0x3f3f3f3f; const double pi= acos(-1.0); const double esp=1e-6; const int maxn=21010; char str[110]; int main() { int T,n,i,j; int cnt; scanf(%d,&T); while(T--){ scanf(%d,&n); memset(str,0,sizeof(str)); i=0; while(n/2!=0){ str[i++]='0'+n%2; n=n/2; } str[i]='1'; cnt=0; for(j=0;j
             
              

?

?

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Codeforces Round #311 (Div. 2) .. 下一篇C++再次理解虚表

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: