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. 前端-带header和footer的双栏布局

    目标是实现如上图带header和footer的双栏布局,其中右侧sidebar是固定宽度,左侧content是自适应: https://www.zybuluo.com/dengzhirong/note ...

  2. Drop it-freecodecamp算法题目

    Drop it 1.要求 丢弃数组(arr)的元素,从左边开始,直到回调函数return true就停止. 第二个参数,func,是一个函数.用来测试数组的第一个元素,如果返回fasle,就从数组中抛 ...

  3. python 实现剪刀石头布(三局两胜)

    # -*- coding:utf-8 -*- import random # best of three def finger_guess(): rule = {1:'rock', 2:'paper' ...

  4. 【JAVA】cxf使用springboot与xml配置的差别所导致的问题。

    使用xml时使用以下配置使报文没有加上命名空间时也能正常访问接口.bean定义的前后顺序不影响程序正常注册对象. <!-- 通过Spring创建数据绑定的类 --> <bean id ...

  5. 洛谷 P2279 [HNOI2003]消防局的设立

    题目描述 2020年,人类在火星上建立了一个庞大的基地群,总共有n个基地.起初为了节约材料,人类只修建了n-1条道路来连接这些基地,并且每两个基地都能够通过道路到达,所以所有的基地形成了一个巨大的树状 ...

  6. 指向class的指针使用方法实例

    // pointer to classes example #include <iostream> using namespace std; class Rectangle { int w ...

  7. HDU:1358-Period

    Period Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Problem Desc ...

  8. wcf第三方客户端与wcf服务之间调用入门

    Wcf服务与我们的客户端如何建立联系的呢.本文简单记录一下 1.创建我们的wcf服务程序. 第一个wcf服务库是创建我们的wcf库,运行时会单独来托管我们的程序,而非托管在iis下. 第二个wcf服务 ...

  9. Android四大组件之服务

    创建一个服务,并与活动绑定 作为安卓四大组件之一的服务,毫无例外也要在manifast中进行注册 新建服务类继承于Service,并覆盖onBind( )方法,用于与活动绑定 public class ...

  10. Java面向对象---面向对象

    程序的发展历程:面向过程-->面向对象 面向过程:不去想怎么做,边做边看 面向对象:先想好怎么做,然后再做 修饰符(访问权限) 访问级别 访问控制修饰符 同类 同包 子类 不同的包 公开 pub ...