1049. Counting Ones (30)

时间限制
100 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

The task is simple: given any positive integer N, you are supposed to count the total number of 1's in the decimal form of the integers from 1 to N. For example, given N being 12, there are five 1's in 1, 10, 11, and 12.

Input Specification:

Each input file contains one test case which gives the positive N (<=230).

Output Specification:

For each test case, print the number of 1's in one line.

Sample Input:

12

Sample Output:

5

思路
给一个数,计算所有小于等于这个数的数字中的1的个数和。
找规律题,计算每一位对应的1的个数,然后相加,每一位的1的计算情况分三种情况:
1.如果当前位数字为0,那么该位的1的个数由更高位的数字确定。比如2120,个位为1的个数为212 * 1 = 212(个位的单位为1)。
2.如果当前位数字为1,那么该位的1的个数不但由高位决定,还由低位数字决定。比如2120百位为1,那么百位数字1的个数为2 * 100 + 20 + 1 = 221个(百位的单位为100)。
3.如果当前位数字大于1,那么该位数字1的个数为(高位数+ 1) * 位数单位。比如2120十位为2,那么十位数字1的个数为(21 + 1) * 10 = 220个(十位的单位为10)
4.继续按照上文,2120千位为2,那么千位为1的个数为(0 + 1)*1000 = 1000
5.综上2120以内的所有数字中出现1的个数为1653个。 代码
#include<iostream>
using namespace std; int CountOnes(int n)
{
int factor = 1,lownum = 0,highnum = 0,cur = 0,countones = 0;
while(n/factor)
{
highnum = n/(factor*10);
lownum = n - (n/factor)*factor;
int cur = (n/factor) % 10;
if(cur == 0)
{
countones += highnum * factor;
}
else if (cur == 1)
{
countones += highnum * factor + lownum + 1;
}
else
{
countones += ( highnum + 1) * factor;
}
factor *= 10;
}
return countones;
} int main()
{
int n;
while(cin >> n)
{
cout << CountOnes(n);
}
}

  

PAT1049:Counting Ones的更多相关文章

  1. pat1049. Counting Ones (30)

    1049. Counting Ones (30) 时间限制 10 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The task ...

  2. 萌新笔记——Cardinality Estimation算法学习(二)(Linear Counting算法、最大似然估计(MLE))

    在上篇,我了解了基数的基本概念,现在进入Linear Counting算法的学习. 理解颇浅,还请大神指点! http://blog.codinglabs.org/articles/algorithm ...

  3. POJ_2386 Lake Counting (dfs 错了一个负号找了一上午)

    来之不易的2017第一发ac http://poj.org/problem?id=2386 Lake Counting Time Limit: 1000MS   Memory Limit: 65536 ...

  4. ZOJ3944 People Counting ZOJ3939 The Lucky Week (模拟)

    ZOJ3944 People Counting ZOJ3939 The Lucky Week 1.PeopleConting 题意:照片上有很多个人,用矩阵里的字符表示.一个人如下: .O. /|\ ...

  5. find out the neighbouring max D_value by counting sort in stack

    #include <stdio.h> #include <malloc.h> #define MAX_STACK 10 ; // define the node of stac ...

  6. 1004. Counting Leaves (30)

    1004. Counting Leaves (30)   A family hierarchy is usually presented by a pedigree tree. Your job is ...

  7. 6.Counting Point Mutations

    Problem Figure 2. The Hamming distance between these two strings is 7. Mismatched symbols are colore ...

  8. 1.Counting DNA Nucleotides

    Problem A string is simply an ordered collection of symbols selected from some alphabet and formed i ...

  9. uva 11401 Triangle Counting

    // uva 11401 Triangle Counting // // 题目大意: // // 求n范围内,任意选三个不同的数,能组成三角形的个数 // // 解题方法: // // 我们设三角巷的 ...

随机推荐

  1. 【一天一道LeetCode】#5 Longest Palindromic Substring

    一天一道LeetCode系列 (一)题目 Given a string S, find the longest palindromic substring in S. You may assume t ...

  2. windows linux—unix 跨平台通信集成控制系统----文件搜索

    跨平台的网络通信,跟设备的集成控制,牵扯到在各种平台下的文件搜索问题,windows下面的已经有了. 地址如下: http://blog.csdn.net/wangyaninglm/article/d ...

  3. ITU-T Technical Paper: QoS 的参数(非常的全,共计88个)

    本文翻译自ITU-T的Technical Paper:<How to increase QoS/QoE of IP-based platform(s) to regionally agreed ...

  4. CMake添加-D_DEBUG宏

    Linux下Debug版不会自动添加 _DEBUG宏,只有NDEBUG宏可用. cmake ../src _DCMAKE_BUILD_TYPE=Debug -D_DEBUG 会报错: -D_DEBUG ...

  5. objective-c如何在linux下进入Modern模式

    自从apple的obj-c进入2.0后,出现了相对于Legacy模式的Modern模式:Modern模式中出现了一些高级功能(比如ARC),并且出现了一些新的字面语法,新旧模式的差别可以参考apple ...

  6. Xcode使用心得02:如何在项目中关闭ARC特性

    在obj-c系列内存管理的博文里大家应该对ARC有所了解,一般是不推荐关闭ARC特性的,但你也保不齐啥时候有这个需求,于是乎我们看看在最新的x6b中如何将其关闭吧. 因为Build Seting里的子 ...

  7. python字符串27种常见的方法

    如有字符串 mystr = 'hello world itcast and itcastcpp' ,以下是常见的操作: <1>find 检测 str 是否包含在 mystr中,如果是返回开 ...

  8. dom4j 解析 xml标签属性

    重写onEnd()和onStart()方法 public class XmlElementHandler implements ElementHandler { @Override public vo ...

  9. JavaScript中将对象数组中的某个属性值,批量替换成另一个数值

    原文链接 https://segmentfault.com/q/1010000010352622 希望将下列数组中的sh替换成沪,sz替换成深 var stooges = [ {label:1,val ...

  10. 重构:以Java POI 导出EXCEL为例

    重构 开头先抛出几个问题吧,这几个问题也是<重构:改善既有代码的设计>这本书第2章的问题. 什么是重构? 为什么要重构? 什么时候要重构? 接下来就从这几个问题出发,通过这几个问题来系统的 ...