竹園論壇

標題: a316 - E 守塔新武器 [打印本頁]

作者: Sylveon    時間: 2014-4-27 11:42
標題: a316 - E 守塔新武器
原文:http://zerojudge.tw/ShowProblem?problemid=a316
AC   :http://zerojudge.tw/Submissions?problemid=a316&account=lfs92002
ACCODE:http://ideone.com/wj3ZNY
懷舊系列,國中時寫的code。位元運算,當初是@masson0223 在黑板上爆出結論的。我再補完計畫有發表解法,不過我覺得最後一個留言做得很漂亮,大家可以觀摩下。
補完 : http://www3.tcgs.tc.edu.tw/npsc/index.php?topic=301.0

#include<iostream>
  • using namespace std;
  • long long int pow2[35]={1,2};
  • short T,p;
  • long int H,s;
  • int main()
  • {
  •         for(int x=1;x<35;x++)
  •                 pow2[x]=pow2[x-1]*2;
  •         cin>>T;
  •         while(T--)
  •         {        
  •                 cin>>H;
  •                 p=s=0;
  •                 while(H!=0)
  •                 {
  •                         H-=pow2[p];
  •                         s++;
  •                         if(H%pow2[p+1]!=0)
  •                         {
  •                                 H-=pow2[p];
  •                                 s++;
  •                         }
  •                         p++;
  •                 }
  •                 cout<<s<<endl;
  •         }
  •         return 0;
  • }





  • 歡迎光臨 竹園論壇 (http://forum.tfcis.org/) Powered by Discuz! X3.2