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. 怎样使用projectproperty sheet(.vsprops)来管理工程

    怎样使用projectproperty sheet(.vsprops)来管理工程 IDE:VS2005 前言 Project Property Sheet的意思是项目属性表,在大型项目中基本上都会使用 ...

  2. CSS3 Media Queries 简介

    原文链接:Introduction to CSS3 Media Queries 原文日期: 2014年2月21日 翻译日期: 2014年2月26日 翻译人员: 铁锚 简介 随着移动设备的日益普及,we ...

  3. 关于App启动加载广告页面思路

    需求 很多app(如淘宝.美团等)在启动图加载完毕后,还会显示几秒的广告,一般都有个跳过按钮可以跳过这个广告,有的app在点击广告页之后还会进入一个广告页面,点击返回进入首页.虽然说这个广告页面对用户 ...

  4. Xcode两种调试小技巧

    1."全局"断点 正常情况下如果代码有错误,会直接触发SIGXXXX信号,然后中断在main函数里. 但是我们还是不知道到底是什么引发了异常信号.我们可以在断点导航器中添加一个全局 ...

  5. Android Camera开发系列(上)——Camera的基本调用与实现拍照功能以及获取拍照图片加载大图片

    Android Camera开发系列(上)--Camera的基本调用与实现拍照功能以及获取拍照图片加载大图片 最近也是在搞个破相机,兼容性那叫一个不忍直视啊,于是自己翻阅了一些基本的资料,自己实现了一 ...

  6. 不要在#include中使用".."

    按照Google C++风格,不应该在#include中使用点号和双点号. 例如:project/scr/base/logging.h 应该这样包含: #include "base/logg ...

  7. C语言实现字符串中(10进制和16进制)转成十进制数

    如何将字符串中的10进制数和16进制数提取出来,看以下代码: #include <stdio.h> typedef char TUINT8 ; typedef int TUINT32; T ...

  8. Leetcode_278_First Bad Version

    本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/49719255 You are a product mana ...

  9. PS 滤镜——扩散特效, 毛玻璃 效果

    %%%  Diffuse %%%  扩散效果    clc; clear all; addpath('E:\PhotoShop Algortihm\Image Processing\PS Algori ...

  10. rails应用ajax之二:使用rails自身支持

    考虑另一种情况: 1. 页面上半部分显示当前的所有用户,页面下半部分是输入新用户的界面: 2. 每当输入新用户时,页面上半部分会动态更新新加用户的内容: 我们还是用ajax实现,不过这次用rails内 ...