pat 1049 Counting Ones

时间:2022-09-01 10:35:45

要统计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&lbrack;dp&rsqb;&lbrack;难&rsqb;

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

  2. PAT 1049 Counting Ones &lbrack;难&rsqb;

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

  3. pat 1049&period; Counting Ones &lpar;30&rpar;

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

  4. PAT甲级1049&period; Counting Ones

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

  5. PAT 解题报告 1049&period; Counting Ones &lpar;30&rpar;

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

  6. pat 甲级 1049&period; Counting Ones &lpar;30&rpar;

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

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

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

  8. PAT &lpar;Advanced Level&rpar; 1049&period; Counting Ones &lpar;30&rpar;

    数位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. redis 常用配置

    参数说明 redis.conf 配置项说明如下: 1. Redis默认不是以守护进程的方式运行,可以通过该配置项修改,使用yes启用守护进程 daemonize no 2. 当Redis以守护进程方式 ...

  2. Python 的 pyinotify 模块 监控文件夹和文件的变动

    官方参考: https://github.com/seb-m/pyinotify/wiki/Events-types https://github.com/seb-m/pyinotify/wiki/I ...

  3. div根据内容改变大小并且左右居中

    div{ display:inline-block; width:auto; } 这个div的父元素text-align:center;

  4. 4&period;13-4&period;17c语言学习

    这周学习开始接触c语言,使用的工具是c-free5,主要是把之前的一些函数流程图通过编写代码实现运行,本周最后一天的作业是做简易的atm机运行逻辑程序,是在main主函数外附加使用void函数,其主要 ...

  5. 移植rom移动TD到联通W

    1.修改build.prop TD为 ril.flightmode.poweroffMD=0 ril.telephony.mode=2 改为 ril.flightmode.poweroffMD=1 r ...

  6. Java Class类以及获取Class实例的三种方式

    T - 由此 Class 对象建模的类的类型.例如,String.class 的类型是Class<String>.如果将被建模的类未知,则使用Class<?>.   publi ...

  7. mysql 经纬度求距离

    SELECT id,lng,lat,ROUND(6378.138*2*ASIN(SQRT(POW(SIN((lat1*PI()/180-lat*PI()/180)/2),2)+COS(lat1*PI( ...

  8. MySQL:1366 - Incorrect string value错误解决办法

    今天使用navicat向MySQL中插入中文时,报错: - Incorrect string value:... 在我自己数据库设计之初,没有设计好字符编码格式的问题. 使用如下语句解决: alter ...

  9. go config

    安装导入 go get github.com/astaxie/beego/config import "github.com/astaxie/beego/config" 使用 配置 ...

  10. pandas选择单元格,选择行列

    首先创建示例df: df = pd.DataFrame(np.arange(16).reshape(4, 4), columns=list('ABCD'), index=list('5678')) d ...