pat 甲级 1049. 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
思路:
对于一个数的abcde,先计算其中的某一位上出现1的个数,不妨考虑百位上的c,若c为0,则百位上出现1的个数和c的高位ab有关,具体个数是ab*digit,这里c是在百位上,那么digit为100;
若c=1,则百位上出现1的个数为(ab*digit)+de+1,其中de为abcde中c的低位.9=>c>=2时,百位上出现1的个数为(ab+1)*digit.
AC代码:
- #define _CRT_SECURE_NO_DEPRECATE
- #include<iostream>
- #include<algorithm>
- #include<cmath>
- #include<cstring>
- #include<string>
- #include<set>
- #include<queue>
- #include<map>
- using namespace std;
- #define INF 0x3f3f3f
- #define N_MAX 200+5
- #define M_MAX 100000+20
- typedef long long ll;
- int n ,cnt = ;
- int Count(int n) {
- int cnt = ,digit=;
- while (n / digit != ) {
- int higher = n / (digit * );
- int lower = n - (n / digit)*digit;//!!
- int cur = n / digit % ;
- switch (cur){
- case :
- cnt += higher*digit;
- break;
- case :
- cnt += higher*digit + lower + ;
- break;
- default:
- cnt += (higher + )*digit;
- break;
- }
- digit *= ;
- }
- return cnt;
- }
- int main() {
- while (cin>>n) {
- cnt = Count(n);
- cout << cnt << endl;
- }
- return ;
- }
pat 甲级 1049. Counting Ones (30)的更多相关文章
- 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
PAT甲级1049. Counting Ones 题意: 任务很简单:给定任何正整数N,你应该计算从1到N的整数的十进制形式的1的总数.例如,给定N为12,在1,10, 11和12. 思路: < ...
- PAT甲级1049 Counting Ones【规律】
题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805430595731456 题意: 给定n,问0~n中,1的总个数 ...
- PAT Advanced 1049 Counting Ones (30) [数学问题-简单数学问题]
题目 The task is simple: given any positive integer N, you are supposed to count the total number of 1 ...
- 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 (30 分)(类似数位DP思想的模拟)
题意: 输入一个正整数N(N<=2^30),输出从1到N共有多少个数字包括1. AAAAAccepted code: #define HAVE_STRUCT_TIMESPEC #include& ...
- 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 1049. Counting Ones (30)
看别人的题解懂了一些些 参考<编程之美>P132 页<1 的数目> #include<iostream> #include<stdio.h> us ...
- PAT (Advanced Level) 1049. Counting Ones (30)
数位DP.dp[i][j]表示i位,最高位为j的情况下总共有多少1. #include<iostream> #include<cstring> #include<cmat ...
随机推荐
- JS时间格式和时间戳的相互转换
时间戳转化为日期的方式 ; var newDate = new Date(); newDate.setTime(timestamp * ); // Mon May 28 2018 console.lo ...
- 题解 P3367 【【模板】并查集】
#include<iostream> #include<cstdio> using namespace std; int n,m,x,y,z; ]; //f[i]表示i的祖先 ...
- 项目实战14.1—ELK 企业内部日志分析系统
本文收录在Linux运维企业架构实战系列 一.els.elk 的介绍 1.els,elk els:ElasticSearch,Logstash,Kibana,Beats elk:ElasticSear ...
- 第二章JavaScript 函数和对象
1 JavaScript 函数 1.1 声明函数的方式 function 关键字 匿名函数方式(表达式方式) Function 构造函数方式 1.2 参数问题 形参和实参数量问题 可选形参(参数默认值 ...
- javascript sprintf方法
转载自: http://demon.tw/programming/javascript-sprintf.html function str_repeat(i, m) { for (var o = [] ...
- 删除项目开发中的.pyc文件
在实际开发中python会自动生成很多pyc文件,但是这些pyc文件是不需要我们追踪的,删除了对项目也没有影响,下面是删除pyc文件的方法. Linux或Mac系统 find /tmp -name & ...
- DAG上的动态规划——嵌套矩阵问题
问题描述:有n个矩形,每个矩形可以用两个整数a,b描述,表示它的长和宽.矩形X(a,b)可以嵌套在矩形Y(c,d)中当且仅当a<c,b<d,或者b<c,a<d(相当于把矩形X旋 ...
- 使用python实现简单爬虫
简单的爬虫架构 调度器 URL管理器 管理待抓取的URL集合和已抓取的URL,防止重复抓取,防止死循环 功能列表 1:判断新添加URL是否在容器中 2:向管理器添加新URL 3:判断容器是否为空 4: ...
- fetch 使用记录
fetch api出来很多年了 ,由于兼容性问题之前一直没在项目中用到,最近做的项目只需兼容IE9+,把fetch引入了进来.目前用起来感觉挺好的,简洁. fetch 返回值是promise对象,对于 ...
- Java并发之(2):线程通信wait/notify(TIJ_21_5)
简介: java中线程间同步的最基本的方式就是使用wait()¬ify()¬ifyAll(),它们是线程间的握手机制.除了上述方法,java5还在java.util.con ...