当前位置: 首页 > news >正文

PAT 1049 Counting Ones

在这里插入图片描述
这一题要求从1到N所有数中有多少个1,可以直接暴力写,但这样不能满分。

#include <iostream>
#include <limits.h>
#include <cstring>
#include <queue>
#include <unordered_map>
#include <algorithm>
#include <cmath>
#include <vector>
using namespace std;long long N;
long long cnt;
// 1
// 20
// 300
// 4000
// 50000
int main()
{ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);cin>>N;string s=to_string(N);int wei=s.size()-1;long long ans=wei*pow(10,wei-1);long long start=1*pow(10,s.size()-1);for(long long i=start;i<=N;i++){long long x=i;while(x!=0){if(x%10==1){ans++;}x/=10;}}cout<<ans<<endl;return 0;} 

正确做法是数学题,
统计每一位中1出现的次数,然后把它们累加,就是最后的ans,
那么如何找到每一位出现的次数呢?
我们需要找到这一位高位high=?,当前位cut的数字是多少,还有低位low是多少,这些都是影响该位1出现的次数的因素
举一个例子: N=32516,我们找它百位上出现1的次数
cut=5,high=32,low=16,
首先考虑高位对cut的贡献,因为从0-32516,不考虑低位,只考虑高位,那么高位有0-31共32个选择,也即当high=0-31时,百位出现1的次数为32100,(因为有0-31,每一个数,对应百位及以下有100-199共100个),又因为cut=5,所以当高位high=32,<32的情况已经讨论过了,存在32100-32199共100个,所以百位出现1的次数的结果就是high100+100。其他位是同理的,因此我们可以得出代码:

#include <iostream>
#include <limits.h>
#include <cstring>
#include <queue>
#include <unordered_map>
#include <algorithm>
#include <cmath>
#include <vector>
using namespace std;long long N;
int wei=1;//表示当前处于的位
int ans;
int main()
{ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);cin>>N;string s=to_string(N);for(int i=0;i<s.size();i++){int high=N/(wei*10);int cut=N/wei%10;int low=N%wei;if(cut==0){ans+=high*wei;} else if(cut==1){ans+=high*wei+low+1;}else{ans+=high*wei+wei;}wei*=10;} cout<<ans;return 0;} 

时间复杂度为 log2(n)n为数字的长度。
按位统计法值得掌握。

http://www.xdnf.cn/news/1156231.html

相关文章:

  • 医学图像超分辨率重建深度学习模型开发报告
  • 如何用immich将苹果手机中的照片备份到指定文件夹
  • Word for mac使用宏
  • UniApp 常用UI库
  • 机器视觉---深度图像存储格式
  • 闲庭信步使用图像验证平台加速FPGA的开发:第二十五课——正弦波图像的FPGA实现
  • 数据存储方案h5py
  • 【C++基础】面试高频考点解析:extern “C“ 的链接陷阱与真题实战
  • MySQL详解三
  • MyBatis Plus高效开发指南
  • 第459场周赛
  • ESXi6.7硬件传感器红色警示信息
  • 详解Mysql解决深分页方案
  • Python类中方法种类与修饰符详解:从基础到实战
  • [simdjson] ondemand::value | object array
  • 低速信号设计之I3C篇
  • 嵌入式Linux:获取线程ID
  • gym 安装
  • PrimeTime:高级片上变化(AOCV)
  • Laravel 框架NOAUTH Authentication required 错误解决方案-优雅草卓伊凡
  • 分享如何在保证画质的前提下缩小视频体积实用方案
  • NISP-PTE基础实操——XSS
  • MybatisPlus-14.扩展功能-DB静态工具-练习
  • windows + phpstorm 2024 + phpstudy 8 + php7.3 + thinkphp6 配置xdebug调试
  • MySQL学习----Explain
  • Kubernetes (K8S)知识详解
  • 二阶 IIR(biquad)滤波器
  • 红宝书单词学习笔记 list 51-75
  • Product Hunt 每日热榜 | 2025-07-20
  • 【c++】200*200 01灰度矩阵求所有的连通区域坐标集合