# a520: 12416 - Excessive Space Remover 題目連結:[a520](https://zerojudge.tw/ShowProblem?problemid=a520) 題目意思是將兩個空白取代為一個 那會需要處理最多次的一定是最長空白的部分 作法如下: \*\*\*\*\*\*\*\*\* Goodbye! 1. 9/2=4.5 =>5 2. 5/2=2.5 =>3 3. 3/2=1.5 =>2 4. 2/2=1 所以要做四次 那因為要每次都會少一半的量 因此只要算log(n)/log(2) 以下為程式: ```c= #include<stdio.h> #include<string.h> #include<math.h> char c[1000000]; int main(){ while(gets(c)){ int l=strlen(c),i,count=0,pr; double len=0.0,space=0.0; for(i=0;i<l;i++){ if(c[i]==' '){ space+=1.0; } if(c[i]!=' '){ space=0.0; } if(space>len){ len=space; } } len=log(len)/log(2); len=ceil(len); pr=len; printf("%d\n",pr); } return 0; } ``` log 運算完要無條件進位解決奇數的問題(如上述範例) 因為log的運算是double 所以輸出上要轉換成int ```c= len=log(len)/log(2); len=ceil(len); pr=len; printf("%d\n",pr); ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up