1049. Counting Ones (30)

时间限制
10 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:

  1. 12

Sample Output:

  1. 5

提交代码

思路:

统计每位的1的贡献。

对于k位(k>=1):

1.Ak=0,count+=AnAn-1....Ak+1AkAk-1....A1*10^(k-1)

2.Ak=1,count+=AnAn-1....Ak+1AkAk-1....A1*10^(k-1)+Ak-1Ak-2...A1+1

3.Ak>=2,count+=(AnAn-1....Ak+1AkAk-1....A1+1)*10^(k-1)

  1. #include<cstdio>
  2. #include<stack>
  3. #include<cstring>
  4. #include<iostream>
  5. #include<stack>
  6. #include<set>
  7. #include<map>
  8. using namespace std;
  9. //count的最大值是1036019223
  10. int main(){
  11. int n;
  12. scanf("%d",&n);
  13. long long base=;
  14. long long count=;
  15. int frpart,afpart,a;
  16. while(n>=base){
  17. a=n/base%;
  18. frpart=n/(*base);
  19. afpart=n%base;
  20. count+=frpart*base;
  21. if(a==){
  22. count+=afpart+;
  23. }
  24. else if(a>){
  25. count+=base;
  26. }
  27. base*=;
  28. }
  29. printf("%lld\n",count);
  30. return ;
  31. }

pat1049. Counting Ones (30)的更多相关文章

  1. 1004. Counting Leaves (30)

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

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

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

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

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

  4. PAT1049:Counting Ones

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

  5. PTA 1004 Counting Leaves (30)(30 分)(dfs或者bfs)

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

  6. pat1004. Counting Leaves (30)

    1004. Counting Leaves (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A fam ...

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

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

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

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

  9. PAT 1004 Counting Leaves (30分)

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

随机推荐

  1. myeclipse保存时弹出Building workspace

    最近做项目,每次保存修改的东西.myeclipse都会building workspace(重新编译)一下.并且那 building的速度真不够慢的啊. 严重影响编程速度. 在网上也发现遇到此问题的很 ...

  2. Ruby中的%表示法

     %{String}  用于创建一个使用双引号括起来的字符串,这个表示法与%Q{String}完全一样 result = %{hello} puts "result is: #{result ...

  3. Sql server 备份及还原

    --最常用的几种备份方法: --数据备份----------------------- --数据库级:完整数据库备份 差异数据库备份 --文件级: 完整文件备份 差异文件备份 --日志备份------ ...

  4. 2.JasperReports学习笔记2-创建简单的报表例子

    转自:http://www.blogjava.net/vjame/archive/2013/10/12/404908.html 一.创建简单的jrxml文件 这里可以手动创建jrxml文件,也可以使用 ...

  5. <正则吃饺子> :关于Java的native方法(转)

    感谢作者的分享,原文地址:http://blog.csdn.net/wike163/article/details/6635321 一. 什么是Native Method   简单地讲,一个Nativ ...

  6. hadoop mapreduce 计算平均气温的代码,绝对原创

    1901 46 1902 21 1903 48 1904 33 1905 43 1906 47 1907 31 1908 28 1909 26 1910 35 1911 30 1912 16 1913 ...

  7. [转]VS 2013 未找到与约束contractname Microsoft.VisualStudio.Utilities.IContentTypeRegistryService...匹配的导出

    前几天,将Visual studio 2013 update 3 升级到了update 5.打开原来的解决方案,出现了 未找到与约束 contractname Microsoft.VisualStud ...

  8. [Uva10641]Barisal Stadium(区间dp)

    题意:按照顺时针给出操场的周边点,然后给出周围可以建设照明灯的位置,以及在该位置建设照明灯的代价,照明灯照射的范围与操场的边界相切,现在要求一个最小的花费,要求操场的所有边都被照射到. 解题关键:预处 ...

  9. [凸包]Triangles

    https://nanti.jisuanke.com/t/15429 题目大意:给出平面内$n$个整数坐标点,保证无三点共线.可以进行若干次连线,每次选择一个点对连接线段,但是任意两条线段都不得在给定 ...

  10. [原创]SQL表值函数:返回从当前周开始往回的自定义周数

    一如往常一样,一篇简短博文记录开发过程中遇到的一个问题.初衷都是记录自己的一些Idea,也是希望能够帮助一些凑巧遇到此类需求的问题,这个需求的的开端是因为,要统计最近N周的销售数据. 接下来我们来看看 ...