bnuoj 20834 Excessive Space Remover(水水)

http://www.bnuoj.com/bnuoj/problem_show.php?pid=20834

【题意】:
 每次减少一半的空格,问经过多少次操作能得到每个单词之间的空格为1,输入字符串大小小于等于1MB

【题解】:
1、如果单词之间最大的空格数为2的n次方,那么输出2
2、否则输出n+1
注意字符串大小1MB,这里用C++中string进行处理
【code】:

 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <string>
 4 #include <string.h>
 5 #include <math.h>
 6 
 7 using namespace std;
 8 
 9 int main()
10 {
11     string str;
12     while(getline(cin,str))  //用string整行读入
13     {
14         int i,cnt=0,maks=-1;
15         for(i=0;i<str.size();i++)
16         {
17             if(str[i]!=' ')
18             {
19                 if(maks<cnt)
20                 {
21                     maks=cnt;  //记录最大空格数
22                 }
23                 cnt=0;
24             }
25             else
26             {
27                 cnt++;
28             }
29         }
30         double ans = log(maks)/log(2);
31         int cmp = (int)(ans);
32         if(fabs(ans-cmp)<1e-6)  //如果刚好是2的n次方
33         {
34             cout<<cmp<<endl;
35         }
36         else  //否则+1
37         {
38             cout<<cmp+1<<endl;
39         }
40     }
41     return 0;
42 }
 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <string>
 4 #include <string.h>
 5 #include <math.h>
 6 
 7 using namespace std;
 8 
 9 int main()
10 {
11     string str;
12     while(getline(cin,str))  //用string整行读入
13     {
14         int i,cnt=0,maks=-1;
15         for(i=0;i<str.size();i++)
16         {
17             if(str[i]!=' ')
18             {
19                 if(maks<cnt)
20                 {
21                     maks=cnt;  //记录最大空格数
22                 }
23                 cnt=0;
24             }
25             else
26             {
27                 cnt++;
28             }
29         }
30         double ans = log(maks)/log(2);
31         int cmp = (int)(ans);
32         if(fabs(ans-cmp)<1e-6)  //如果刚好是2的n次方
33         {
34             cout<<cmp<<endl;
35         }
36         else  //否则+1
37         {
38             cout<<cmp+1<<endl;
39         }
40     }
41     return 0;
42 }

 

转载于:https://www.cnblogs.com/crazyapple/p/3335220.html

THE END
< <上一篇
下一篇>>