移位运算遇到long long要强制转换???!!

题目https://nanti.jisuanke.com/t/43375
The famous City Central Zoo houses just two creatures, Zebras and Ocelots. These magical creatures spend their time in a single, tall column, each standing atop the back of the creature below it. Whenever the zoo bell rings, the ocelot lowest in the pile turns into a zebra, and all zebras (if there are any) below that creature simultaneously turn into ocelots. If there is no ocelot in the pile when the bell tolls, then nothing happens. The zookeeper has been watching this interesting process for years. Today, suddenly, he begins to wonder how much longer this can go on. That is, given a pile of zebras and/or ocelots, how many times may the bell toll before there are no more ocelots?

Input consists of a number N in the range 1 to 60, followed by N lines, each of which is a single character, either Z (for zebra) or O (for ocelot). These give the order of the creatures from top (first) to bottom (last).

Output should be a single integer giving the number of times the bell must toll in order for there to be no more ocelots.

规律:用Z表示1,O表示0,这样从上到下组成一个二进制数,用这个数与1<<n -1做差,相差的值的十进制就是答案

但是好像要强制转换longlong在能过,没搞明白为什么。。。

#include <bits/stdc++.h>
using namespace std;
const int maxn=100001;
typedef long long ll;
const double esp=1e-4;
const int mod=1e9+7;
ll ans;int n;
ll sum;ll num;

int main(){
    scanf("%d",&n);char ch;
    ll add=(ll)1<<(n-1);
    //ll add=1<<(n-1); 不强制转换就答案错误
    sum=num=0;
    for(int i=0;i<n;i++){
        getchar();
        scanf("%c",&ch);
        if(ch=='Z'){
            num+=add;
        }
        sum+=add;
        add>>=1;
    }
    ans=sum-num;
    printf("%lld",ans);

    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值