1049. Counting Ones 解析

本文提供了一种高效算法来计算从1到任意整数N中数字1出现的总次数,并附带了一个C++实现示例。通过巧妙地划分高位、当前位置和低位三部分,该算法避免了暴力求解所带来的效率问题。

题目是要求数1-N的数字里面一共有多少个数字1,别理解成含有1的数字的个数了。

如果有《编程之美》这本书可以看看里面的介绍。讲的不错的。

我这里就贴两个图大概说下规律。佩服总结出这个规律的人。。。




#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <map>
#include <queue>
#include <stack>
#include <cstring>

using namespace std;

long long n,ans=0;

int main(){
	
	scanf("%lld",&n);

	long long pre,now,low;
	long long fac = 1;

	while(n/fac!=0){
		low = n % fac;
		pre = n / (10* fac);
		now = n / fac %10;
		if(now == 0)
			ans+=pre*fac;
		else if(now == 1)
			ans+=pre*fac+low+1;
		else
			ans += ((pre+1)*fac);
		fac*=10;
	}
	cout << ans << endl;
	
	return 0 ;
}


E:\apache开源\commons-lang>ort analyze -i . -o ort-output Hoplite is configured to infer which sealed type to choose by inspecting the config values at runtime. This behaviour is now deprecated in favour of explicitly specifying the type through a discriminator field. In 3.0 this new behavior will become the default. To enable this behavior now (and disable this warning), invoke withExplicitSealedTypes() on the ConfigLoaderBuilder. ______________________________ / \_______ \__ ___/ The OSS Review Toolkit, version 69.0.0, | | | | _/ | | built with JDK 21.0.8+9-LTS, running under Java 21.0.7. | | | | | \ | | Executing 'analyze' as 'ASUS' on Windows 11 \________/ |____|___/ |____| with 32 CPUs and a maximum of 4002 MiB of memory. Environment variables: USERPROFILE = C:\Users\ASUS OS = Windows_NT COMSPEC = C:\WINDOWS\system32\cmd.exe JAVA_HOME = E:\jdk Looking for ORT configuration in the following file: C:\Users\ASUS\.ort\config\config.yml (does not exist) Looking for analyzer-specific configuration in the following files and directories: E:\apache开源\commons-lang\.ort.yml (does not exist) C:\Users\ASUS\.ort\config\resolutions.yml (does not exist) The following 26 package manager(s) are enabled: Bazel, Bower, Bundler, Cargo, Carthage, CocoaPods, Composer, Conan, GoMod, Gradle Inspector, Maven, NPM, NuGet, PIP, Pipenv, PNPM, Poetry, Pub, SBT, SpdxDocumentFile, Stack, Swift Package Manager, Tycho, Unmanaged, Yarn, Yarn 2+ The following 2 package curation provider(s) are enabled: DefaultDir, DefaultFile Analyzing project path: E:\apache开源\commons-lang Found 1 Maven definition file(s) at: pom.xml Found in total 1 definition file(s) from the following 1 package manager(s): Maven Wrote analyzer result to 'E:\apache开源\commons-lang\ort-output\analyzer-result.yml' (0.03 MiB) in 962.845300ms. The analysis took 3m 43.401327100s. Found 1 project(s) and 21 package(s) in total (not counting excluded ones). Applied 0 curation(s) from 0 of 2 provider(s). Resolved issues: 0 errors, 0 warnings, 0 hints. Unresolved issues: 0 errors, 0 warnings, 0 hints.
最新发布
10-01
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值