A. BowWow and the Timetable
http://codeforces.com/contest/1204/problem/A
In the city of Saint Petersburg, a day lasts for 21002100 minutes. From the main station of Saint Petersburg, a train departs after 11 minute, 44minutes, 1616 minutes, and so on; in other words, the train departs at time 4k4k for each integer k≥0k≥0. Team BowWow has arrived at the station at the time ss and it is trying to count how many trains have they missed; in other words, the number of trains that have departed strictly before time ss. For example if s=20s=20, then they missed trains which have departed at 11, 44 and 1616. As you are the only one who knows the time, help them!
Note that the number ss will be given you in a binary representation without leading zeroes.
Input
The first line contains a single binary number ss (0≤s<21000≤s<2100) without leading zeroes.
Output
Output a single number — the number of trains which have departed strictly before the time ss.
Examples
input
Copy
100000000
output
Copy
4
Note
In the first example 1000000002=256101000000002=25610, missed trains have departed at 11, 44, 1616 and 6464.
In the second example 1012=5101012=510, trains have departed at 11 and 44.
The third example is explained in the statements.
也不知何时能变绿,昨天一看到第一道题,woc,签到题简单啊,但是菜逼最后这道题并没有成功签到。
错误解法:进制转换,输入的二进制数转换为十进制数,然后for语句循环,每次变化范围i*=4,cnt++,其实吧,我举得逻辑没有问题,我过不去的原因是大数,当输入的字符串(二进制数)特别长时,就过不去。
题解:看了题解发现自己思路错了按照自己的做法需要写出大数二进制转换为十进制。题解给的方法直接是数学方法,应该算是简单的逻辑和基础数学的结合。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=2001;
char str[maxn];
int main()
{
int cnt=0,ans=0;
cin>>(str+1);
int len=strlen(str+1);
for(int i=1;i<=len;i++)
{
if(str[i]=='1')
{
cnt++;
}
}
if(len==1)//1输出的是0
{
ans=0;
}
else if(cnt==1)//只有一个1,则说明是2的幂
{
ans=len/2;
}
else
{
ans=(len+1)/2;
}
cout<<ans<<endl;
return 0;
}