zoukankan      html  css  js  c++  java
  • 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 }
  • 相关阅读:
    JS随笔
    tp5 redis 单例模式 转载
    分享我编程工作经历及对软件开发前景的看法
    redis详解(一)-- 概述
    redis详解(二)-- 数据类型详解
    redis详解(四)-- 高可用分布式集群
    redis详解(三)
    新工科平台
    关于Nginx的负载均衡
    微信退款回调
  • 原文地址:https://www.cnblogs.com/crazyapple/p/3335220.html
Copyright © 2011-2022 走看看