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. Smart ECM数据发布假数据测试工作。

    1. ScriptBom.java//文件方法供接口调用 代码: public String setBomEcnHistoryDataByXML(String strView){//传入arg文件名 ...

  2. php常用Stream函数集介绍

    php常用Stream函数集介绍 作者: 字体:[增加 减小] 类型:转载 时间:2013-06-24   本篇文章是对php中的常用Stream函数集进行了详细的分析介绍,需要的朋友参考下     ...

  3. A20VGA和lvds显示的切换-

    ./fex2bin sys_config_lvds.fex /boot/script.bin sys_config_lvds.fex的作用:配置各种外设,端口,I/O针脚信息的文件 生成 script ...

  4. (转)maven 配置在eclipse中

    maven3 下载配置 下载地址 http://maven.apache.org/download.cgi 在线文档 http://maven.apache.org/ref/3.0.5/ 安装 一.安 ...

  5. jdk Tomcat配置

    安装Tomcat需要先安装JDKJDK安装JDK安装会有两次,两次不能再同一目录 你可以新建两个文件夹 JDK JRE 第一次安装在JDK 第二次在JRE在我的电脑右键属性 高级系统设置 环境变量 系 ...

  6. 【转】CSS(10)盒子模型

    CSS中, Box Model叫盒子模型(或框模型),Box Model规定了元素框处理元素内容(element content).内边距(padding).边框(border) 和 外边距(marg ...

  7. 【总结】使用jdbc+servlet开发一个bug管理系统的经验总结

    开发背景: 公司目前使用Teambition里面的task作为bug管理系统,既没有bug的当前状态,也不能写上bug的详细复现步骤,被assign了任务(该修复bug或者验证bug是否被修复)也没有 ...

  8. nrf51822-添加DFU服务

    以ble_app_uart例子为基础,在其上添加dfu服务. Sdk中的bootloader提供了两个方式来进入升级模式,一种是按键,另一种是手机点击升级. 在bootloader代码相关代码如下 如 ...

  9. 简易自定义下拉菜单 与简易默认下拉html片段

    简易自定义下拉选择 html片段 html: <div class="select_box province"> <div class="selecte ...

  10. Project Management Process

    Project Management ProcessDescription .............................................................. ...