✅ 主要成分 map 实现
This commit is contained in:
		
							
								
								
									
										33
									
								
								take-most-space-element/main.cc
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										33
									
								
								take-most-space-element/main.cc
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,33 @@ | ||||
| #include <unordered_map> | ||||
| using namespace std; | ||||
|  | ||||
| int read() { | ||||
|   int x = 0, w = 1; | ||||
|   char ch = 0; | ||||
|   while (ch < '0' || ch > '9') { | ||||
|     if (ch == '-') | ||||
|       w = -1; | ||||
|     ch = getchar(); | ||||
|   } | ||||
|   while (ch >= '0' && ch <= '9') { | ||||
|     x = x * 10 + (ch - '0'); | ||||
|     ch = getchar(); | ||||
|   } | ||||
|   return x * w; | ||||
| } | ||||
|  | ||||
| int main() { | ||||
|   int n = read(); | ||||
|   unordered_map<int, int> u; | ||||
|   for (int i = 0; i < n; i++) { | ||||
|     int val = read(); | ||||
|     u[val]++; | ||||
|   } | ||||
|   for (const auto &[key, value] : u) { | ||||
|     if (value >= n / 2) { | ||||
|       printf("%d\n", key); | ||||
|       return 0; | ||||
|     } | ||||
|   } | ||||
|   printf("No\n"); | ||||
| } | ||||
		Reference in New Issue
	
	Block a user