1049. Counting Ones (30)

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:

  1. 12

Sample Output:

  1. 5
题意

给定一个正整数 N(<=230),要求计算所有小于 N 的正整数的各个位置上,1 出现的次数之和。

分析

比较有思维难度的一题,核心在于找规律。10ms 的时间限制表明了不能用常规的循环遍历来解决。需要从简单的 case 找规律,逐步扩大到常规的情况。

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <string>
  4. #include <cstring>
  5. #include <cmath>
  6. #include <algorithm>
  7. //规律0~9有1个1;0~99有20个1;0~999有300个1...
  8. using namespace std;
  9. #define UP 10
  10. int a[UP] = {};
  11. void mka(){
  12. for (int i=; i<UP; i++) {
  13. double tmp = pow(, i-);
  14. a[i] = tmp * i;
  15. }
  16. }
  17. int getD(int n) {//得出几位数,123是3位数
  18. int cnt = ;
  19. while (n!=) {
  20. n/=;
  21. cnt++;
  22. }
  23. return cnt;
  24. }
  25.  
  26. int main()
  27. {
  28. mka();
  29. int N;
  30. scanf("%d", &N);
  31. int sum = ;
  32. while (N != ) {//每次循环处理最高位
  33. if (N< && N>=) {
  34. sum += ;
  35. break;
  36. }
  37. int d = getD(N);//d位数
  38. double tmp = pow(, d-); int t = tmp;
  39. int x = N / t;//最高位的数字
  40. if (x ==) {
  41. sum += N - x*t + ;
  42. }
  43. else if (x>) {
  44. sum += t;
  45. }
  46. sum += x*a[d-];
  47. N -= x*t;//去掉最高位,处理后面的数
  48. }
  49. printf("%d", sum);
  50.  
  51. return ;
  52. }

PAT 解题报告 1049. Counting Ones (30)的更多相关文章

  1. PAT 解题报告 1004. Counting Leaves (30)

    1004. Counting Leaves (30) A family hierarchy is usually presented by a pedigree tree. Your job is t ...

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

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

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

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

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

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

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

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

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

  7. pat 1049. Counting Ones (30)

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

  8. PAT 解题报告 1052. Linked List Sorting (25)

    1052. Linked List Sorting (25) A linked list consists of a series of structures, which are not neces ...

  9. PAT 解题报告 1051. Pop Sequence (25)

    1051. Pop Sequence (25) Given a stack which can keep M numbers at most. Push N numbers in the order ...

随机推荐

  1. (一)win7下cocos2d-x 21 + vs2010

    1.下载SDK http://cocos2d.cocoachina.com/download,我下载2.1版本,cocos2d-2.1rc0-x-2.1.2-hotfix.zip @ Apr.08, ...

  2. github 有名的问题【ERROR: Permission to .git denied to user】

    小乌龙 以前一直是单兵做战,所以github repo对于我而言,只是一个存放.同步.备份代码的地方,协同作用完全没有体现出来. 最近跟朋友一起开发一个项目,他在github建了个公共的repo,我正 ...

  3. 页面静态化1 --- 概念(Apache内置压力测试工具使用方法)

    三个概念 ①静态网址: http://127.0.0.1/regis.html ②动态网址:在请求页面时可以动态的传一些值进去. http://127.0.0.1/regis.php?id=45&am ...

  4. maven 的一些基本操作

    maven  install :把打出的包装载到本地仓库,package:是打包的意思 每当项目中的模块里的东西发生变化的时候,先install一下项目 ,在启用maven的tomcat插件就不会报错 ...

  5. SQuirreL 连接 hive

    软件安装版本: hadoop-2.5.1 hbase-0.98.12.1-hadoop2 apache-hive-1.2.1-bin SQuirreL SQL Client3.7 集成步骤: 1. S ...

  6. v2.0

    #include <stdio.h>#include <string.h>#include <ctype.h>typedef struct node{ char l ...

  7. qt如何实现一个渐隐窗口呢(开启的时候他是从上往下渐渐显示)

    qt如何实现一个渐隐窗口呢?就是比如说开启的时候他是从上往下渐渐显示的,关闭的时候从下往上渐渐小时的http://stackoverflow.com/questions/19087822/how-to ...

  8. 【Android测试】【第十节】MonkeyRunner—— 录制回放

    ◆版权声明:本文出自胖喵~的博客,转载必须注明出处. 转载请注明出处:http://www.cnblogs.com/by-dream/p/4861693.html 前言 在实际项目进行过程中,频繁的需 ...

  9. jQuery 遍历json数组的实现代码

    <script type="text/javascript"> "}]; $(d1).each(function(){ alert(this.text+&qu ...

  10. ArcSoft's Office Rearrangement---hdu5933

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5933 题意:给你一个数组含有n个数,然后把这些数分为k份,每份都相等:有两个操作:合并相邻的两个数:把 ...