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
 #include<cstdio>
#include<iostream>
using namespace std;
int main(){
long long N, a = , ans = , left, mid, right;
scanf("%lld", &N);
while(N / (a / ) != ){
left = N / a;
mid = N % a / (a / );
right = N % (a / );
if(mid == )
ans += left * (a / );
else if(mid == )
ans += left * (a / ) + right + ;
else if(mid > )
ans += (left + ) * (a / );
a *= ;
}
printf("%d", ans);
cin >> N;
return ;
}

总结:

1、本题应寻找规律完成。对于数字abcde来说,讨论第c位为1的有多少个数字,分为左边:ab,中间c, 右边de。当c = 1时,ab可以取0到ab的任意数字,当取0到ab - 1时,de位任意取。当ab取ab时,de位只能从0 到 de, 故共有ab * 1000 + de + 1个。  当c = 0时,为了让c能 =  1, ab位只能取0 到 ab - 1共ab种可能,de位任取,故共有ab*1000种可能。  当c > 1时,ab位可以取0到ab共ab+1种可能,de任取,故共有 (ab + 1)* 1000种可能。

A1049. Counting Ones的更多相关文章

  1. PAT甲级——A1049 Counting Ones

    The task is simple: given any positive integer N, you are supposed to count the total number of 1's ...

  2. PAT甲级题解分类byZlc

    专题一  字符串处理 A1001 Format(20) #include<cstdio> int main () { ]; int a,b,sum; scanf ("%d %d& ...

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

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

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

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

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

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

  6. 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 ...

  7. 1004. Counting Leaves (30)

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

  8. 6.Counting Point Mutations

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

  9. 1.Counting DNA Nucleotides

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

随机推荐

  1. Ubuntu16.04下安装破解secureCRT和secureFX的操作记录

    本地电脑之前安装的是win10,疲于win10频繁的更新和各种兼容问题,果断放弃win10系统,安装了Ubuntu 16.04系统,现在微信.QQ.钉钉.WPS等都已支持linux版本,所以在Ubun ...

  2. 解决error while loading shared libraries: libXXX.so.X: cannot open shared object file: No such file

    原文地址:http://blog.csdn.net/yjk13703623757/article/details/53217377 一.问题 运行hydra时,提示错误: hydra : error ...

  3. 第二次作业 对VC++6.0编译软件的评价

    首先这个软件伴随着我们很长时间了,它是我们一上大学最先接触的,也是应用相当多的一个软件,其实在最初的时候,我对编译软件的理解非常有限,觉得它能实现一个代码的功能十分神奇的一件事情,虽然彼时我们写的代码 ...

  4. 个人项目Week1

    一.项目时间规划与实际用时 PSP2.1 Personal Software Process Stages 预计时间/h 实际时间/h Planning 计划     · Estimate · 估计这 ...

  5. linux内核分析第七次实验

    实验: rm menu -rf git clone https://github.com/megnning/menu.git cd menu ls mv test_exec.c test.c vi t ...

  6. 在Java中执行Tomcat中startup.bat

    问题:更改数据库时,需要重启Tomcat服务器,才能把更改后的数据加载到项目中.于是想每次更改数据库时,都调用Java方法,重启Tomcat 代码: Process process = Runtime ...

  7. 关于git的一些体会:

    周忠贤github链接:https://github.com/zhouzhongxian git学习心得:通过这次的学习,体会到了许多东西只要你用心去做,就没有什么做不成,,这次体会到了网上学习的重要 ...

  8. "一个程序员的生命周期"读后感

    这篇文章中作者叙述了自己和大多数大学生或许都会面对的问题,即是会走过挺多的歪路,面临很多的困难和压力,但是作者却从未放弃自己真正追求的东西.对于一个过来人的经验之谈,我们应该吸取经验,在大学好好去奋斗 ...

  9. 虚拟机Linux(centos)系统能ping通主机,主机无法ping通Linux解决方案

    本文引用:https://blog.csdn.net/clean_water/article/details/53023308 三个步骤: 第一步:虚拟机网络连接方式选择Nat 第二步.关闭liunx ...

  10. PAT 1071 小赌怡情

    https://pintia.cn/problem-sets/994805260223102976/problems/994805264312549376 常言道“小赌怡情”.这是一个很简单的小游戏: ...