不知道这题算作什么类型的题目,反正很巧妙,队友小杰想了没一会就搞定了
为了学习这种方法,我也搞了搞,其实思路不难想,位运算嘛,只有0和1,而且该位的运算只影响该位,最多20位,一位一位地计算即可,只需要把每位的情况考虑清楚
#include <stdio.h> int a[210]; char op[210]; double p[210]; int main() { int n,cas=0; while(scanf("%d",&n)==1) { for(int i=0;i<=n;i++) scanf("%d",&a[i]); for(int i=1;i<=n;i++) { getchar(); scanf("%c",&op[i]); } for(int i=1;i<=n;i++) scanf("%lf",&p[i]); double ans=0; for(int i=0;i<=20;i++) { double P; if(a[0]&(1<<i))P=1; else P=0; for(int j=1;j<=n;j++) { if(a[j]&(1<<i)) { if(op[j]=='&') continue; else if(op[j]=='^') P=P*p[j]+(1-P)*(1-p[j]); else P=P*p[j]+1-p[j]; } else { if(op[j]=='&') P=P*p[j]; else continue; } } ans+=P*(1<<i); } printf("Case %d: ",++cas); printf("%lf ",ans); } return 0; }