题目

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

题目分析

已知一个正整数N,求1~N的正整数中出现1的次数(如:11算出现两次1)

解题思路

从低位到高位,计算每一位为1时的数字个数,进行累加

  • 如果当前位为0, 左边为0left-1时都可以在当前位取1(此时右边可以取到的数字总数为099999...即:a个),因为当前位为0,所以左边取left时不能取1。
  • 如果当前位为1,左边为0left-1时都可以在当前位取1(此时右边可以取到的数字总数为099999...即:a个),当前位为1时,右边只可以取到0~right(即:right个数)
  • 如果当前位大于2,左边为0left时都可以在当前位取1(此时右边可以取到的数字总数为099999...即:a个)

易错点

  1. 若枚举1~N数字,再对数字统计出现1的次数,会超时(测试点4,6)

知识点

  1. 已知正整数为N,从右往左,指针i指向N的某个位置,a当前位的权重(如:i=2,a=102;i=0,a=100)
N/(a*10) //取指针i指向的数字左边数字
N/a%10 //取指针i指向的数字
N%a //取指针i指向的数字右边数字

Code

Code 01

#include <iostream>
using namespace std;
/*
测试点4,6超时
*/
int main(int argc,char * argv[]) {
int n,t=0,a=1,left,now,right;
scanf("%d",&n);
while(n/a>0) {
left = n/(a*10),now=n/a%10,right=n%a;
if(now==0)t+=left*a;
else if(now==1)t+=left*a+right+1;
else t+=(left+1)*a;
a*=10;
}
printf("%d",t);
return 0;
}

PAT Advanced 1049 Counting Ones (30) [数学问题-简单数学问题]的更多相关文章

  1. pat 甲级 1049. Counting Ones (30)

    1049. Counting Ones (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The tas ...

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

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

  3. PAT Advanced 1004 Counting Leaves (30) [BFS,DFS,树的层序遍历]

    题目 A family hierarchy is usually presented by a pedigree tree. Your job is to count those family mem ...

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

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

  5. PAT甲级1049. Counting Ones

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

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

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

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

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

  8. PAT Advanced 1115 Counting Nodes in a BST (30) [⼆叉树的遍历,BFS,DFS]

    题目 A Binary Search Tree (BST) is recursively defined as a binary tree which has the following proper ...

  9. pat 1049. Counting Ones (30)

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

随机推荐

  1. Mac修改用户名后程序配置和文件都找不到了?

    小编今天手残,修改mac 的用户名,幸亏文件没丢失,要不然配置程序估计至少要花费周末的两天时间了.. 所幸的是,各种Google,终于找回了原用户名下的所有配置. 接下来,讲讲小编如何入坑又如何脱坑的 ...

  2. CV_图像滤波[转]---python+opencv均值滤波,高斯滤波,中值滤波,双边滤波

    1.图像滤波算法(cv2) https://blog.csdn.net/qq_27261889/article/details/80822270 2.

  3. Redis的安装并配置快捷启动

    Redis 安装 1.下载 wget http://download.redis.io/releases/redis-5.0.5.tar.gz 2.解压 tar -zxvf redis-5.0.5.t ...

  4. 【剑指Offer】面试题13. 机器人的运动范围

    题目 地上有一个m行n列的方格,从坐标 [0,0] 到坐标 [m-1,n-1] .一个机器人从坐标 [0, 0] 的格子开始移动,它每次可以向左.右.上.下移动一格(不能移动到方格外),也不能进入行坐 ...

  5. 【剑指Offer】面试题22. 链表中倒数第k个节点

    题目 输入一个链表,输出该链表中倒数第k个节点.为了符合大多数人的习惯,本题从1开始计数,即链表的尾节点是倒数第1个节点.例如,一个链表有6个节点,从头节点开始,它们的值依次是1.2.3.4.5.6. ...

  6. Spring入门之二-------SpringIoC之实例化Bean以及注入Bean

    一.实例化Bean 1. 通过默认构造方法实创建Bean public class Bean1 { public Bean1() { System.out.println(this.getClass( ...

  7. Arduino -- variables

    Arduino data types and constants. Constants Floating Point Constants Integer Constants HIGH | LOW IN ...

  8. Linux系统中的变量PATH

    PATH 作用 在Linux安装一些可执行文件通常要添加路径环境变量PATH,PATH环境变量通俗的讲就是把程序的路径"备案"到系统中,这样执行这些程序时就不需要输入完整路径,直接 ...

  9. 10 Json(unity3D)

    //写入json文档注意事项: 1.在Asset下要有一个StreamingAssets文件夹 2.在文件夹内,有一个已创建好的json空文档 3.引入命名空间 using Litjson; usin ...

  10. 吴裕雄--天生自然TensorFlow2教程:张量限幅

    import tensorflow as tf a = tf.range(10) a # a中小于2的元素值为2 tf.maximum(a, 2) # a中大于8的元素值为8 tf.minimum(a ...