当前位置:网站首页>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);
}
};边栏推荐
- . Solution to the problem of Chinese garbled code when net core reads files
- 士官类学校名录
- 【LeetCode】977-有序數組的平方
- Evaluation of embedded rz/g2l processor core board and development board of Feiling
- 19_ Redis_ Manually configure the host after downtime
- [leetcode] 876 intermediate node of linked list
- 百变大7座,五菱佳辰产品力出众,人性化大空间,关键价格真香
- SQL transaction
- 08_ strand
- MySQL calculate n-day retention rate
猜你喜欢

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

How to find a sense of career direction

Bing.com网站

Basic knowledge of cryptography

Data analysis thinking analysis methods and business knowledge - business indicators

Bing.com網站

LeetCode刷题——验证二叉树的前序序列化#331#Medium

03.golang初步使用

List set & UML diagram

07_ Hash
随机推荐
[leetcode] 1162 map analysis
6095. 强密码检验器 II
【LeetCode】1905-统计子岛屿
Wechat Alipay account system and payment interface business process
MySQL calculate n-day retention rate
高考录取分数线爬虫
让您的HMI更具优势,FET-G2LD-C核心板是个好选择
FPGA - 7系列 FPGA内部结构之Clocking -03- 时钟管理模块(CMT)
Storage read-write speed and network measurement based on rz/g2l | ok-g2ld-c development board
College entrance examination admission score line climbing
Case introduction and problem analysis of microservice
4. Data splitting of Flink real-time project
[leetcode] 977 - carré du tableau ordonné
[leetcode] 486 predict winners
[leetcode] 417 - Pacific Atlantic current problem
Semantic segmentation learning notes (1)
Pytoch saves tensor to Mat file
04.进入云原生后的企业级应用构建的一些思考
Bing. Site Internet
高考录取分数线爬取