1. 程式人生 > 實用技巧 >IDEA 打包流程

IDEA 打包流程

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int N = 100010;
 4 typedef pair<string, int> PSI;
 5 PSI a[N]; //儲存n扇門的兩個屬性,first是操作方式,second是運算元
 6 int n, m;
 7 int cal(int bit, int now) { //返回用now和引數的第bit位進行n次運算之後的結果
 8     for (int i = 0; i < n; i++) {
 9         int x = a[i].second >> bit & 1
; //x等於第i扇門運算元的第bit位上的數 10 //然後根據操作方式進行操作 11 if (a[i].first == "AND") { 12 now &= x; 13 } else if (a[i].first == "OR") { 14 now |= x; 15 } else { 16 now ^= x; 17 } 18 } 19 return now; 20 } 21 int main() { 22 cin >> n >> m;
23 for (int i = 0; i < n; i++) { //輸入 24 string op; 25 int x; 26 cin >> op >> x; 27 a[i].first = op; 28 a[i].second = x; 29 } 30 int val = 0; //val用於判定該位放了1後是否超出了m 31 int ans = 0; //ans為最終答案 32 for (int bit = 29; bit >= 0; bit--) { //從高位到低位依次判斷該位是放1還是放0
33 int res0 = cal(bit, 0); //res0儲存第bit位填0後,又經過n次運算後的結果 34 int res1 = cal(bit, 1); //res1儲存第bit位填1後,又經過n次運算後的結果 35 if (res0 >= res1) { //該位放0 36 ans += res0 << bit; 37 } else if ((res0 < res1) && (val + (1 << bit) <= m)) { //該位放1 38 ans += res1 << bit; 39 val += res1 << bit; 40 } 41 } 42 cout << ans << endl; 43 return 0; 44 }