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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值