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 ...
随机推荐
- Java基础语法:类型转换
由于Java是强类型语言,所以有时候在进行运算时,需要用到类型转换. 整型.常量.字符类型数据可以混合运算. 运算中,不同类型的数据先转化为同一类型,然后再进行运算. 类型转换等级有低级到高级的划分, ...
- Spirent Tester二层裸流配置
1.OLT配置 配一个VLAN,若GE口打Tag,不需要打PVID,打Untag,配PVID. 在ONU上配一个Other Bridge Wan链接. 2.TestCenter配置 选定两个TestC ...
- 数据结构-PHP 线段树的实现
转: 数据结构-PHP 线段树的实现 1.线段树介绍 线段树是基于区间的统计查询,线段树是一种 二叉搜索树,它将一个区间划分成一些单元区间,每个单元区间对应线段树中的一个叶结点.使用线段树可以快速的查 ...
- 该死的端口占用!教你用 Shell 脚本一键干掉它!
1. 前言 大家好,我是安果! 在 Web 开发中,经常会遇到「端口被占用」的场景 常规解决方案是: 使用 lsof -i 命令查询占用端口的进程 PID 利用 kill -9 PID 干掉目标进程 ...
- 不一样的软件们——GitHub 热点速览 v.21.10
作者:HelloGitHub-小鱼干 创意,是程序员的一个身份代名词,一样的软件有不一样的玩法.比如,你可以像用 git 一样操作一个 SQL 数据库,dolt 就是这样的数据库.又比如,你可以只写文 ...
- editplus更改编码
1.在Tools下拉后选择Configure User Tools 2.在左边导航菜单找到File,对应右边视图中的Default encoding 3.将编码更改为utf-8,点击底部的OK保存.
- [源码分析] 消息队列 Kombu 之 Hub
[源码分析] 消息队列 Kombu 之 Hub 0x00 摘要 本系列我们介绍消息队列 Kombu.Kombu 的定位是一个兼容 AMQP 协议的消息队列抽象.通过本文,大家可以了解 Kombu 中的 ...
- All I know about A/B Test (1) : 均值型指标与比值(率)型指标的计算区别
因为最近在找实习,所以打算把自己之前学过的关数据分析的知识总结(复习)一下.在总结A/B test时,我发现中文互联网中关于A/B test的总结已经很多了,但是对于均值型指标和比值(率)型指标在设计 ...
- 利用Navicat premium实现将数据从Oracle导入到MySQL
背景:我们给用户提供了新的直播系统,但客户之前的老系统用的数据库是Oracle,我们提供的新系统用的是MySQL 客户诉求:将老系统中的所有直播数据导入到MySQL中: 思路:我知道Navicat有数 ...
- Android Studio之显示本地时间
•效果展示图 •代码 1 package com.example.table; 2 3 import android.os.Bundle; 4 import android.os.Handler; 5 ...