PAT 解题报告 1049. Counting Ones (30)
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
题意
给定一个正整数 N(<=230),要求计算所有小于 N 的正整数的各个位置上,1 出现的次数之和。
分析
比较有思维难度的一题,核心在于找规律。10ms 的时间限制表明了不能用常规的循环遍历来解决。需要从简单的 case 找规律,逐步扩大到常规的情况。
- #include <iostream>
- #include <cstdio>
- #include <string>
- #include <cstring>
- #include <cmath>
- #include <algorithm>
- //规律0~9有1个1;0~99有20个1;0~999有300个1...
- using namespace std;
- #define UP 10
- int a[UP] = {};
- void mka(){
- for (int i=; i<UP; i++) {
- double tmp = pow(, i-);
- a[i] = tmp * i;
- }
- }
- int getD(int n) {//得出几位数,123是3位数
- int cnt = ;
- while (n!=) {
- n/=;
- cnt++;
- }
- return cnt;
- }
- int main()
- {
- mka();
- int N;
- scanf("%d", &N);
- int sum = ;
- while (N != ) {//每次循环处理最高位
- if (N< && N>=) {
- sum += ;
- break;
- }
- int d = getD(N);//d位数
- double tmp = pow(, d-); int t = tmp;
- int x = N / t;//最高位的数字
- if (x ==) {
- sum += N - x*t + ;
- }
- else if (x>) {
- sum += t;
- }
- sum += x*a[d-];
- N -= x*t;//去掉最高位,处理后面的数
- }
- printf("%d", sum);
- return ;
- }
PAT 解题报告 1049. Counting Ones (30)的更多相关文章
- PAT 解题报告 1004. Counting Leaves (30)
1004. Counting Leaves (30) A family hierarchy is usually presented by a pedigree tree. Your job is t ...
- 【PAT甲级】1049 Counting Ones (30 分)(类似数位DP思想的模拟)
题意: 输入一个正整数N(N<=2^30),输出从1到N共有多少个数字包括1. AAAAAccepted code: #define HAVE_STRUCT_TIMESPEC #include& ...
- PAT (Advanced Level) 1049. Counting Ones (30)
数位DP.dp[i][j]表示i位,最高位为j的情况下总共有多少1. #include<iostream> #include<cstring> #include<cmat ...
- 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 Advanced 1049 Counting Ones (30) [数学问题-简单数学问题]
题目 The task is simple: given any positive integer N, you are supposed to count the total number of 1 ...
- pat 1049. Counting Ones (30)
看别人的题解懂了一些些 参考<编程之美>P132 页<1 的数目> #include<iostream> #include<stdio.h> us ...
- PAT 解题报告 1052. Linked List Sorting (25)
1052. Linked List Sorting (25) A linked list consists of a series of structures, which are not neces ...
- PAT 解题报告 1051. Pop Sequence (25)
1051. Pop Sequence (25) Given a stack which can keep M numbers at most. Push N numbers in the order ...
随机推荐
- (一)win7下cocos2d-x 21 + vs2010
1.下载SDK http://cocos2d.cocoachina.com/download,我下载2.1版本,cocos2d-2.1rc0-x-2.1.2-hotfix.zip @ Apr.08, ...
- github 有名的问题【ERROR: Permission to .git denied to user】
小乌龙 以前一直是单兵做战,所以github repo对于我而言,只是一个存放.同步.备份代码的地方,协同作用完全没有体现出来. 最近跟朋友一起开发一个项目,他在github建了个公共的repo,我正 ...
- 页面静态化1 --- 概念(Apache内置压力测试工具使用方法)
三个概念 ①静态网址: http://127.0.0.1/regis.html ②动态网址:在请求页面时可以动态的传一些值进去. http://127.0.0.1/regis.php?id=45&am ...
- maven 的一些基本操作
maven install :把打出的包装载到本地仓库,package:是打包的意思 每当项目中的模块里的东西发生变化的时候,先install一下项目 ,在启用maven的tomcat插件就不会报错 ...
- SQuirreL 连接 hive
软件安装版本: hadoop-2.5.1 hbase-0.98.12.1-hadoop2 apache-hive-1.2.1-bin SQuirreL SQL Client3.7 集成步骤: 1. S ...
- v2.0
#include <stdio.h>#include <string.h>#include <ctype.h>typedef struct node{ char l ...
- qt如何实现一个渐隐窗口呢(开启的时候他是从上往下渐渐显示)
qt如何实现一个渐隐窗口呢?就是比如说开启的时候他是从上往下渐渐显示的,关闭的时候从下往上渐渐小时的http://stackoverflow.com/questions/19087822/how-to ...
- 【Android测试】【第十节】MonkeyRunner—— 录制回放
◆版权声明:本文出自胖喵~的博客,转载必须注明出处. 转载请注明出处:http://www.cnblogs.com/by-dream/p/4861693.html 前言 在实际项目进行过程中,频繁的需 ...
- jQuery 遍历json数组的实现代码
<script type="text/javascript"> "}]; $(d1).each(function(){ alert(this.text+&qu ...
- ArcSoft's Office Rearrangement---hdu5933
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5933 题意:给你一个数组含有n个数,然后把这些数分为k份,每份都相等:有两个操作:合并相邻的两个数:把 ...