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)的更多相关文章

  1. PAT 解题报告 1004. Counting Leaves (30)

    1004. Counting Leaves (30) A family hierarchy is usually presented by a pedigree tree. Your job is t ...

  2. 【PAT甲级】1049 Counting Ones (30 分)(类似数位DP思想的模拟)

    题意: 输入一个正整数N(N<=2^30),输出从1到N共有多少个数字包括1. AAAAAccepted code: #define HAVE_STRUCT_TIMESPEC #include& ...

  3. PAT (Advanced Level) 1049. Counting Ones (30)

    数位DP.dp[i][j]表示i位,最高位为j的情况下总共有多少1. #include<iostream> #include<cstring> #include<cmat ...

  4. pat 甲级 1049. Counting Ones (30)

    1049. Counting Ones (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The tas ...

  5. PAT 甲级 1049 Counting Ones (30 分)(找规律,较难,想到了一点但没有深入考虑嫌麻烦)***

    1049 Counting Ones (30 分)   The task is simple: given any positive integer N, you are supposed to co ...

  6. 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 ...

  7. pat 1049. Counting Ones (30)

    看别人的题解懂了一些些    参考<编程之美>P132 页<1 的数目> #include<iostream> #include<stdio.h> us ...

  8. PAT 解题报告 1052. Linked List Sorting (25)

    1052. Linked List Sorting (25) A linked list consists of a series of structures, which are not neces ...

  9. 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 ...

随机推荐

  1. 页面静态化3 --- 伪静态技术之Apache的rewrite机制

      Apache的rewrite机制: 意思就是,你发送的地址,比如:http://localhost/news-id67.html会被Apache改写成http://localhost/news.p ...

  2. 发布(高程数据)服务,Service Editor界面无LERC格式选项

    [问题描述]: ArcGIS Server 发布(高程数据)服务,无 LERC格式选项,而官方帮助中发布流程提示需要选择LERC格式. [解决办法]: 需求:发布高程数据,ArcGIS Server版 ...

  3. JBoss的安装与配置(对应eclipse配置)【转】

    安装JBoss纯粹是目的就是学习EJB3...至少现在是这样的 EJB需要运行在EJB容器中.每个J2EE应用服务器都含有EJB容器和Web容器.这样,既支持运行EJB,也可以运行Web应用 目前EJ ...

  4. js 定时跳转, 格式化字符串时间

    效果 1.js中将一字符串表示的系统时间转换为Date时间对象 //js中将一串字符串转换为date类型,主要是先过滤字符,然后分割开 function parseToDate(strTime) { ...

  5. WIN10 ANDROIDSTUDIO1.2 安装完首次启动报错

    环境 ACER NOTEBOOK  WIN10 ANNDROID 1.2 解决方案: 在Android Studio安装目录下的 bin 目录下,找到 idea.properties 文件,在文件最后 ...

  6. PIVOT&UNPIVOT

    如果是家电销售员,那么可能需要统计每月日销售的彩电.冰箱.空调...最大值.最小值.平均值等 如果你是耳鼻喉科医生,那么可能需要统计月度年度日接客咽炎.喉炎.鼻炎...最大值.最小值.平均值等 如果你 ...

  7. yii2.0框架中session与cookie的用法

    我们在开发项目中南面使用到session给和cookie,那么在yii中有他自己的规则 如下案例: session使用     function actionS1(){         echo $t ...

  8. pushViewController addSubview presentModalViewController视图切换

    1.pushViewController和popViewController来进行视图切换,首先要确保根视图是NavigationController,不然是不可以用的, pushViewContro ...

  9. MongoDB过过瘾

    MongoDB 中默认的数据库为 test,连接后尝试以下操作 连接 插入数据:用过json的同学看到这格式相信不会陌生吧! db.person.insert({}) db.person.insert ...

  10. 小米1plus MIUI RadioButton的问题

    小米1plus MIUI RadioButton不能设置setBackground(Drawable drawable);会变成黑色背景,需要单独处理