寒光博客

[Codeforces]#580 (Div. 2) A. BowWow and the Timetable 二进制巧解
https://codeforces.com/contest/1204/problem/A A. BowWow a...
扫描右侧二维码阅读全文
21
2019/08

[Codeforces]#580 (Div. 2) A. BowWow and the Timetable 二进制巧解

https://codeforces.com/contest/1204/problem/A

A. BowWow and the Timetable

In the city of Saint Petersburg, a day lasts for 2^100 minutes. From the main station of Saint Petersburg, a train departs after 1 minute, 4 minutes, 16 minutes, and so on; in other words, the train departs at time 4^k for each integer k≥0. Team BowWow has arrived at the station at the time s 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 s. For example if s=20, then they missed trains which have departed at 1, 4 and 16. As you are the only one who knows the time, help them!

Note that the number s will be given you in a binary representation without leading zeroes.

Input
The first line contains a single binary number s (0≤s<2^100) without leading zeroes.

Output
Output a single number — the number of trains which have departed strictly before the time s.

题目大意及分析

给一个二进制串 判断这个二进制数字 大于多少个2^k (必须大于不能等于)
既然是二进制就可以巧解咯
二进制的话 从低位第二位开始 每两位 k ==> ++k
直接len/2可以得出 只有一个1的情况下 最多的个数
其次就是特判断 如果这个数字恰好是2^k 那么其中有一个是1 那么这个数一定大于2^k

如果首位等于某个4的幂,若后面任意一位为1,则答案+1
否则为首位位数/2

代码

package cf._581D2;
import java.util.Scanner;

public class A {
    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in);
        String S = cin.next();
        int ans = S.length() / 2;
        if ((S.length() - 1) % 2 == 0) {//首位是4倍数
            for (int i = 1; i < S.length(); i++) {
                if (S.charAt(i) == '1') {
                    ans++;
                    break;
                }
            }
        }
        System.out.println(ans);
    }
}
本文作者:Author:     文章标题:[Codeforces]#580 (Div. 2) A. BowWow and the Timetable 二进制巧解
本文地址:https://dxoca.cn/Algorithm/259.html       百度未收录
版权说明:若无注明,本文皆为“Dxoca's blog (寒光博客)”原创,转载请保留文章出处。
Last modification:August 21st, 2019 at 04:14 pm
如果觉得我的文章对你有用,请随意赞赏

Leave a Comment