求从1到n整数中1出现的次数:O(logn)算法
来源:互联网 发布:淘宝自带erp系统 编辑:程序博客网 时间:2024/05/20 13:37
剑指offer上的一题,但是感觉这位兄弟的解法更好
#include<iostream>using namespace std;#define N 2000int cnt(int n){ if (n < 1) { return 0; } int count = 0; int base = 1; int round = n; while (round > 0) { int weight = round % 10; round /= 10; count += round * base; if (weight == 1) { count += (n % base) + 1; } else if (weight > 1) { count += base; } base *= 10; } return count;}int main(){ int n; cin >> n; cout << cnt(n) << endl; return 0;}
阅读全文
0 0
- 求从1到n整数中1出现的次数:O(logn)算法
- 从1到n整数中1出现的次数:O(logn)算法
- 从1到n整数中1出现的次数:O(logn)算法
- 从1到n整数中1出现的次数:O(logn)算法
- 从1到n整数中1出现的次数:O(logn)算法
- 从1到n整数中1出现的次数:O(logn)算法
- 从1到n整数中1出现的次数:O(logn)算法
- 从1到n整数中1出现的次数:O(logn)算法
- 从1到n整数中1出现的次数:O(logn)算法
- 从1到n整数中1出现的次数:O(logn)算法
- 从1到n整数中1出现的次数:O(logn)算法
- 整数中1出现的次数(从1到n整数中1出现的次数)(算法)
- 每天学习一算法系列(28)(输入一个整数n,求从1 到n 这n 个整数的十进制表示中1 出现的次数)
- 经典算法: 输入一个整数n,求从1到n这n个整数的十进制表示中1出现的次数
- 剑指offer面试题 求从1到n整数中1出现的次数
- 整数中1出现的次数(从1到n整数中1出现的次数)
- 从1到n整数中1出现的次数
- 从1到n整数中1出现的次数
- 天天学Linux命令31--chown命令
- junittest中出现的initializationError
- 3-5 学生成绩统计
- letex(winedit)公式和正文的间距,超级简单(以后别踩坑)
- 自学C语言第一个程序
- 求从1到n整数中1出现的次数:O(logn)算法
- Qt中定时器使用的两种方法
- Delphi-DrawTextEx
- Java多线程系列--“JUC集合”10之 ConcurrentLinkedQueue
- 类
- 未来五年最具前景的一门编程语言是什么?
- 你的工资基础决定你的前途
- 《Learning RabbitMQ》阅读笔记(1.简介)
- Linux的SOCKET编程详解