PAT甲级——A1049 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
显然暴力枚举的方法是不可取的,我们需要寻找其中的规律,本题中可以分别计算出每一位的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的更多相关文章
- PAT甲级1049. Counting Ones
PAT甲级1049. Counting Ones 题意: 任务很简单:给定任何正整数N,你应该计算从1到N的整数的十进制形式的1的总数.例如,给定N为12,在1,10, 11和12. 思路: < ...
- 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 ...
- PAT甲级1049 Counting Ones【规律】
题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805430595731456 题意: 给定n,问0~n中,1的总个数 ...
- PAT 甲级 1115 Counting Nodes in a BST
https://pintia.cn/problem-sets/994805342720868352/problems/994805355987451904 A Binary Search Tree ( ...
- PAT 甲级 1004 Counting Leaves
https://pintia.cn/problem-sets/994805342720868352/problems/994805521431773184 A family hierarchy is ...
- PAT甲级 1004.Counting Leaves
参考:https://blog.csdn.net/qq278672818/article/details/54915636 首先贴上我一开始的部分正确代码: #include<bits/stdc ...
- 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 ...
- PAT甲级——A1004 Counting Leaves
A family hierarchy is usually presented by a pedigree tree. Your job is to count those family member ...
随机推荐
- socket2里面,有些函数带WSA开头,有些不带。请问有何区别?
WSASocket可以使用WinSock特有功能,比如重叠IO,用dwflags指定. WSA的A是指api,用于区别spi,因为在spi中还有wspsocket,wspaccept等... 在 ...
- day22_3-json模块
# 参考资料:# python模块(转自Yuan先生) - 狂奔__蜗牛 - 博客园# https://www.cnblogs.com/guojintao/articles/9070485.html# ...
- Django的日常-模型层(2)
目录 Django的日常-模型层(2) 几种常用的查询方式 聚合查询 分组查询 F和Q查询 查询优化相关 orm中常见字段 choices参数 orm的事务操作 Django的日常-模型层(2) 几种 ...
- Python3中面向对象 OOP
Python3中面向对象 OOP 定义: python中通过关键字 class 实现类的定义: class ClassName(object): pass 获取成员变量:ClassName.变量名 修 ...
- 使用CEfSharp之旅(5)CEFSharp 隔离Cookie
原文:使用CEfSharp之旅(5)CEFSharp 隔离Cookie 版权声明:本文为博主原创文章,未经博主允许不得转载.可点击关注博主 ,不明白的进群191065815 我的群里问 https:/ ...
- readv 和 writev
Unix 系统已经长时间支持名为 readv 和 writev 的 2 个系统调用. 这些 read 和 write 的"矢量"版本使用一个结构数组, 每个包含一个缓存的指针和一个 ...
- 洛谷P3239 [HNOI2015]亚瑟王
题目描述 小 K 不慎被 LL 邪教洗脑了,洗脑程度深到他甚至想要从亚瑟王邪教中脱坑.他决定,在脱坑之前,最后再来打一盘亚瑟王.既然是最后一战,就一定要打得漂亮.众所周知,亚瑟王是一个看脸的游戏,技能 ...
- 【JZOJ3347】树的难题
description analysis 比较麻烦树形\(DP\) 不过这个我还是不算很懂-- 下次要注意思考,不要怕麻烦 code #pragma GCC optimize("O3&quo ...
- 廖雪峰Java11多线程编程-3高级concurrent包-4Concurrent集合
Concurrent 用ReentrantLock+Condition实现Blocking Queue. Blocking Queue:当一个线程调用getTask()时,该方法内部可能让给线程进入等 ...
- mysql权限操作
1.mysql权限操作 grant select,insert on test1.tb1 to ltx2@127.0.0.1 默认权限:什么都没有 2.用户管理特殊命令: 创建用户:create us ...