当前位置:网站首页>给定正整数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;
}边栏推荐
- 微信公众号获取素材列表
- CRC16数据校验支持ModelBus和XMODEM校验模式(C语言)
- 【指针内功修炼】字符指针 + 指针数组 + 数组指针 + 指针参数(一)
- Oracle system composition
- Sort 4-heap sort and massive TOPK problem
- 重置grafana登录密码为默认密码
- “蔚来杯“2022牛客暑期多校训练营3 H.Hacker SAM+线段树/DP/分治(不带修查区间最大子段和)
- Multiple commands produce ‘.../xxx.app/Assets.car‘问题
- 排序5-计数排序
- Configure HyperMesh secondary development environment on vs Code
猜你喜欢

Learn to use MySQL explain to execute the plan, and SQL performance tuning is no longer difficult

优化Hypermesh脚本性能的几点建议

MySQL5.7及SQLyogV12安装及使用破解及常用命令

TCP handshake, waving, time wait connection reset and other records

Introduction and implementation of stack (detailed explanation)

What does it remote operation and maintenance mean? Which is the best remote operation and maintenance software?

队列的介绍与实现(详解)

HM secondary development - data names and its use

Sort 4-heap sort and massive TOPK problem

Interesting kotlin 0x06:list minus list
随机推荐
【从零开始学习SLAM】将坐标系变换关系发布到 topic tf
Signal shielding and processing
Early in the morning, pay Bora SMS to say that you won the "prize"? Dealing with server mining virus - kthreaddi
Brother Ali teaches you how to correctly understand the problem of standard IO buffer
Redis source code optimization -- binding core
遭MQ连连干翻后的醒悟!含恨码出这份MQ手册助力秋招之旅
LwIP development | socket | UDP
Li Hongyi, machine learning 4. Deep learning
ANSA二次开发 - 在PyCharm上搭建ANSA/META二次开发环境
栈的介绍与实现(详解)
Splash (rendering JS service) introduction installation
WSL+Valgrind+Clion
在abaqus中使用PyQt设计GUI
Cluster construction and use of redis5
Wake up after being repeatedly upset by MQ! Hate code out this MQ manual to help the journey of autumn recruitment
Learn ABAQUS script programming script in an hour
排序4-堆排序与海量TopK问题
后台弹出layer提示
[pointer internal skill cultivation] character pointer + pointer array + array pointer + pointer parameter (I)
QT designer for QT learning