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. 20145317彭垚 《Java程序设计》第8周学习总结

    20145317彭垚 <Java程序设计>第8周学习总结 教材学习内容总结 第十四章 1.NIO的定义 InputStream.OutputStream的输入输出,基本上是以字节为单位进行 ...

  2. Apache服务器代理与缓存

    Apache服务器代理与缓存 1.代理  正向代理:             反向代理:   mod_proxy模块 apache实现代理和网关的关键模块.加载mod_proxy模块:LoadModu ...

  3. GenderGuesser

    http://www.hackerfactor.com/GenderGuesser.php#Analyze

  4. Kafka可靠性的思考

    首先kafka的throughput 很牛逼,参考:http://engineering.linkedin.com/kafka/benchmarking-apache-kafka-2-million- ...

  5. phpcms v9模版调用代码大全(全面而实用)

    首页调用栏目 {pc:content action="category" siteid="$siteid" num="15" order=& ...

  6. java jmx

    http://blog.csdn.net/qiao000_000/article/details/6063949 一.JMX简介 什么是JMX?在一篇网文中是这样说的:"JMX(Java M ...

  7. 巧用AWK处理二进制数据文件

    AWK是Unix下的一款功能强大的文本格式化和抽取工具.利用这个工具,可以对复杂的文本文件进行整理,提取其中的全部或者部分数据,按照需要的格式予以显示.需要说明的是,AWK的强大功能只针对纯文本文件. ...

  8. c signal

    信号是Linux编程中非常重要的部分,本文将详细介绍信号机制的基本概念.Linux对信号机制的大致实现方法.如何使用信号,以及有关信号的几个系统调用. 信号机制是进程之间相互传递消息的一种方法,信号全 ...

  9. HttpContext为null new HttpContextWrapper(System.Web.HttpContext.Current)

    HttpContext = (context == null ? new HttpContextWrapper(System.Web.HttpContext.Current) : context);

  10. Python 扫面文件夹中的文件

    #coding=utf-8 import os,sys reload(sys) sys.setdefaultencoding("utf-8") def scan_files_sub ...