找1到n所有整数出现1的个数
编程之美2.4
n=12时,1,11,12这3个数包含1,所以1的个数是5.
Line 9是为了防止factor溢出。
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std; int countOne(int n) {
int ans = ;
int factor = ;
while (n / factor >= ) { // avoid overflow
int high = n / factor / ;
int low = n % factor;
int current = (n / factor) % ;
if (current == ) {
ans += high * factor;
} else if (current == ) {
ans += high * factor + low + ;
} else {
ans += (high + ) * factor;
}
factor *= ;
}
if (n / factor == ) ans += (n % factor) + ;
else if (n / factor != ) ans += factor;
return ans;
} int bruteForce(int n) {
int ans = ;
for (int i = ; i <= n; ++i) {
int tmp = i;
while (tmp) {
if (tmp % == ) ans++;
tmp /= ;
}
}
return ans;
} int main() {
srand(time(NULL)); for (int i = ; i < ; ++i) {
int n = rand() % ;
int c1 = countOne(n);
int c2 = bruteForce(n);
if (c1 != c2) {
cout << c1 << " " << c2 << " " << n << endl;
}
}
return ;
}
找1到n所有整数出现1的个数的更多相关文章
- C语言:找出一个大于给定整数m且紧随m的素数,-求出能整除x且不是偶数的数的个数,
//函数fun功能:找出一个大于给定整数m且紧随m的素数,并作为函数值返回. #include <stdlib.h> #include <conio.h> #include & ...
- ZT 计算一个无符整数中1Bit的个数(1) 2010-04-20 10:52:48
计算一个无符整数中1Bit的个数(1) 2010-04-20 10:52:48 分类: C/C++ [转]计算一个无符整数中1Bit的个数(1) Count the number of bits ...
- 输入n个字符串,找出最长最短字符串(若有个数相同的,都打印出来)
首先,要求找到最长最短字符串,我们应该用数组将其存起来,输入的个数是不固定的,我们就可以用Scanner获取要输入的个数,最终找到的个数也不固定,我们可以封装两个方法,并且返回值类型为数组. 我遇到的 ...
- 课题练习——找从1到N出现的1的个数
#include<iostream.h>#include<conio.h>int Sum1(int n){ int count = 0; //记录1的个数 int factor ...
- 找出一堆数中最小的前K个数
描写叙述: 给定一个整数数组.让你从该数组中找出最小的K个数 思路: 最简洁粗暴的方法就是将该数组进行排序,然后取最前面的K个数就可以. 可是,本题要求的仅仅是求出最小的k个数就可以,用排序能够但显然 ...
- 剑指Offer30 从1到n整数出现1的个数
/************************************************************************* > File Name: 30_NumerO ...
- 【剑指offer】找出数组中出现一次的两个数
2013-09-08 10:50:46 一个整型数组中,除了两个数字之外,其他数字都出现了2次,找出这两个只出现一次的数字,要求时间复杂度是O(N),空间复杂度是O(1). 小结: 任何数与0异或,结 ...
- 485. 找出二进制串中连续的1的个数 Max Consecutive Ones
Given a binary array, find the maximum number of consecutive 1s in this array. Example 1: Input: [1, ...
- 从1到n整数中1的个数
[问题]求出1~13的整数中1出现的次数,并算出100~1300的整数中1出现的次数?为此他特别数了一下1~13中包含1的数字有1.10.11.12.13因此共出现6次,但是对于后面问题他就没辙了.A ...
随机推荐
- Controller与View之间的数据传递
1)Controller向View传递数据ViewData["message"] = "Hello";//使用ViewData传递数据ViewBag.Time ...
- C++文件读写详解(ofstream,ifstream,fstream)
C++文件读写详解(ofstream,ifstream,fstream) 这里主要是讨论fstream的内容: #include <fstream> ofstream //文件写操作 内存 ...
- vijos 1025 背包 *
链接:点我 输入顺序又反了 #include<cstdio> #include<iostream> #include<algorithm> #include< ...
- POJ 2482 Stars in Your Window 线段树扫描线
Stars in Your Window Description Fleeting time does not blur my memory of you. Can it really be 4 ...
- OD使用心得
查看堆栈 从返回前面的竖线 到 竖线结束 也就是到 (从返回 msconfig.0065a318) 全看.这样不会丢失些东西 或者 看堆栈 直接将堆栈框拉大 避免有些东西没有看到
- 监控Spark应用方法简介
监控Spark应用有很多种方法. Web接口每一个SparkContext启动一个web UI用来展示应用相关的一些非常有用的信息,默认在4040端口.这些信息包括: 任务和调度状态的列表RDD大小和 ...
- helpDB
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Da ...
- 2016.6.17 kali Linux 隧道工具
隧道工具的基本概念: 1.在计算机网络中,隧道工具是指使用一种网络协议去封装另一种网络协议的技术. 2.通常用来数据伪装或者穿越防火墙,在入侵目标系统后,可用来提升权限和权限维持. Kali中的隧道工 ...
- 空函数有参函数调用参数的注意事项Swift 1.1语言
空函数有参函数调用参数的注意事项Swift 1.1语言 7.2.3 空函数 空函数有参函数调用参数的注意事项Swift 1.1语言空函数是函数中最简单的形式.在空函数中,函数只有一个空壳,里面是没有 ...
- IE6/IE7中display:inline-block解决办法
IE6/IE7下对display:inline-block的支持性不好. 1.inline元素的display属性设置为inline-block时,所有的浏览器都支持: 2.block元素的displ ...