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:
12
Sample Output:
5
对于此类问题,应当从找一般化的规律为主,而不应当试图推导排序组合的数学表达式。
这道题目的难点在于每一位都有出现1的可能,例如100,010,001,110等,如果按照排序组合考虑,因为要计数1的个数,因此对于1的个数不同,要分别对待,这将会是很棘手的问题。
如果换个思路,从每一位入手,研究每一位的规律,则会使得问题简单得多,这种研究方法是科学的,因为每一位都考虑了自己的1,那么合起来对于多个1的问题就自然考虑进来了,例如N=12时,我们分别考虑下面的情况:
个位出现1的有1,11,十位出现1的有10,11,12,我们看到,对于11这种多个1的情况,正好计算了两次,对于多位也是这个道理。
下面我们从一个5位数入手,研究百位不同时,百位为1的所有情况的计算:
我们以12045、12145、12245为例。
12045:
百位为0,那么高位不变低位部分无法产生类似1XX的数字,因此考虑高位,注意到只要比当前值小就可以,因此可选择:00100-00199,01100-01199 ... 11100-11199 一共12*100=1200,观察到等于比当前高位的数字*100。
也就是:高位数字*100,注意到这个100和当前考虑的位有关,百位为100,从低位为第1位开始计算第x位为10的x次方,也就是说对不同的位这个100应该换成相应的值。
12145:
百位为1,那么高位不变,低位从100-145都可以,共45+1=46个,高位仍然可以产生百位为0时的变化,共1200+46+1246种
也就是:高位数字*100 + 低位数字+1
12245:
对于百位大于1的情况,我们只需要考虑高位即可列全所有,从00100-00199到12100-12199,共13*100=1300
也就是:(高位数字 + 1)*100
解决了这个问题,下面要解决的是取出高位数、当前位、低位数的问题,我们以6位数abcdef取出百位为例:
对于abcdef,设因数factor=100,即要取出百位d
按照常规方法,百位 = abcdef / factor % 10 = abcd % 10 = d
高位数字 = abcdef / factor / 10 = abc
低位数字 = abcdef - abcd00 = ef
而abcd00 = (abcdef / factor) * factor = abcd00
所以低位数字 = abcdef - ( abcdef / factor ) * factor
这时候,factor就是前面我们要找的从低位开始算的10的x次方,因此编写代码变得简单,只需要从低位开始,也就是factor从1开始,每次乘以10,直到超出当前值,此时N/factor=0,停止运算。
代码如下:
#include <iostream>
#include <stdio.h> using namespace std; int compute(int N){
int factor = 1;
int low = 0;
int high = 0;
int now = 0;
int cnt = 0; while(N / factor != 0){
now = (N / factor) % 10;
high = N / factor / 10;
low = N - (N / factor) * factor; switch(now){
case 0:
cnt += high * factor;
break;
case 1:
cnt += high * factor + low + 1;
break;
default:
cnt += (high + 1) * factor;
} factor *= 10; } return cnt; } int main()
{
int N;
while(scanf("%d",&N) != EOF){
cout << compute(N) << endl;
} return 0;
}
1049. Counting Ones (30)的更多相关文章
- PAT 解题报告 1049. Counting Ones (30)
		1049. Counting Ones (30) The task is simple: given any positive integer N, you are supposed to count ... 
- pat 甲级 1049. Counting Ones (30)
		1049. Counting Ones (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The tas ... 
- PAT 甲级 1049 Counting Ones (30 分)(找规律,较难,想到了一点但没有深入考虑嫌麻烦)***
		1049 Counting Ones (30 分) The task is simple: given any positive integer N, you are supposed to co ... 
- 【PAT甲级】1049 Counting Ones (30 分)(类似数位DP思想的模拟)
		题意: 输入一个正整数N(N<=2^30),输出从1到N共有多少个数字包括1. AAAAAccepted code: #define HAVE_STRUCT_TIMESPEC #include& ... 
- pat 1049. Counting Ones (30)
		看别人的题解懂了一些些 参考<编程之美>P132 页<1 的数目> #include<iostream> #include<stdio.h> us ... 
- PAT (Advanced Level) 1049. Counting Ones (30)
		数位DP.dp[i][j]表示i位,最高位为j的情况下总共有多少1. #include<iostream> #include<cstring> #include<cmat ... 
- 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 ... 
- 1049 Counting Ones (30分)
		The task is simple: given any positive integer N, you are supposed to count the total number of 1's ... 
- PAT 1049 Counting Ones[dp][难]
		1049 Counting Ones (30)(30 分) The task is simple: given any positive integer N, you are supposed to ... 
随机推荐
- Python中模块之sys的功能介绍
			sys模块的功能介绍 1. sys的变量 argv 命令行参数 方法:sys.argv 返回值:list 例如:test1.py文件中有两句语句1.import sys 2.print(sys.arg ... 
- Jenkins简明入门(三) -- Blue Ocean,让一切变得简单
			我们在上一节Jenkins简明入门(二) 中见识到了Jenkins能做些什么:利用Jenkins完成python程序的build.test.deployment. 同时,也有一种简单的方法,不需要写J ... 
- WPF ListBox/ListView/DataGrid 虚拟化时的滚动方式
			ListBox的滚动方式 分为像素滚动和列表项滚动 通过ListBox的附加属性ScrollViewer.CanContentScroll来设置.因此ListBox的默认模板中,含有ScrollVie ... 
- VMware在宿主上没有VMnet0、VMnet8,解决方法
			一开始,坐着上机实验,一直搞不通为什么虚拟机上的客户机可以ping通自己的ip也可以ping通自己本身的ip,但是主机ping不通虚拟机的客户机,也ping不通虚拟机的网关. 尝试了各种问题,也追出了 ... 
- ACM  Ignatius and the Princess I
			公主被BEelzebub feng5166绑架,我们的英雄Ignatius必须拯救我们美丽的公主. 现在他进入feng5166的城堡.城堡是一个大迷宫.为简单起见,( To make the prob ... 
- Activity的四种启动模式任务栈图解
			转载本专栏文章,请注明出处,尊重原创 .文章博客地址:道龙的博客 今天带来另一篇Activity的文章--Activity的四种启动模式.该篇文章,会以图文讲解的方式带你彻底掌握Activity的启动 ... 
- Eclipse调试(1)——基础篇
			作为使用Eclipse的程序员都会使用它的Debug.但是有不少人只会用F6.F8,其他功能知之甚少.今天我就来总结一下我在使用eclipse的debug时的一些个人经验.水平有限,不足之处还请赐教. ... 
- Linux 性能监测:CPU
			CPU 的占用主要取决于什么样的资源正在 CPU 上面运行,比如拷贝一个文件通常占用较少 CPU,因为大部分工作是由 DMA(Direct Memory Access)完成,只是在完成拷贝以后给一个中 ... 
- Java中获取文件大小的正确方法
			本文出处:http://blog.csdn.net/djy1992/article/details/51146837,转载请注明.由于本人不定期会整理相关博文,会对相应内容作出完善.因此强烈建议在原始 ... 
- 【mybatis深度历险系列】延迟加载
			在前面的博文中,小编主要简单的介绍了mybatis中的高级映射,小伙伴们可以把mybatis和hibernate的因素进行对比,更加有利于理解.今天这篇博文,小编主要来简单介绍一下mybatis中的延 ... 
