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 ...
随机推荐
- 重建索引解决mssql表查询超时的问题
表已有数据,150万+,执行一个group by 的查询出现超时,一个一个条件减少尝试,前几个where条件不超时,而在加上最后一个条件时就超时了. 分析表的索引建立情况:DBCC showconti ...
- Netdata---Linux系统性能实时监控平台部署记录
通常来说,作为一个Linux的SA,很有必要掌握一个专门的系统监控工具,以便能随时了解系统资源的占用情况.下面就介绍下一款Linux性能实时监测工具-Netdata,它是Linux系统实时性能监测工具 ...
- 实践——ELF文件格式分析
一.分析文件头 1. 段入口类型定义(/usr/include/elf.h)下面产生的hello是32位的 使用命令#Hexdump –x ELF_1.o 第一行: 前4字节,蓝色部分,是一个魔数,表 ...
- 第三个Sprint冲刺第6天
成员:罗凯旋.罗林杰.吴伟锋.黎文衷
- (Alpha)Let's-M1后分析报告
Chronos团队Let's项目 Postmortem结果 设想和目标 1. 我们的软件要解决什么问题?是否定义得很清楚?是否对典型用户和典型场景有清晰的描述? 在最初的用户需求和市场调研方面,团队进 ...
- [2017BUAA软工]第0次作业
第0次作业 Part 1:结缘计算机 1. 你为什么选择计算机专业?你认为你的条件如何?和这些博主比呢? 我跟这篇博客中的作者相似的地方在于,我们都在一个比较早的阶段接触了计算机,我家乡的经济在全国来 ...
- shell脚本--数值计算
原生bash不支持简单的数学运算,即使是最简单的加减乘除 但是,可以使用$[]和expr来实现整数运算 如果要实现小数运算,可以使用bc命令 使用$[]来实现: #!/bin/bash #文件名:te ...
- [转帖]JavaEE中Web服务器、Web容器、Application服务器区别及联系
JavaEE中Web服务器.Web容器.Application服务器区别及联系 https://www.cnblogs.com/vipyoumay/p/5853694.html 在JavaEE 开发W ...
- Linux 文件系统概览
本文导航 -定义07% -文件系统的基本功能12% -目录结构26% -Linux 统一目录结构50% -文件系统类型74% -挂载81% -结论90% -下个月92% 本文旨在高屋建瓴地来讨论 ...
- HTTP协议整理
一.概念 1.HTTP协议:即超文本传输协议(Hypertext transfer protocol).是一种详细规定了浏览器和Web服务器之间互相通信的规则,它允许将超文本标记语言(HTML)文档从 ...