要统计1到N之间‘1’的个数,如数11包含2个1.所以当N=12时,答案为5。

思想:

找规律,假设ans[N]表示1到N的‘1’的个数,则有a[100]=(a[10]-1)*9+10+a[10]-1+1;

先打表求出1ek的答案;

然后对N由高到低逐位拆分。

有种情况要特别注意:

当N=100001时,高位出现1时要累加到后面第一个非0位数上。

#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include <algorithm>
#include "malloc.h"
#include <cstring>
using namespace std;
#define LL long long
LL a[20]={0,1,2,21};//0, 1, 10,100,...
LL b[20]={1,10,100};
int main()
{
int i=3,j=2,n,cnt=0;
LL c=(1<<30),x,ans=0;
while(b[i-1]<=c){
a[i]=(a[i-1]-1)*9+b[i-2]+a[i-1]-1+1;
b[i]=b[i-1]*10;
i++;
// printf("%d %lld %lld %lld\n",i-1,a[i-1],b[i-1],c);
}
n=i;
scanf("%lld",&x);
if (x<10)
{
printf("1\n");
return 0;
}
j=0;
while(b[j]<=x)j++;
j--;
int flag=0;
while(x>0)
{// ans+=b[j]+(a[j+1]-1)+(n-1)*(a[j+1]-1);
n=x/b[j];
if(n==0);
else if(n==1)
ans+=(flag*x+a[j+1]),flag=0;
else
ans+=(flag*x+(n-1)*(a[j+1]-1)+b[j]+(a[j+1]-1)),flag=0;
if(n==1)flag=1;
x-=n*b[j--];
}
printf("%lld\n",ans);
return 0;
}

pat 1049 Counting Ones的更多相关文章

  1. PAT 1049 Counting Ones[dp][难]

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

  2. PAT 1049 Counting Ones [难]

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

  3. pat 1049. Counting Ones (30)

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

  4. PAT甲级1049. Counting Ones

    PAT甲级1049. Counting Ones 题意: 任务很简单:给定任何正整数N,你应该计算从1到N的整数的十进制形式的1的总数.例如,给定N为12,在1,10, 11和12. 思路: < ...

  5. PAT 解题报告 1049. Counting Ones (30)

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

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

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

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

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

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

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

  9. PAT甲级1049 Counting Ones【规律】

    题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805430595731456 题意: 给定n,问0~n中,1的总个数 ...

随机推荐

  1. [Angular 2] Injecting a Service

    Using Services in Angular 2 is very simple. This lesson covers how to create a simple class as a Ser ...

  2. java接口传递数据的实例

    我们要讲E类中的数据变化通知A类,这样通过接口F来实现.具体原理就是E的每次数据改变都让其通知接口:而A类继承接口,所以每次E的调用接口都会触发A类的数据更改事件的触发. 首先创建一个类E: publ ...

  3. AIX 常用命令和知识

      BOOTLIST:#bootlist -m normal -o (查看bootlist)#bootlist -m normal (设置bootlist为空,谁要在我机器上执行我就要哭了)#boot ...

  4. Linux基础知识(二)

    1. 请回答,32位和64位有什么区别呢?什么时候安装32位的,又什么时候安装64位操作系统呢?如何查看系统是32位的还是64位的? 所谓的32位.64位指的是CPU的GPRs(General-Pur ...

  5. Javascript基础引用类型之Object

    虽然说ECMAScript也是一门对象语言,但是它和其他面向对象语言还是有区别的,它不具有类和接口等基本结构.所以在ECMAScript中一般说类指的是引用类型.创建Object实例的方式有两种: 第 ...

  6. vc2015 编译libcurl带openssl

    1.先编译zlib下载地址 http://zlib.net/ 我这边vc2015编译需要配置环境变量,不知道是装了wdk的原因还是多个vc版本的原因 设置环境变量lib和include路径 INCLU ...

  7. push方法的页面间跳转--

    一,自定义动画写push方法-- 添加coreGraphics.framework框架 在CATransitionAnimation.h文件里面引入-- #import <QuartzCore/ ...

  8. centos中的配置文件

    /etc/profile:此文件为系统的每个用户设置环境信息,当用户第一次登录时,该文件被执行.并从/etc/profile.d目录的配置文件中搜集shell的设置. /etc/bashrc:为每一个 ...

  9. phpMyAdmin中mysql的创建数据库时的编码的问题

    转载自新浪博客    Sean 一. mysql中utf8编码的utf8_bin,utf8_general_cs,utf8_general_ci的区别 utf8_general_ci 不区分大小写,这 ...

  10. Creating a settings table that can handle almost any type of value

    Update: Updated article here. Today I wanted to be able to have a table store any type of value as a ...