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

显然暴力枚举的方法是不可取的,我们需要寻找其中的规律,本题中可以分别计算出每一位的1出现的个数再进行加和。本博客中以数字N=345、N=305、N=315为例,寻找十位数字上是1的数字个数。将数字分成3部分:百位、十位、各位。
显然,从1~345这345个数中,百位数字可以出现0、1、2、3四种,每种百位数字都可以跟一个数字为1的十位,而每种十位数字可以跟0~9这十种数字,所以从1~345这345个数中,十位数字为1的数共有(3+1)×10=40 (3+1)×10=40(3+1)×10=40个,故十位上的1共出现40次。
当N=305时,百位数字依然可以出现0、1、2、3四种,但要注意,百位数字为3时,后面不能再跟数字为1的十位,因为这样的数字已经大于305了,所以从1~305这305个数中,十位数字为1的数共有3×10=30 3×10=303×10=30个,故十位上的1共出现30次。
当N=315时,百位数字依然可以出现0、1、2、3四种,此时要注意,百位数字为3时,后面可以再跟数字为1的十位,但这样的数字个位上只能出现0~5这6个数,即310、311、312、313、314、315,其他数字都会大于315,所以从1~315这315个数中,十位数字为1的数共有3×10+(5+1)=36 3×10+(5+1)=363×10+(5+1)=36个,故十位上的1共出现36次。

综上,对于任意一个数字N,当要判断从右向左数第i位上1出现的次数num时,可以将这个数字分成三部分,分别用left、current、right表示,即left=数字N在i位左侧的数字、current=数字N在第i位的数字、right=数字N在i位右侧的数字。例如数字N=123456,判断从右向左第2位也就是百位上,即数字4所在位置1出现的次数时,left=123、current=4、right=56。此时分三种情况进行计算:

 #include <iostream>
using namespace std;
int main()
{
int n, ans = ;
cin >> n;
int left = n / , right = , cur = n % ;
for (int i = ; right != n; i *= )
{
ans += left * i + (cur == ? : cur == ? (right + ) : i);
right += cur * i;
cur = left % ;
left /= ;
}
cout << ans << endl;
return ;
}

PAT甲级——A1049 Counting Ones的更多相关文章

  1. PAT甲级1049. Counting Ones

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

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

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

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

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

  4. PAT甲级1049 Counting Ones【规律】

    题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805430595731456 题意: 给定n,问0~n中,1的总个数 ...

  5. PAT 甲级 1115 Counting Nodes in a BST

    https://pintia.cn/problem-sets/994805342720868352/problems/994805355987451904 A Binary Search Tree ( ...

  6. PAT 甲级 1004 Counting Leaves

    https://pintia.cn/problem-sets/994805342720868352/problems/994805521431773184 A family hierarchy is ...

  7. PAT甲级 1004.Counting Leaves

    参考:https://blog.csdn.net/qq278672818/article/details/54915636 首先贴上我一开始的部分正确代码: #include<bits/stdc ...

  8. PAT甲级——A1115 Counting Nodes in a BST【30】

    A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...

  9. PAT甲级——A1004 Counting Leaves

    A family hierarchy is usually presented by a pedigree tree. Your job is to count those family member ...

随机推荐

  1. 2015年MBA备考心得

    2015年12月份山大MBA备考心得 我在去年的12月26日参加了山大的MBA招生考试,在今年3月底参加了山大的复试,并被山大录取.初试英语70分,综合部分151,总分在今年的山大考生中是第16名:复 ...

  2. php 扫描url死链接 \033[31m ANSI Linux终端输出带颜色

    * 从Packagist上搜索需要的包 https://packagist.org/ * 通过composer下载依赖包 composer require guzzlehttp/guzzlecompo ...

  3. vs 利用web deploy发布 常见问题解决

    1. 下载并安装 web deploy, 安装时选择自定义,选择全部安装 https://www.iis.net/downloads/microsoft/web-deploy  2. 确保iis的管理 ...

  4. 跨数据库查询——dblink

    现在本地建一个dblink Create database link create public database link DBLINKTEST (名称) connect to MGP(用户名) i ...

  5. <每日一题>题目14:拷贝的问题

    ''' 拷贝的问题 引用:无论怎么变都一起变 浅拷贝:只拷贝父对象,不会拷贝父对象中的子对象 深拷贝:完全拷贝,重新划分内存空间 ''' 具体如下图: 题目: #求a.b.c.d的值 import c ...

  6. 2019个人计划与Flag与期望

    突然发现写博客是真的好. 希望未来能在其他地方写上日记. 总结2018中的个人缺陷: 1.忘掉了学习方法或者说学习方法不正确 2.偶尔就会去偷下懒,对自己不够严格,自控能力差. 3.心态虽比以前好很多 ...

  7. 通过真值树解析布尔表达式(eg:A&B|C)

    第一步:求出一个表达式的truth tree 1.生成真值表 2.根据真值表生成真值树(合并短路产生相同的两个子树) /**************************************** ...

  8. Python-匿名函数与异常处理

    目录 匿名函数 什么叫匿名函数? 语法 max/min() sorted() map() filter() reduce函数 内置函数 面向过程编程 异常处理 什么是异常 异常的种类 异常处理 try ...

  9. centos7 搭建 php7 + nginx (1)

    前言 曾今,写过几篇类似的文章,但是发现几个月后,自己回头再看的时候,有种支离破碎的感觉.自己写的并不全,所以今天打算写一篇比较详细的文档.争取下次环境的减的时候,只需要拷贝复制粘贴即可完成环境搭建. ...

  10. 19-11-05-Night

    我就是不行. ZJ: 好像是因为郁闷了才咕掉的…… 33 Miemeng 30 00:01:34 40 00:01:46 0 00:01:22 70 00:01:46 不记得当时怎么想的 T1只会暴力 ...