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 (≤).

Output Specification:

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

Sample Input:

12
 

Sample Output:

5

题意:

  给出一个数字n,问0~n这些数字中,‘1’出现的次数。

思路:

  如果暴力求解的话,会有两组数据超时,这道题是一道数学题,可以归纳总结出公式来求解这道题。

  从0~n这些数字中,‘1’出现的次数,我们可以通过计算每一位上出现‘1’的次数然后相加即可。具体的证明我没有推导,给出一个直观的例子:12——个位上为‘1’的所有可能:1, 11; 十位上为‘1’的所有可能:10, 11, 12; 这样我们就可以把‘11’这种出现两个‘1’的情况计算两次,从而满足要求。

  以一个5位数字,百位的计算方法为例:12045, 12145, 12245;

  12045——百位为‘0’,只要百位左边的数字比12小,且有‘1’出现都要考虑进去:00100~00199; 00200~00299; …… 11100~11199; 11000~11099;共有12 * 100 个数字满足要求。

  12145——百位为‘1’,在原来百位为‘0’的基础上再加上 100 ~ 145 这46种不同的情况,共 12 * 100 + (45 + 1)个不同的数字。

  12245——百位大于‘1’,我们只需要考虑高位就可以列全所有,00100 ~ 00199; 00200 ~ 00299; …… 12100 ~ 12199,共 (12 + 1) * 100 个不同的数字。

  清楚了上面的基本原理之后,我们来推导计算公式:

  

  left = n / (a * 10);

  right = n % a;

Code :

 1 #include <bits/stdc++.h>
2
3 using namespace std;
4
5 int main() {
6 int n;
7 cin >> n;
8 int now, left, right, a = 1, ans = 0;
9 while (n / a) {
10 now = n / a % 10;
11 left = n / (a * 10);
12 right = n % a;
13 if (now == 0) {
14 ans += left * a;
15 } else if (now == 1) {
16 ans += left * a + (right + 1);
17 } else {
18 ans += (left + 1) * a;
19 }
20 a *= 10;
21 }
22 cout << ans << endl;
23 return 0;
24 }

参考:

  https://blog.csdn.net/xyt8023y/article/details/46953935

  https://blog.csdn.net/CV_Jason/article/details/85112495

1049 Counting Ones的更多相关文章

  1. PAT 解题报告 1049. Counting Ones (30)

    1049. Counting Ones (30) The task is simple: given any positive integer N, you are supposed to count ...

  2. PAT 1049 Counting Ones[dp][难]

    1049 Counting Ones (30)(30 分) The task is simple: given any positive integer N, you are supposed to ...

  3. PAT甲级1049. Counting Ones

    PAT甲级1049. Counting Ones 题意: 任务很简单:给定任何正整数N,你应该计算从1到N的整数的十进制形式的1的总数.例如,给定N为12,在1,10, 11和12. 思路: < ...

  4. PAT 1049 Counting Ones [难]

    1049 Counting Ones (30 分) The task is simple: given any positive integer N, you are supposed to coun ...

  5. pat 甲级 1049. Counting Ones (30)

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

  6. PAT 甲级 1049 Counting Ones (30 分)(找规律,较难,想到了一点但没有深入考虑嫌麻烦)***

    1049 Counting Ones (30 分)   The task is simple: given any positive integer N, you are supposed to co ...

  7. 1049. Counting Ones/整数中1出现的次数(从1到n整数中1出现的次数)

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

  8. pat 1049. Counting Ones (30)

    看别人的题解懂了一些些    参考<编程之美>P132 页<1 的数目> #include<iostream> #include<stdio.h> us ...

  9. pat 1049 Counting Ones

    要统计1到N之间‘1’的个数,如数11包含2个1.所以当N=12时,答案为5. 思想: 找规律,假设ans[N]表示1到N的‘1’的个数,则有a[100]=(a[10]-1)*9+10+a[10]-1 ...

  10. PAT (Advanced Level) 1049. Counting Ones (30)

    数位DP.dp[i][j]表示i位,最高位为j的情况下总共有多少1. #include<iostream> #include<cstring> #include<cmat ...

随机推荐

  1. CRLF注入原理

    CRLF 指的是回车符(CR,ASCII 13,\r,%0d) 和换行符(LF,ASCII 10,\n,%0a),操作系统就是根据这个标识来进行换行的,你在键盘输入回车键就是输出这个字符,只不过win ...

  2. Docker下FastDFS环境搭建

    本文使用docker进行搭建. #拉取镜像docker pull delron/fastdfs​#创建tracker容器docker create --network=host --name trac ...

  3. 常用linux命令,开发必备-速收藏

    在前面我们介绍了通过VirtualBox安装Linux的方法,参考: 一网打尽,一文讲通虚拟机VirtualBox及Linux使用 本文我们将介绍在使用linux的过程中常用的一些Linux命令,掌握 ...

  4. URL 地址解析

    //虚拟目录的路径 Response.Write("<strong>Request.ApplicationPath:</strong>" + Request ...

  5. CVE-2016-10033 WordPress <= 4.6 命令执行漏洞

    漏洞参考 https://www.jianshu.com/p/85ac4af9f947 漏洞信息 这个锅还是要PHPMailer背(CVE-2016-10033,WordPress 使用 PHPMai ...

  6. Java 树结构的基础部分(一)

    二叉树 1.1 为什么需要树这种数据结构 1) 数组存储方式的分析 优点:通过下标方式访问元素,速度快.对于有序数组,还可使用二分查找提高检索速度. 缺点:如果要检索具体某个值,或者插入值(按一定顺序 ...

  7. mongodb 聚合(Map-Reduce)

    介绍 Map-reduce 是一种数据处理范式,用于将大量数据压缩为有用的聚合结果.对于 map-reduce 操作,MongoDB 提供MapReduce数据库命令. MongoDB中的MapRed ...

  8. 以“有匪”为实战案例,用python爬取视频弹幕

    最近腾讯独播热剧"有匪"特别火,我也一直在追剧,每次看剧的时候都是把弹幕开启的,这样子看剧才有灵魂呀.借助手中的技术,想爬取弹幕分析下这部电视剧的具体情况和网友们的评论!对于弹幕的 ...

  9. 京东 vue3 组件库震撼升级,如约而至!

    京东零售开源项目 NutUI 是一套京东风格的轻量级移动端 Vue 组件库,是开发和服务于移动 Web 界面的企业级产品.经过长时间的开发与打磨,NutUI 3.0 终于和大家见面了!3.0 版本在技 ...

  10. Topshelf一个用于使用.NET构建Windows服务框架

    1 Topshelf是什么? Topshelf是用于托管使用.NET框架编写的Windows服务的框架.服务的创建得到简化,从而使开发人员可以创建一个简单的控制台应用程序,可以使用Topshelf将其 ...