题目

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. request.getParameter() 接收参数中文乱码

    修改tomcat配置文件 设置url编码集 <Connector port="8080" protocol="HTTP/1.1" connectionTi ...

  2. ACM-Work Assignment

    题目描述:Work Assignment   设有n件工作分配给n个人.将工作i 分配给第j 个人所需的费用为Cij.试设计一个算法,为每一个人都分配1 件不同的工作,并使总费用达到最小. 设计一个算 ...

  3. 日期月份是英文转成数字——oracle

    update table01 set F_DATE = to_char(to_date(F_DATE, 'dd-month-yy', 'NLS_DATE_LANGUAGE=AMERICAN'), 'y ...

  4. 51nod 1435:位数阶乘

    1435 位数阶乘 题目来源: CodeForces 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题  收藏  关注 X是一个n位数的正整数 (x=a0a1...a ...

  5. apt-get install oracle-java8-installer时Err:7 http://ppa.launchpad.net/webupd8team/java/ubuntu xenial/main amd64 Packages 404 not found

    所有其他网址都有效,而不是amd64端点. 然后,当运行apt-get install oracle-java8-installer时,出现以下错误: Package oracle-java8-ins ...

  6. RecyclerView使用介绍

    来源 http://jinyudong.com/2014/11/13/Introduce-RecyclerView-%E4%B8%80/ 编辑推荐:稀土掘金,这是一个针对技术开发者的一个应用,你可以在 ...

  7. Diligent Engine学习笔记初衷

    2020年过去一个月了,回首过去的一年,工作确实很忙,但是自己个人的技术也没得到什么成长,项目当中一些比较难搞的问题也没得到更深入的研究.思来想去,希望新的一年能改变自己的工作方式,将工作上的事物进一 ...

  8. Swift保存RSA密钥到Keychain

    https://www.jianshu.com/p/c1e9bffc76f4 最近项目的需求用到RSA的加密解密,并且需要把公钥信息保存到Keychain里面,网上很多文章都是用Keychain保存账 ...

  9. statement 、prepareStatement的用法和解释

    转自:http://blog.csdn.net/QH_JAVA/article/details/48245945   一.prepareStatement 的用法和解释 1.PreparedState ...

  10. 数组空值empty

    Array构造函数只带一个数字参数时(否则是作为填充),该参数会被作为数组的预设长度,而非填充一个元素,因此数组内是空单元 如果一个数组中存在一个空单元,即length的值大于实际单元数,这样的数组称 ...