当前位置:网站首页>给定正整数N、M,均介于1~10 ^ 9之间,N <= M,找出两者之间(含N、M)的位数为偶数的数有多少个
给定正整数N、M,均介于1~10 ^ 9之间,N <= M,找出两者之间(含N、M)的位数为偶数的数有多少个
2022-07-28 15:58:00 【从一而终】
/*******************************************************
给定正整数N、M,均介于1~10 ^ 9之间,N <= M,找出两者之间(含N、M)的位数为偶数的数有多少个。
测试用例:
输入:N = 1, M = 100
输出:90
样例说明:[1, 100]区间上,有10~99元素的位数是2位,是偶数,这些整数的数量为90。
输入:N = 1, M = 1000
输出:91
********************************************************/
#include <iostream>
#include <cmath>
#include <vector>
#define _DEBUG
class Solution
{
public:
std::vector<std::pair<int, int> > m_vpDict;
// 初始化一个字典
void initDict(int bitNum)
{
for (int i = 0; i < bitNum; i++)
{
m_vpDict.push_back(std::pair<int, int>(pow(10, i), pow(10, i + 1) - 1));
}
}
// 判断一个数是几位数
int bitNum(int value)
{
if (value == 0)
{
return 0;
}
return 1 + bitNum( value / 10);
}
// 判断区间偶数个数
int mostDouble(int start, int end)
{
int ret = 0;
int startBits = bitNum(start);
int endBits = bitNum(end);
// 边界条件
if (endBits - startBits == 0)
{
if (startBits % 2 != 0)
{
return 0;
}
else
{
return end - start + 1;
}
}
if (startBits % 2 == 0)
{
ret += m_vpDict[startBits - 1].second - start + 1;
startBits += 2;
for (int i = startBits + 2; i < endBits; i += 2)
{
ret += 9 * pow(10, i);
}
if (endBits % 2 == 0)
{
ret += end - m_vpDict[endBits - 1].first + 1;
}
}
else
{
startBits += 1;
for (int i = startBits; i < endBits; i += 2)
{
ret += 9 * pow(10, i - 1);
}
if (endBits % 2 == 0)
{
ret += end - m_vpDict[endBits - 1].first + 1;
}
}
return ret;
}
};
int main()
{
Solution solution;
// 字典初始化
solution.initDict(9);
#ifdef DEBUG
// 测试字典初始化
for (auto iter : solution.m_vpDict)
{
std::cout << iter.first << " " << iter.second << std::endl;
}
// 测试bitNum函数
std::cout << solution.bitNum(1254785) << std::endl;
#endif // DEBUG
std::cout << solution.mostDouble(50, 80) << std::endl;
return 0;
}边栏推荐
- QT packaging
- PHP about problems such as memory overflow timeout caused by large amount of data exporting or traversing data
- egg(十九):使用egg-redis性能优化,缓存数据提升响应效率
- USB产品(FX3、CCG3PA)的调试方法
- Abaqus GUI界面解决中文乱码问题(插件中文乱码也适用)
- 微软:Edge 浏览器已内置磁盘缓存压缩技术,可节省空间占用且不降低系统性能
- asp.net大文件分块上传断点续传demo
- 小程序:scroll-view默认滑倒最下面
- Sort 1-insert sort and Hill sort
- Kubeedge releases white paper on cloud native edge computing threat model and security protection technology
猜你喜欢

HyperMesh运行脚本文件的几种方法

Optimization of network request success rate in IM instant messaging software development

栈的介绍与实现(详解)

Interesting kotlin 0x0a:fun with composition

快速掌握 Kotlin 集合函数

Configure HyperMesh secondary development environment on vs Code

有趣的 Kotlin 0x09:Extensions are resolved statically

Introduction and implementation of stack (detailed explanation)

排序3-选择排序与归并排序(递归实现+非递归实现)

Use js direct OSS to store files in Alibaba cloud and solve the limitation of large file upload server
随机推荐
ANSA二次开发 - 抽中面的两种方法
2021-04-02
QT designer for QT learning
Interesting kotlin 0x07:composition
nowcode-学会删除链表中重复元素两题(详解)
有趣的 Kotlin 0x06:List minus list
关于 CMS 垃圾回收器,你真的懂了吗?
First day of QT study
排序2-冒泡排序与快速排序(递归加非递归讲解)
Redis系列4:高可用之Sentinel(哨兵模式)
日常开发方案设计指北
redis源码优化--绑核
ABAQUS GUI interface solves the problem of Chinese garbled code (plug-in Chinese garbled code is also applicable)
Ansa secondary development - Introduction to interface development tools
Sort 3-select sort and merge sort (recursive implementation + non recursive implementation)
Reset grafana login password to default password
Debugging methods of USB products (fx3, ccg3pa)
Solve the width overflow of rich text pictures such as uniapp
WSL+Valgrind+Clion
Implementation of paging