当前位置:网站首页>C+每日练题(15)
C+每日练题(15)
2022-06-11 09:24:00 【不知名的新手】
目录
手套
来源:牛客网
描述
在地下室里放着n种颜色的手套,手套分左右手,但是每种颜色的左右手手套个数不一定相同。A先生现在要出门,所以他要去地下室选手套。但是昏暗的灯光让他无法分辨手套的颜色,只能分辨出左右手。所以他会多拿一些手套,然后选出一双颜色相同的左右手手套。现在的问题是,他至少要拿多少只手套(左手加右手),才能保证一定能选出一双颜色相同的手套。
给定颜色种数n(1≤n≤13),同时给定两个长度为n的数组left,right,分别代表每种颜色左右手手套的数量。数据保证左右的手套总数均不超过26,且一定存在至少一种合法方案。
测试样例:
4,[0,7,1,6],[1,5,0,6]返回:10(解释:可以左手手套取2只,右手手套取8只)
题解:
解题:
代码:
class Gloves {
public:
int findMinimum(int n, vector<int> left, vector<int> right) {
// write code here
int left_sum = 0, left_min = INT_MAX;
int right_sum = 0, right_min = INT_MAX;
int sum = 0;
//遍历每一种颜色的左右手套序列
for (int i = 0; i < n; ++i) {
if (left[i] * right[i] == 0) { //对于有0存在的颜色手套,累加
sum += left[i] + right[i];
//对于左右手都有的颜色手套,执行累加-最小值+1
//找到最小值和总数
} else {
left_sum += left[i];
left_min = min(left_min, left[i]);
right_sum += right[i];
right_min = min(right_min, right[i]);
}
}//结果为有左右都有数量的手套序列的结果+有0存在的手套数+最后再加一肯定就能保证了
return sum + min(left_sum - left_min + 1, right_sum - right_min + 1) + 1;
}
};
查找输入整数二进制中1的个数
链接:查找输入整数二进制中1的个数_牛客题霸_牛客网 (nowcoder.com)
来源:牛客网
描述
输入一个正整数,计算它在二进制下的1的个数。
注意多组输入输出!!!!!!
数据范围: 1 \le n \le 2^{31}-1 \1≤n≤231−1
输入描述:
输入一个整数
输出描述:
计算整数二进制中1的个数
示例1
输入:
5
输出:
2
说明:
5的二进制表示是101,有2个1
示例2
输入:
0
输出:
0
题解:
代码:
#include <iostream>
using namespace std;
int Count(size_t n) {
int count = 0;
while (n) {
n &= n - 1;
count++;
}
return count;
}
int main() {
size_t n;
int count = 0;
while (cin >> n) {
count = Count(n);
cout << count << endl;
}
return 0;
}边栏推荐
- Redis transaction details
- Flask (II) - route
- 报错[DetectionNetwork(1)][warning]Network compiled for 6 shaves,maximum available 10,compiling for 5 s
- Product list display
- 关于原型及原型链
- Oracle 11g RAC disk group has space and cannot add data files?
- [ROS] noedic moveit installation and UR5 model import
- Device = depthai Device(““, False) TypeError: _init_(): incompatible constructor arguments.
- Day39 process object and other method mutexes
- Comparison and introduction of OpenCV oak cameras
猜你喜欢

Day45 storage engine data type integer floating point character type date type enumeration and set type constraints table to table relationships

Machine learning notes - in depth Learning Skills Checklist

Oracle 11g RAC disk group has space and cannot add data files?
![报错[DetectionNetwork(1)][warning]Network compiled for 6 shaves,maximum available 10,compiling for 5 s](/img/54/f42146ae649836fe7070ac90f2160e.png)
报错[DetectionNetwork(1)][warning]Network compiled for 6 shaves,maximum available 10,compiling for 5 s
![12.5 concurrent search + violent DFS - [discovery ring]](/img/bb/f6cde1bd253c75106251acf1ff3615.jpg)
12.5 concurrent search + violent DFS - [discovery ring]

ORA-00059 超过db_files限制

How do we connect to WiFi?

Openstack explanation (22) -- neutron plug-in configuration

报错Version mismatch between installed depthai lib and the required one by the scrip.

About prototype and prototype chain
随机推荐
[TiO websocket] v. TiO websocket server counts the number of online people
1400. construct K palindrome strings
Output image is bigger (1228800b) than maximum frame size specified in properties (1048576b)
RAC expdp export error: ora-31693, ora-31617, ora-19505
An error will be reported when the RAC modifies the scanip to different network segments
2022 must have Chrome extension - browser plug-in to double your Internet efficiency
OpenSSL usage
Package details
Zhiyun health submitted the statement to HKEx again: the loss in 2021 exceeded 4billion yuan, an increase of 43% year-on-year
Development of PCBA circuit board for small oxygen generator
Suffix Array
「INS-30131」 安装程序验证所需的初始设置失败
Type-C docking station adaptive power supply patent protection case
Exclusive interview - dialogue on open source Zhai Jia: excellent open source projects should be seen by more people. I am honored to participate in them
Machine learning notes - the story of master kaggle Janio Martinez Bachmann
ESP8266_ Connect to Alibaba cloud through mqtt protocol
OpenCV CEO教你用OAK(四):创建复杂的管道
Telecommuting best practices and Strategies
[scheme development] scheme of infrared thermometer
ES6新增特性--箭头函数