pat1049. Counting Ones (30)
1049. Counting Ones (30)
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 (<=230).
Output Specification:
For each test case, print the number of 1's in one line.
Sample Input:
12
Sample Output:
5
提交代码
思路:
统计每位的1的贡献。
对于k位(k>=1):
1.Ak=0,count+=AnAn-1....Ak+1AkAk-1....A1*10^(k-1)
2.Ak=1,count+=AnAn-1....Ak+1AkAk-1....A1*10^(k-1)+Ak-1Ak-2...A1+1
3.Ak>=2,count+=(AnAn-1....Ak+1AkAk-1....A1+1)*10^(k-1)
1 #include<cstdio> 2 #include<stack> 3 #include<cstring> 4 #include<iostream> 5 #include<stack> 6 #include<set> 7 #include<map> 8 using namespace std; 9 //count的最大值是1036019223 10 int main(){ 11 int n; 12 scanf("%d",&n); 13 long long base=1; 14 long long count=0; 15 int frpart,afpart,a; 16 while(n>=base){ 17 a=n/base%10; 18 frpart=n/(10*base); 19 afpart=n%base; 20 count+=frpart*base; 21 if(a==1){ 22 count+=afpart+1; 23 } 24 else if(a>1){ 25 count+=base; 26 } 27 base*=10; 28 } 29 printf("%lld\n",count); 30 return 0; 31 }
转载于:https://www.cnblogs.com/Deribs4/p/4776672.html
pat1049. Counting Ones (30)相关推荐
- pat1004. Counting Leaves (30)
1004. Counting Leaves (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A fam ...
- PAT甲级1049 Counting Ones (30 分):[C++题解]统计1的个数、数位统计
文章目录 题目分析 题目链接 题目分析 来源:PAT网站 分析: 以数字abcdefg这个7位数字为例,说一下本题的思路. 1)数字1在每一位出现的次数. 2)以第d位为例,第d位的取值可以分为3种情 ...
- PAT甲级1004 Counting Leaves (30分):[C++题解]树、邻接表存储树、dfs遍历树
文章目录 题目分析 题目链接 题目分析 题意重述:一棵树,求每一层的叶子节点数目. 分析 构造树,使用邻接表来存(相当于存储有向图). 需要一个头结点数组h[N],然后每个头节点往外形成一个单链表e[ ...
- 1004. Counting Leaves (30)
时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A family hierarchy is usually pr ...
- 1004 Counting Leaves (30分) (vector实现)
题解: 用vector邻接表建图,两个邻接表实现层序遍历. 有注释在代码 代码: /*Keep on going Never give up*/ #pragma GCC optimize(3,&quo ...
- 1049. Counting Ones (30)
题目如下: The task is simple: given any positive integer N, you are supposed to count the total number o ...
- 1049 Counting Ones (30 分)【难度: 难 / 知识点: 分治 / DP】
https://pintia.cn/problem-sets/994805342720868352/problems/994805430595731456 方法一: 找规律,分治做法. //0-999 ...
- 1004 Counting Leaves (30 分)【难度: 中 / 知识点: 树的遍历】
题目意思: 求每一层的叶子结点数 方法一: 深搜 数组模拟存储邻接表 #include<bits/stdc++.h> using namespace std; const int N=1e ...
- 【两种解法】1004 Counting Leaves (30 分)_27行代码AC
立志用最少的代码做最高效的表达 PAT甲级最优题解-->传送门 A family hierarchy is usually presented by a pedigree tree. Your ...
最新文章
- Jackson 通过自定义注解来控制json key的格式
- DayDayUp:微软最有价值专家MVP申请表格、准备条件、流程等详细攻略
- Flask 中内置的 Session
- HDU 4990 Ordered Subsequence --数据结构优化DP
- Linux Shell脚本_禁止定时任务发送邮件
- 在windows下codeblocks中配置pthread库
- MyEclipse在删除文件后servers报错问题解决
- 【博客管理】博客新建栏目的添加—HTML格式书写规范
- Java简单代码-用*号拼三角形
- Hive 窗口函数详解
- 钟平逻辑英语语法_逻辑英语-钟平笔记.pdf
- 未来真的可以影响过去吗?人人都能看懂的波粒二象性和延迟选择量子擦除实验解释,通俗易懂量子物理
- 蜀山前传之二---------------第八回
- 蓝牙HID——将android设备变成蓝牙鼠标/触控板(BluetoothHidDevice)
- 苏雅欣课后作业四 个人总结
- ubuntu 从刷机到yolov5环境搭建训练记录
- python这个语言值得学吗_python和java哪个更值得学
- https自签证书tls握手时错误或go系统错误处理
- flash的计算机知识,了解flash动画基础知识 -电脑资料
- Swin Transformer全方位解读【ICCV2021马尔奖】