当前位置:网站首页>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;
}边栏推荐
- RAC单独修改scanip到不同网段时会报错
- ESP8266_MQTT协议
- Augmented reality experiment IV of Shandong University
- [intelligent development] scheme design and hardware development of sphygmomanometer
- Zhiyun health submitted the statement to HKEx again: the loss in 2021 exceeded 4billion yuan, an increase of 43% year-on-year
- OpenCV CEO教你用OAK(四):创建复杂的管道
- Package details
- Simple recommendation based on Euclidean distance
- Suffix Array
- affair
猜你喜欢

ESP8266_GET请求天气预报、json解析

Machine learning notes - in depth Learning Skills Checklist

ESP8266_通过MQTT协议连接阿里云

Output image is bigger (1228800b) than maximum frame size specified in properties (1048576b)

OpenCV OAK相机对比及介绍

ORACLE RAC中连接ScanIP报错ORA-12545的问题解决

RAC单独修改scanip到不同网段时会报错

「INS-30131」 安装程序验证所需的初始设置失败

Understanding of the keyword this in JS

PD chip ga670-10 for OTG while charging
随机推荐
P4147 "jade toad Palace"
js中关键字this的理解
[ROS] noedic moveit installation and UR5 model import
Modularnotfounderror: no module named 'find_ version’
Where is it safer to open an account for soda ash futures? How much do you need to buy at least?
Type-C docking station adaptive power supply patent protection case
ESP8266_通过MQTT协议连接阿里云
ESP8266_MQTT协议
About prototype and prototype chain
MySQL:Got a packet bigger than ‘max_ allowed_ packet‘ bytes
Rebuilding Oracle XdB components
ESP8266_ SmartConfig
Output image is bigger (1228800b) than maximum frame size specified in properties (1048576b)
Esp8266 Smartconfig
The first TOF related data set available for deep learning: deep learning for confidence information in stereo and TOF data fusion (iccv 2017)
Oracle XDB組件的重建
Use of MSF evaluation module
How do we connect to WiFi?
ESP8266_SmartConfig
整型提升例题