A1049. Counting Ones
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
#include<cstdio>
#include<iostream>
using namespace std;
int main(){
long long N, a = , ans = , left, mid, right;
scanf("%lld", &N);
while(N / (a / ) != ){
left = N / a;
mid = N % a / (a / );
right = N % (a / );
if(mid == )
ans += left * (a / );
else if(mid == )
ans += left * (a / ) + right + ;
else if(mid > )
ans += (left + ) * (a / );
a *= ;
}
printf("%d", ans);
cin >> N;
return ;
}
总结:
1、本题应寻找规律完成。对于数字abcde来说,讨论第c位为1的有多少个数字,分为左边:ab,中间c, 右边de。当c = 1时,ab可以取0到ab的任意数字,当取0到ab - 1时,de位任意取。当ab取ab时,de位只能从0 到 de, 故共有ab * 1000 + de + 1个。 当c = 0时,为了让c能 = 1, ab位只能取0 到 ab - 1共ab种可能,de位任取,故共有ab*1000种可能。 当c > 1时,ab位可以取0到ab共ab+1种可能,de任取,故共有 (ab + 1)* 1000种可能。
A1049. Counting Ones的更多相关文章
- PAT甲级——A1049 Counting Ones
The task is simple: given any positive integer N, you are supposed to count the total number of 1's ...
- PAT甲级题解分类byZlc
专题一 字符串处理 A1001 Format(20) #include<cstdio> int main () { ]; int a,b,sum; scanf ("%d %d& ...
- 萌新笔记——Cardinality Estimation算法学习(二)(Linear Counting算法、最大似然估计(MLE))
在上篇,我了解了基数的基本概念,现在进入Linear Counting算法的学习. 理解颇浅,还请大神指点! http://blog.codinglabs.org/articles/algorithm ...
- POJ_2386 Lake Counting (dfs 错了一个负号找了一上午)
来之不易的2017第一发ac http://poj.org/problem?id=2386 Lake Counting Time Limit: 1000MS Memory Limit: 65536 ...
- ZOJ3944 People Counting ZOJ3939 The Lucky Week (模拟)
ZOJ3944 People Counting ZOJ3939 The Lucky Week 1.PeopleConting 题意:照片上有很多个人,用矩阵里的字符表示.一个人如下: .O. /|\ ...
- find out the neighbouring max D_value by counting sort in stack
#include <stdio.h> #include <malloc.h> #define MAX_STACK 10 ; // define the node of stac ...
- 1004. Counting Leaves (30)
1004. Counting Leaves (30) A family hierarchy is usually presented by a pedigree tree. Your job is ...
- 6.Counting Point Mutations
Problem Figure 2. The Hamming distance between these two strings is 7. Mismatched symbols are colore ...
- 1.Counting DNA Nucleotides
Problem A string is simply an ordered collection of symbols selected from some alphabet and formed i ...
随机推荐
- ExtJS初探:在项目中使用ExtJS
注意:本文写作时间是 2013 年,所讲的 ExtJS 如今早已过时,请勿学习! -------------------------------- 今天ExtJS官网发布了ExtJS最新正式版4.2. ...
- [T-ARA][ORGR]
歌词来源:http://music.163.com/#/song?id=29343993 作曲 : 4번타자/에스킴 [作曲 : 4p/beon-Ta-c/ja-/e-seu-Kim] 作词 : 4번 ...
- 牛客网-小白月赛6-J-洋灰三角
题目链接https://www.nowcoder.com/acm/contest/136/J 这题我还是不找规律了,老老实实推吧,传说找规律也可以,我还是算了 递推式:f(n)=k*f(n-1)+p ...
- 树的最长链-POJ 1985 树的直径(最长链)+牛客小白月赛6-桃花
求树直径的方法在此转载一下大佬们的分析: 可以随便选择一个点开始进行bfs或者dfs,从而找到离该点最远的那个点(可以证明,离树上任意一点最远的点一定是树的某条直径的两端点之一:树的直径:树上的最长简 ...
- 第二次作业 --- 我对QQ的评测
腾讯QQ(简称“QQ”)是腾讯公司开发的一款基于Internet的即时通信(IM)软件.腾讯QQ支持在线聊天.视频通话.点对点断点续传文件.共享文件.网络硬盘.自定义面板.QQ邮箱等多种功能,并可与多 ...
- Daily Scrumming* 2015.12.21(Day 13)
一.团队scrum meeting照片 大部分成员编译请假,故今天没有开scrum meeting 二.成员工作总结 姓名 任务ID 迁入记录 江昊 无 无 任务说明: 今日准备编译测验,请假 遇到问 ...
- 使用docker安装paddlepaddle 和 tensorflow
1.tensorflow安装 http://blog.csdn.net/freewebsys/article/details/70237003 (1)拉取镜像:docker pull tensorfl ...
- github第一次作业链接
https://github.com/xuhuzi/test/blob/master/test1 https://github.com/xuhuzi/test/blob/master/test2 ht ...
- C#中byte[] 与string相互转化问题
using System; using System.IO; using System.Security.Cryptography; namespace ShareX.UploadersLib.Oth ...
- 关于Windows 2019 antimalware 进程占用CPU 过多的处理方法 关闭windows 病毒防护的方法
0. 客户端打开报错 重启之后 响应速度很慢. 解决办法: 1. 打开组策略 gpedit.msc 2. 选择位置为 3. 查看 进程里面后台程序 antimalware 进程消失 即可