cf B Red and Blue Balls

思路:把字符串转化为一个二进制,遇到B就是一个数二进制中的1,答案就是这个数。

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 #define ll __int64
 5 using namespace std;
 6 
 7 int n;
 8 char str[100];
 9 
10 int main()
11 {
12    while(scanf("%d",&n)!=EOF)
13    {
14       scanf("%s",str);
15       int k=strlen(str);
16       ll ans=0;
17       ll s=1;
18       for(int i=0; i<k; i++)
19       {
20           if(str[i]=='B')
21           {
22               ans+=s;
23           }
24           s*=2;
25       }
26       printf("%I64d
",ans);
27    }
28    return 0;
29 }
View Code
原文地址:https://www.cnblogs.com/fanminghui/p/4259190.html