1049. Counting Ones (30)

时间限制
10 ms

内存限制
65536 kB

代码长度限制
16000 B

判题程序
Standard

作者
CHEN, Yue

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)相关推荐

  1. pat1004. Counting Leaves (30)

    1004. Counting Leaves (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A fam ...

  2. PAT甲级1049 Counting Ones (30 分):[C++题解]统计1的个数、数位统计

    文章目录 题目分析 题目链接 题目分析 来源:PAT网站 分析: 以数字abcdefg这个7位数字为例,说一下本题的思路. 1)数字1在每一位出现的次数. 2)以第d位为例,第d位的取值可以分为3种情 ...

  3. PAT甲级1004 Counting Leaves (30分):[C++题解]树、邻接表存储树、dfs遍历树

    文章目录 题目分析 题目链接 题目分析 题意重述:一棵树,求每一层的叶子节点数目. 分析 构造树,使用邻接表来存(相当于存储有向图). 需要一个头结点数组h[N],然后每个头节点往外形成一个单链表e[ ...

  4. 1004. Counting Leaves (30)

    时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A family hierarchy is usually pr ...

  5. 1004 Counting Leaves (30分) (vector实现)

    题解: 用vector邻接表建图,两个邻接表实现层序遍历. 有注释在代码 代码: /*Keep on going Never give up*/ #pragma GCC optimize(3,&quo ...

  6. 1049. Counting Ones (30)

    题目如下: The task is simple: given any positive integer N, you are supposed to count the total number o ...

  7. 1049 Counting Ones (30 分)【难度: 难 / 知识点: 分治 / DP】

    https://pintia.cn/problem-sets/994805342720868352/problems/994805430595731456 方法一: 找规律,分治做法. //0-999 ...

  8. 1004 Counting Leaves (30 分)【难度: 中 / 知识点: 树的遍历】

    题目意思: 求每一层的叶子结点数 方法一: 深搜 数组模拟存储邻接表 #include<bits/stdc++.h> using namespace std; const int N=1e ...

  9. 【两种解法】1004 Counting Leaves (30 分)_27行代码AC

    立志用最少的代码做最高效的表达 PAT甲级最优题解-->传送门 A family hierarchy is usually presented by a pedigree tree. Your ...

最新文章

  1. Jackson 通过自定义注解来控制json key的格式
  2. DayDayUp:微软最有价值专家MVP申请表格、准备条件、流程等详细攻略
  3. Flask 中内置的 Session
  4. HDU 4990 Ordered Subsequence --数据结构优化DP
  5. Linux Shell脚本_禁止定时任务发送邮件
  6. 在windows下codeblocks中配置pthread库
  7. MyEclipse在删除文件后servers报错问题解决
  8. 【博客管理】博客新建栏目的添加—HTML格式书写规范
  9. Java简单代码-用*号拼三角形
  10. Hive 窗口函数详解
  11. 钟平逻辑英语语法_逻辑英语-钟平笔记.pdf
  12. 未来真的可以影响过去吗?人人都能看懂的波粒二象性和延迟选择量子擦除实验解释,通俗易懂量子物理
  13. 蜀山前传之二---------------第八回
  14. 蓝牙HID——将android设备变成蓝牙鼠标/触控板(BluetoothHidDevice)
  15. 苏雅欣课后作业四 个人总结
  16. ubuntu 从刷机到yolov5环境搭建训练记录
  17. python这个语言值得学吗_python和java哪个更值得学
  18. https自签证书tls握手时错误或go系统错误处理
  19. flash的计算机知识,了解flash动画基础知识 -电脑资料
  20. Swin Transformer全方位解读【ICCV2021马尔奖】

热门文章

  1. photoshop切片的取消操作
  2. 面试官:你对Kafka比较熟? 那说说kafka日志段如何读写的吧?
  3. 聊聊大厂面试官必问的 MySQL 锁机制
  4. 一个历时五天的 Bug
  5. 无责任书评:每个Java程序员都应该深入理解Java虚拟机!
  6. WEB攻击手段及防御第3篇-CSRF
  7. 网络:HTTP1.1和HTTP2区别
  8. JVM:jstack
  9. 插入排序 链表 java_JAVA单链表(多项式)直接插入排序,大家看看我的怎么不行呢...
  10. 首批49个国家绿色数据中心名单公布