找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 ...
随机推荐
- Java for LeetCode 150 Evaluate Reverse Polish Notation
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- 【JAVA、C++】LeetCode 012 Integer to Roman
Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...
- codeforces 488A. Giga Tower 解题报告
题目链接:http://codeforces.com/problemset/problem/488/A 题目意思:给出一个数a,范围是[-10^9, 10^9],问它最少需要加的一个正整数 b 是多少 ...
- ext树表
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAA2UAAAHwCAIAAACpIFDdAAAgAElEQVR4nOy9f5Qb5ZnvWWQZlnO5Oc ...
- Light OJ 1253 Misere Nim (尼姆博弈(2))
LightOJ1253 :Misere Nim 时间限制:1000MS 内存限制:32768KByte 64位IO格式:%lld & %llu 描述 Alice and Bob ar ...
- POJ 3904 Sky Code (容斥原理)
B - Sky Code Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit ...
- Android 设置旋转朝向
在AndroidMenifest.xml中activity标签中,添加一个属性 android:screenOrientation="landscape"
- CI中PHP写法规范(不断更新)
1.类名首字母大写,多个单词用下划线连接,首字母小写是无效的 举例: class CI_Model 2.routes路由配置中的右侧在配置类名和方法名的时候都是小写,如果大写可能会出现404找不到的错 ...
- CM12同步源码及编译教程
同时提供基于安卓5.0的MKL魔趣猪扒饭编译教程~[玩机组出品]魔趣猪扒饭MKL50.1编译教程http://www.oneplusbbs.com/forum.php?mod=viewthread&a ...
- Codeforces Gym 100187K K. Perpetuum Mobile 构造
K. Perpetuum Mobile Time Limit: 2 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100187/pro ...