当前位置:网站首页>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);
}
};
边栏推荐
猜你喜欢
Bing. Site Internet
4. Jctree related knowledge learning
LeetCode刷题——验证二叉树的前序序列化#331#Medium
做好抗“疫”之路的把关人——基于RK3568的红外热成像体温检测系统
. Solution to the problem of Chinese garbled code when net core reads files
终于搞懂了JS中的事件循环,同步/异步,微任务/宏任务,运行机制(附笔试题)
How to intercept the value of a key from the JSON string returned by wechat?
How to choose a third-party software testing organization for automated acceptance testing of mobile applications
飞凌嵌入式RZ/G2L处理器核心板及开发板上手评测
SQL transaction
随机推荐
高考录取分数线爬取
Bing.com网站
【LeetCode】577-反转字符串中的单词 III
【LeetCode】486-预测赢家
FPGA - clock-03-clock management module (CMT) of internal structure of 7 Series FPGA
LeetCode刷题——验证二叉树的前序序列化#331#Medium
10_Redis_geospatial_命令
How to find a sense of career direction
2022 college students in Liaoning Province mathematical modeling a, B, C questions (related papers and model program code online disk download)
The traversal methods of binary tree mainly include: first order traversal, middle order traversal, second order traversal, and hierarchical traversal. First order, middle order, and second order actu
[leetcode] 1020 number of enclaves
【LeetCode】1254-统计封闭岛屿的数量
百变大7座,五菱佳辰产品力出众,人性化大空间,关键价格真香
Force deduction solution summary 2029 stone game IX
How to intercept the value of a key from the JSON string returned by wechat?
MySQL calculate n-day retention rate
2022 年辽宁省大学生数学建模A、B、C题(相关论文及模型程序代码网盘下载)
[leetcode] 417 - Pacific Atlantic current problem
LeetCode刷题——奇偶链表#328#Medium
Force deduction solution summarizes the lucky numbers in 1380 matrix