1049 Counting Ones
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的更多相关文章
- PAT 解题报告 1049. Counting Ones (30)
1049. Counting Ones (30) The task is simple: given any positive integer N, you are supposed to count ...
- PAT 1049 Counting Ones[dp][难]
1049 Counting Ones (30)(30 分) The task is simple: given any positive integer N, you are supposed to ...
- PAT甲级1049. Counting Ones
PAT甲级1049. Counting Ones 题意: 任务很简单:给定任何正整数N,你应该计算从1到N的整数的十进制形式的1的总数.例如,给定N为12,在1,10, 11和12. 思路: < ...
- PAT 1049 Counting Ones [难]
1049 Counting Ones (30 分) The task is simple: given any positive integer N, you are supposed to coun ...
- pat 甲级 1049. Counting Ones (30)
1049. Counting Ones (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The tas ...
- PAT 甲级 1049 Counting Ones (30 分)(找规律,较难,想到了一点但没有深入考虑嫌麻烦)***
1049 Counting Ones (30 分) The task is simple: given any positive integer N, you are supposed to co ...
- 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 ...
- pat 1049. Counting Ones (30)
看别人的题解懂了一些些 参考<编程之美>P132 页<1 的数目> #include<iostream> #include<stdio.h> us ...
- 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 ...
- PAT (Advanced Level) 1049. Counting Ones (30)
数位DP.dp[i][j]表示i位,最高位为j的情况下总共有多少1. #include<iostream> #include<cstring> #include<cmat ...
随机推荐
- SAP Spartacus简介
转: SAP Spartacus简介 终于写到Jerry目前正在做的开发任务了. 2015年的时候,那时Jerry已经做了一年多的SAP UI5开发,想进一步精进自己的开发技能,就申请了一个位于德国W ...
- 剑指 Offer 66. 构建乘积数组 + 思维
剑指 Offer 66. 构建乘积数组 Offer_66 题目描述 题解分析 java代码 package com.walegarrett.offer; /** * @Author WaleGarre ...
- HDOJ-2181(深搜记录路径)
哈密顿绕行世界问题 HDOJ-2181 1.本题是典型的搜索记录路径的问题 2.主要使用的方法是dfs深搜,在输入的时候对vector进行排序,这样才能按照字典序输出. 3.为了记录路径,我使用的是两 ...
- CentOS 7关闭firewalld启用iptables 开放端口
在CentOS7中,有很多CentOS 6中的常用服务发生了变化. 其中iptables是其中比较大的一个.防火墙iptables被firewalld取代. 本文将介绍,如果采用systemctl关闭 ...
- Nginx重定向到其他端口
location / { # limit_req zone=test_req burst=5 nodelay; return 302 http://$host:3000/; } # 我这里的端口为30 ...
- Mock 框架 Moq 的使用
Mock 框架 Moq 的使用 Intro Moq 是 .NET 中一个很流行的 Mock 框架,使用 Mock 框架我们可以只针对我们关注的代码进行测试,对于依赖项使用 Mock 对象配置预期的依赖 ...
- Java多线程之线程
前言 线程作为现代操作系统调度的最小单元,多个线程能够同时执行,这将显著提高程序的性能,而且在当前多核CPU的环境下也能更好的利用资源.Java提供了对多线程的良好支持.线程是多线程的基础. 使用多线 ...
- RocketMQ安装配置过程
官网 官方网站:http://rocketmq.apache.org 下载源码包:https://www.apache.org/dyn/closer.cgi?path=rocketmq/4.8.0/r ...
- 前后端分离之DRF——1
1. 作用 1. 序列化,序列化器会把模型对象转成字典,经过 response 以后变成 json 字符串 2. 反序列化,把客户端发送过来的数据,经过 request 以后变成字典,序列化器可以把字 ...
- 04-Spring自定义标签解析
自定义标签的解析 这一篇主要说明自定义标签的解析流程,除了 bean.alias.import.beans之外的标签,都属于自定义标签的范围,自定义标签的解析需要命名空间配合, 获取对应的命名空间 根 ...