1049. Counting Ones (30)

时间限制
100 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
思路:
对于一个数的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)的更多相关文章

  1. PAT 甲级 1049 Counting Ones (30 分)(找规律,较难,想到了一点但没有深入考虑嫌麻烦)***

    1049 Counting Ones (30 分)   The task is simple: given any positive integer N, you are supposed to co ...

  2. PAT甲级1049. Counting Ones

    PAT甲级1049. Counting Ones 题意: 任务很简单:给定任何正整数N,你应该计算从1到N的整数的十进制形式的1的总数.例如,给定N为12,在1,10, 11和12. 思路: < ...

  3. PAT甲级1049 Counting Ones【规律】

    题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805430595731456 题意: 给定n,问0~n中,1的总个数 ...

  4. 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 ...

  5. PAT 解题报告 1049. Counting Ones (30)

    1049. Counting Ones (30) The task is simple: given any positive integer N, you are supposed to count ...

  6. 【PAT甲级】1049 Counting Ones (30 分)(类似数位DP思想的模拟)

    题意: 输入一个正整数N(N<=2^30),输出从1到N共有多少个数字包括1. AAAAAccepted code: #define HAVE_STRUCT_TIMESPEC #include& ...

  7. 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 ...

  8. pat 1049. Counting Ones (30)

    看别人的题解懂了一些些    参考<编程之美>P132 页<1 的数目> #include<iostream> #include<stdio.h> us ...

  9. PAT (Advanced Level) 1049. Counting Ones (30)

    数位DP.dp[i][j]表示i位,最高位为j的情况下总共有多少1. #include<iostream> #include<cstring> #include<cmat ...

随机推荐

  1. Vue2+webpack+node 配置+入门+详解

    Vue2介绍 1.vue2.0 Vue (读音 /vjuː/,类似于 view) 是一套用于构建用户界面的渐进式框架. Vue 的核心库只关注视图层 采用单文件组件 复杂大型单页应用程序(SPA) 响 ...

  2. LeetCode#5 两个排序数组的中位数

      给定两个大小为 m 和 n 的有序数组 nums1 和 nums2 . 请找出这两个有序数组的中位数.要求算法的时间复杂度为 O(log (m+n)) . 你可以假设 nums1 和 nums2  ...

  3. C++学习网站——www.cplusplus.com

    http://www.cplusplus.com/ 有各个函数.语法的实例代码,可以在线运行http://cpp.sh/不支持中文字符,不错.

  4. 菜鸟学Linux - bash的配置文件

    bash是各大Linux发行版都支持的shell.当我们登陆bash的时候,虽然我们什么都没做,但是我们已经可以在bash中调用各种各样的环境变量了.这是因为,系统中已经定义了一系列的配置文件,以及加 ...

  5. Android使用Glide加载Gif.解决Glide加载Gif非常慢问题

    在Glide文档中找了半天没发现加载Gif的方式.然后通过基本的用法去加载: Glide.with(MainActivity.this).load(url).asGif().into(imageVie ...

  6. js数据类型的检测总结,附面试题--封装一个函数,输入任意,输出他的类型

    一.javascript 中有几种类型的值 1.基本数据类型 : 包括 Undefined.Null.Boolean.Number.String.Symbol (ES6 新增,表示独一无二的值) 特点 ...

  7. 设计模式之第4章-装饰模式(Java实现)

    设计模式之第4章-装饰模式(Java实现) “怎么了,鱼哥?” “唉,别提了,网购了一件衣服,结果发现和商家描述的差太多了,有色差就算了,质量还不好,质量不好就算了,竟然大小也不行,说好的3个X,邮的 ...

  8. 【Add Two Numbers】

    题目: You are given two linked lists representing two non-negative numbers. The digits are stored in r ...

  9. Leetcode 630.课程表III

    课程表III 这里有 n 门不同的在线课程,他们按从 1 到 n 编号.每一门课程有一定的持续上课时间(课程时间)t 以及关闭时间第 d 天.一门课要持续学习 t 天直到第 d天时要完成,你将会从第 ...

  10. 求解Catalan数,(大数相乘,大数相除,大数相加)

    Catalan数 卡塔兰数是组合数学中一个常在各种计数问题中出现的数列.以比利时的数学家欧仁·查理·卡塔兰(1814–1894)命名.历史上,清代数学家明安图(1692年-1763年)在其<割圜 ...