Invading system
Time Limit: 1000ms
Memory Limit: 65536KB
This problem will be judged on WHU. Original ID:
1469
64-bit integer IO format: %lld Java class name: Main
64-bit integer IO format: %lld Java class name: Main
Font Size:
Type: None Graph Theory 2-SAT Articulation/Bridge/Biconnected Component Cycles/Topological Sorting/Strongly Connected Component Shortest Path Bellman Ford Dijkstra/Floyd Warshall Euler Trail/Circuit Heavy-Light Decomposition Minimum Spanning Tree Stable Marriage Problem Trees Directed Minimum Spanning Tree Flow/Matching Graph Matching Bipartite Matching Hopcroft–Karp Bipartite Matching Weighted Bipartite Matching/Hungarian Algorithm Flow Max Flow/Min Cut Min Cost Max Flow DFS-like Backtracking with Pruning/Branch and Bound Basic Recursion IDA* Search Parsing/Grammar Breadth First Search/Depth First Search Advanced Search Techniques Binary Search/Bisection Ternary Search Geometry Basic Geometry Computational Geometry Convex Hull Pick's Theorem Game Theory Green Hackenbush/Colon Principle/Fusion Principle Nim Sprague-Grundy Number Matrix Gaussian Elimination Matrix Exponentiation Data Structures Basic Data Structures Binary Indexed Tree Binary Search Tree Hashing Orthogonal Range Search Range Minimum Query/Lowest Common Ancestor Segment Tree/Interval Tree Trie Tree Sorting Disjoint Set String Aho Corasick Knuth-Morris-Pratt Suffix Array/Suffix Tree Math Basic Math Big Integer Arithmetic Number Theory Chinese Remainder Theorem Extended Euclid Inclusion/Exclusion Modular Arithmetic Combinatorics Group Theory/Burnside's lemma Counting Probability/Expected Value Others Tricky Hardest Unusual Brute Force Implementation Constructive Algorithms Two Pointer Bitmask Beginner Discrete Logarithm/Shank's Baby-step Giant-step Algorithm Greedy Divide and Conquer Dynamic Programming
Eyelids is a student in WHU, at the same time he is a hacker. Shortly after the end of the examination, he was surprised to find he had failed in advanced mathematics, so he decisively invaded School Academic Department website. In the process of invasion, he found that the contents related to the score is an encrypted file, this file consisted by n numbers, after analysis, he find the password of the encrypted file was the number whose binary form has the least 1 among these n numbers. However, as there are too many numbers, Eyelids hopes you can help him decipher the password, to change the score before the results will be finally announced.
Input
The first line is an integer T indicates the number of test cases.
Input contains multiple test cases.
For each test case, the first line has a number n (1<=n<=10^5), showing the number of the numbers. The second line contains n integers separated by blank, showing the n numbers in the file (1<=ai<=10^9).
Input contains multiple test cases.
For each test case, the first line has a number n (1<=n<=10^5), showing the number of the numbers. The second line contains n integers separated by blank, showing the n numbers in the file (1<=ai<=10^9).
Output
For each set of data, output the case number first, then the key in a line. If there are more than one number are eligible, output the smallest one.
Sample Input
1
5
3 2 4 5 6
5
3 2 4 5 6
Sample Output
Case 1: 2
Source
题目大意: 求给出的n个数中,找出化为二进制数状态时含有1个数最少的那个数,如果多个数同时最少,输出最小的那个。
解题思路:
位运算 计算出每个数1的个数 进行比较
注意: 在多个数同时最少是输出最小的数的处理。
1 #include<stdio.h> 2 #include<string.h> 3 unsigned int data[100010]; 4 int s[100010]; //存放每个数的1的个数 5 int BitCount(unsigned int n) 6 { 7 unsigned int c = 0 ; //计数器 8 while (n>0) 9 { 10 if((n&1)==1) //当前位为1 11 ++c ; //计数器+1 12 n >>= 1 ; //向右移位 13 } 14 return c ; 15 } 16 int main() 17 { 18 int t,n,ans,savei,i,c; 19 while(scanf("%d",&t)!=EOF) 20 { 21 c=0; 22 while(t--) 23 { 24 memset(data,0,sizeof(data)); 25 memset(s,0,sizeof(s)); 26 scanf("%d",&n); 27 for(i=0;i<n;i++) 28 { 29 scanf("%d",&data[i]); 30 s[i]=BitCount(data[i]); 31 } 32 ans=s[0]; 33 savei=0; 34 for(i=0;i<n-1;i++) 35 { 36 if(ans>s[i+1]) 37 { ans=s[i+1];savei=i+1; } 38 else if(ans==s[i+1]) //当两个数的1的个数相等时 取值较小的那个。 39 { 40 if(data[savei]>data[i+1]) 41 savei=i+1; 42 } 43 } 44 printf("Case %d: %d\n",++c,data[savei]); 45 } 46 } 47 return 0; 48 }