要统计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. 设置单选的listView或者gridview

    主要是这个BeaseAdapter的方法notifyDataSetChanged()的使用;作用 :调用BaseAdapter中的getView();方法,刷新ListView中的数据.实现:1.在B ...

  2. (转)IIS5.1的安装配置并发布ASP.NET网站

    最近跟老师做一个桥梁养护系统的项目,要求用VS2008+Sql Server2000,服务器用IIS.由于之前做过的ASP.NET项目都是用的VS内置的服务器,并没有使用过IIS,第一次搭,花了几个小 ...

  3. Sql Server根据表名获得所有列及其属性

    select a.name columnname,c.name as typename,case when a.is_nullable =0 then 'Not Null' else 'Null' e ...

  4. 激光推送SSL问题

    1.导出极光推送服务器上的证书,导出后文件扩展名是.cer.  下载极光推送服务器上的证书 2.利用极光推送服务器的证书文件,创建客户端密钥库,密钥库的文件扩展名是.jks Dos command: ...

  5. silverlight+wcf 项目 silverlight获得web程序的参数

    silverlight 可以通过属性InitParams 获得参数,如果参数是动态的需要web程序传递的,具体操作如下: web程序后台:AppID,USERID需要的参数 this.frmRepor ...

  6. ADO.NET中连接SQL Sever

    1.在配置文件中定义数据库连接信息. 在配置文件*.config中添加这段代码在<configuration>与</configuration>之间: <connecti ...

  7. HSSFClientAnchor(int dx1,int dy1,int dx2,int dy2,short col1,int row1,short col2, int row2)

      public HSSFClientAnchor(int dx1, int dy1, int dx2, int dy2, short col1, int row1, short col2, int  ...

  8. Mysql学习(慕课学习笔记3)数据类型

    数据类型 数据类型是指.存储过程参数.表达式和局部变量的数据特征, 它决定了数据的存储格式,代表了不同的信息类型. 整型 Tinyint      有符号位 -128到127   无符号位 0到255 ...

  9. forward 和redirect的区别

    1.从地址栏显示来说 forward是服务器请求资源,服务器直接访问目标地址的URL,把那个URL的响应内容读取过来,然后把这些内容再发给浏览器.浏览器根本不知道服务器发送的内容从哪里来的,所以它的地 ...

  10. js 框架及uI框架

    转载自:http://www.techweb.com.cn/network/system/2015-12-23/2245809.shtml 这篇文章主要描述现今流行的一些 Javascript web ...