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:

12

Sample Output:

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)

 #include<cstdio>
#include<stack>
#include<cstring>
#include<iostream>
#include<stack>
#include<set>
#include<map>
using namespace std;
//count的最大值是1036019223
int main(){
int n;
scanf("%d",&n);
long long base=;
long long count=;
int frpart,afpart,a;
while(n>=base){
a=n/base%;
frpart=n/(*base);
afpart=n%base;
count+=frpart*base;
if(a==){
count+=afpart+;
}
else if(a>){
count+=base;
}
base*=;
}
printf("%lld\n",count);
return ;
}

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. RS485波特率问题

    转载请注明出处:http://blog.csdn.net/qq_26093511/article/details/51683648 最近再做一个项目,发现485不能发送数据,感到非常奇怪!后来查阅相关 ...

  2. python 基础 字典 增删改查

    content = {"name":"wd","pc":{"phone":111111,"age": ...

  3. maven可用镜像

    <mirrors> <mirror> <id>alimaven</id> <name>aliyun maven</name> & ...

  4. MySQL绿色版的安装步骤

    由于工作需要最近要开始研究MySQL了(看来学习都是逼出来的),本人对mysql没有研究,可以说一个小白. 下面就从安装开始吧,虽然网上关于这方面的东西很多,还是需要自己把操作过程写下来. 1.数据库 ...

  5. [凸包]Triangles

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

  6. 初始String

    --------------siwuxie095                             使用频繁操作繁琐的数据有哪些?                 对于基本数据类型:int.ch ...

  7. hdu1077

    #include<iostream> #include<cmath> using namespace std; struct Point { double x,y; }; do ...

  8. HN669打包工具--调试文档

    调试有两种方式,一是直接在游戏工程上面调试,这比较麻烦,需要根据插件配置文件和脚本文件去配置好工程选项后,才能调试.简单一点就是通过脚本文件打包后会有生成游戏工程对应每个渠道的工程. 如下图:这个工程 ...

  9. 谈谈Java异常处理这件事儿

    此文已由作者谢蕾授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 前言 我们对于"异常处理"这个词并不陌生,众多框架和库在异常处理方面都提供了便利,但是对于 ...

  10. Libvirt外部快照

    外部快照的创建 实验环境 CentOS 7 升级QEMU CentOS 7自带的qemu版本太低需要升级 $ sudo yum install -y gcc $ sudo yum install -y ...