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. VS2010-MFC(状态栏的使用详解)

    转自:http://www.jizhuomi.com/software/219.html 上一节讲了工具栏的创建.停靠与使用,本节来讲解状态栏的知识. 状态栏简介 状态栏相信大家在很多窗口中都能见到, ...

  2. JS switch 分支语句

    描述:根据一个变量的不同取值,来执行不同的代码. 语法结构: switch(变量) { case 值1: 代码1; break; case 值2: 代码2; break; case 值3: 代码3; ...

  3. java获取服务器路径

    java获取服务器一些信息的方法(服务器地址/相对路径/端口/项目名字 request.getServletContext().getRealPath("/")  获取项目所在服务 ...

  4. <Django> MVT三大块之view(视图)

    1.视图简介 作用:接受web请求,返回web响应 本质:一个函数,定义在views.py文件中(定义在其他地方也行,约定俗成) 流程:(如果匹配不到,报一个404错误)-----结果返回http r ...

  5. 用MapReduce实现关系的自然连接

  6. art-template官方文档

    http://aui.github.io/art-template/zh-cn/docs/

  7. 廖雪峰Java11多线程编程-2线程同步-2synchronized方法

    1.Java使用synchronized对一个方法进行加锁 class Counter{ int count = 0; public synchronized void add(int n){ cou ...

  8. iOS之String动态书写

    /** String动画书写出来 @param string 要写的字 @param view 父视图 @param ui_font 字体大小 @param color 字体颜色 */ - (void ...

  9. Docker系列(十):Kubernetes集群入门

    kubenetes安装 官网:https://github.com/kubernetes/kubernetes/blob/release-1.0/docs/getting-started- guide ...

  10. HTML 排版标记

    <p></p> : 表示一个段落 常用属性 : align : 水平对齐方式  取值 :left  center right 和Word文档一样 : 段落有空行  <br ...