题目

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. [题解] LuoguP4609 [FJOI2016]建筑师

    传送门 首先对于高度为\(n\)的建筑,他的左边有\(A-1\)个建筑能被看到,右边有\(B-1\)个建筑能被看到,这两者类似,所以先来看左边. 一个建筑将会遮挡住它后面的高度比它矮的建筑,直到一个高 ...

  2. 吴裕雄--天生自然C++语言学习笔记:C++ 指针

    每一个变量都有一个内存位置,每一个内存位置都定义了可使用连字号(&)运算符访问的地址,它表示了在内存中的一个地址. #include <iostream> using namesp ...

  3. 手动搭建简单的vue项目

    创建项目根目录 切换到根目录下 , 并执行 npm init , 所有选项都默认即可. 安装 webpack webpack-cli vue vue-loader 添加项目结构         

  4. c++程序—while猜数字游戏

    #include<iostream> using namespace std; #include<string> #include<ctime> int main( ...

  5. Arduino -- variables

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

  6. JAVAEE 和项目开发(第六课:服务器的安装和目录介绍和闪退解决办法)

    课程介绍: 在学习了 HTTP 协议后,我们对浏览器和服务器的交互流程以及规范有了一定程度的认知,并也有了自己的理解.但是不少同学对服务器的概念还是有些模糊的,那么本节课就针对服务器进行介绍,我们一起 ...

  7. Python安装和虚拟环境创建以及外部库的安装

    Python.虚拟环境.外部库的安装 一 安装Python 1 Windows 到官网下载对应的版本 下载地址 我选择的是Python3.6.8 下载完成后双击运行 !!!勾选Add Python 3 ...

  8. Java平台上的AOP实现机制

    Java平台上的AOP实现机制 动态代理(Dynamic Proxy)机制,在运行期间动态的为相应接口生成对应的代理对象.SpringAop默认情况下采用这种机制来实现AOP机能.缺点:相对于编译后的 ...

  9. 登录之后跳转到登录之前的页面 之 Referer 的坑

    简而言之:通过鼠标在页面上点击链接发送请求,请求header中会包含referer信息, 通过在浏览器地址栏书写并发送请求的,header中不会有referer信息. 为了完成登录动作成功后返回原页面 ...

  10. 关于GAN的一些笔记

    目录 1 Divergence 1.1 Kullback–Leibler divergence 1.2 Jensen–Shannon divergence 1.3 Wasserstein distan ...