日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

acdream 1023 xor按位思考

發布時間:2023/12/13 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 acdream 1023 xor按位思考 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

思路:記答案為ans,統計出數列A和B在某二進制某一位上有多少個1,如果個數相同,則ans那一位上為0(因為題目要求最小的滿足條件的值),如果不一樣(則需要考慮那一位上異或個1),則判斷數列A在那一位上0的個數是否等于數列B那一位上1的個數,不等于則無解,否則,那一位上set為1,繼續判斷。

1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 using namespace std; 5 6 const int N = 31; 7 int cnt1[N]; 8 int cnt2[N]; 9 10 void decompose( int num, int * a ) 11 { 12 int p = 0; 13 while ( num ) 14 { 15 p++; 16 if ( num & 1 ) 17 { 18 a[p]++; 19 } 20 num >>= 1; 21 } 22 } 23 24 int main () 25 { 26 int n, tmp; 27 while ( scanf("%d", &n) != EOF ) 28 { 29 memset( cnt1, 0, sizeof(cnt1) ); 30 for ( int i = 0; i < n; i++ ) 31 { 32 scanf("%d", &tmp); 33 decompose( tmp, cnt1 ); 34 } 35 memset( cnt2, 0, sizeof(cnt2) ); 36 for ( int i = 0; i < n; i++ ) 37 { 38 scanf("%d", &tmp); 39 decompose( tmp, cnt2 ); 40 } 41 int ans = 0; 42 bool flag = true; 43 for ( int i = 1; i < N; i++ ) 44 { 45 if ( cnt1[i] == cnt2[i] ) continue; 46 if ( cnt1[i] + cnt2[i] == n ) 47 { 48 ans += ( 1 << ( i - 1 ) ); 49 } 50 else 51 { 52 flag = false; 53 break; 54 } 55 } 56 if ( !flag ) 57 { 58 ans = -1; 59 } 60 printf("%d\n", ans); 61 } 62 return 0; 63 }

?

轉載于:https://www.cnblogs.com/huoxiayu/p/4694590.html

總結

以上是生活随笔為你收集整理的acdream 1023 xor按位思考的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。