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. Docker-compose编排微服务顺序启动

    一.概述 docker-compose可以方便组合多个 docker 容器服务, 但是, 当容器服务之间存在依赖关系时, docker-compose 并不能保证服务的启动顺序.docker-comp ...

  2. redis数据结构和对象二

    跳跃表(skiplist) 跳跃表是一种有序数据结构.跳跃表支持平均O(logN),最坏O(N)复杂度的节点查找,大部分情况下,跳跃表的效率可以和平衡树相媲美,并且因为跳跃表的实现比平衡树简单,所有不 ...

  3. 后端程序员之路 33、Index搜索引擎实现分析2-对外接口和大体流程

    # index_manager的单例是index server对外的唯一接口,part_indexer是index搜索的核心部分,index_manager持有了一组part_indexer. typ ...

  4. 通过 .NET NativeAOT 实现用户体验升级

    前言 TypedocConverter 是我先前因帮助维护 monaco-editor-uwp 但苦于 monaco editor 的 API 实在太多,手写 C# 的类型绑定十分不划算而发起的一个项 ...

  5. 细说MySQL连接查询:内连、左连和右连

    转: 细说MySQL连接查询:内连.左连和右连 简介: MySQL 的连接查询,通常都是将来自两个或多个表的行结合起来,基于这些表之间的共同字段,进行数据的拼接.首先,要确定一个主表作为结果集,然后将 ...

  6. 推荐模型AutoRec:原理介绍与TensorFlow2.0实现

    1. 简介 本篇文章先简单介绍论文思路,然后使用Tensoflow2.0.Keras API复现算法部分.包括: 自定义模型 自定义损失函数 自定义评价指标RMSE 就题目而言<AutoRec: ...

  7. 【Arduino学习笔记07】模拟信号的输入与输出 analogRead() analogWrite() map() constrain()

    模拟信号:Arduino中的模拟信号就是0v~5v的连续的电压值 数字信号:Arduino中的数字信号就是高电平(5V)或者低电平(0V),是两个离散的值 模拟信号->数字信号:ADC(模数转换 ...

  8. 【odoo14】第六章、管理模块数据

    本章代码可在原作者github下载 使用外部ID及命名空间 外部ID及XML ID用于标记记录.到目前为止,我们在视图.菜单及动作中接触了XML IDs.本节我们将进一步了解什么是XML ID. 步骤 ...

  9. GRU算法原理

    一.GRU算法 GRU(Gate Recurrent Unit,循环门单元)是循环神经网络(Recurrent Neural Network, RNN)的一种.和LSTM(Long-Short Ter ...

  10. Git命令太多记不住?有了这个神器,从此告别输入命令行

    一 .SourceTree简介 SourceTree 是 Windows 和Mac OS X 下免费的 Git 和 Hg 客户端,拥有可视化界面,容易上手操作.同时它也是Mercurial和Subve ...