当前位置:网站首页>2278. 字母在字符串中的百分比
2278. 字母在字符串中的百分比
2022-07-02 12:10:00 【紫菜(Nori)】
问题:
为什么,当代码中的times变量为float类型是,执行case会得到错误的结果?
class Solution {
public:
int percentageLetter(string s, char letter) {
// 这里使用浮点数,方便进行浮点数的除法运算
/* Note:
如果这里使用float类型将结果装换为int时会导致结果减一
case: times = 59; size = 100,返回58
*/
// 字母出现次数
double times = 0;
for(int i = 0; i < s.size(); i++){
if(s[i] == letter){
times++;
}
}
// 将结果扩大100被再去除小数部分,这里要求向下取证
return (int)((times/s.size())*100);
}
};边栏推荐
- SQL stored procedure
- 彻底弄懂浏览器强缓存和协商缓存
- LeetCode_ Sliding window_ Medium_ 395. Longest substring with at least k repeated characters
- 【LeetCode】486-预测赢家
- 基于RZ/G2L | OK-G2LD-C开发板存储读写速度与网络实测
- MD5加密
- 【LeetCode】344-反转字符串
- [leetcode] 1020 number of enclaves
- [development environment] install the Chinese language pack for the 2013 version of visual studio community (install test agents 2013 | install visual studio 2013 simplified Chinese)
- Be a good gatekeeper on the road of anti epidemic -- infrared thermal imaging temperature detection system based on rk3568
猜你喜欢

Storage read-write speed and network measurement based on rz/g2l | ok-g2ld-c development board

Custom exception

yolo格式数据集处理(xml转txt)

Party History Documentary theme public welfare digital cultural and creative products officially launched

How to avoid 7 common problems in mobile and network availability testing

FPGA - 7系列 FPGA内部结构之Clocking -03- 时钟管理模块(CMT)

SQL transaction

Case introduction and problem analysis of microservice

百变大7座,五菱佳辰产品力出众,人性化大空间,关键价格真香

PTA ladder game exercise set l2-001 inter city emergency rescue
随机推荐
Huffman tree: (1) input each character and its weight (2) construct Huffman tree (3) carry out Huffman coding (4) find hc[i], and get the Huffman coding of each character
Wechat Alipay account system and payment interface business process
SQL transaction
6095. 强密码检验器 II
LeetCode刷题——两整数之和#371#Medium
【LeetCode】200-岛屿数量
There are 7 seats with great variety, Wuling Jiachen has outstanding product power, large humanized space, and the key price is really fragrant
6.12 企业内部upp平台(Unified Process Platform)的关键一刻
Redux - detailed explanation
Solution of Queen n problem
【网络安全】网络资产收集
Leetcode question brushing - parity linked list 328 medium
. Solution to the problem of Chinese garbled code when net core reads files
Equipped with Ti am62x processor, Feiling fet6254-c core board is launched!
搭建自己的语义分割平台deeplabV3+
18_ Redis_ Redis master-slave replication & cluster building
XML Configuration File
Force deduction solution summary 2029 stone game IX
LeetCode_ String_ Simple_ 412.Fizz Buzz
(Video + graphic) machine learning introduction series - Chapter 5 machine learning practice