pat 1049 Counting Ones
要统计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的更多相关文章
- PAT 1049 Counting Ones[dp][难]
1049 Counting Ones (30)(30 分) The task is simple: given any positive integer N, you are supposed to ...
- PAT 1049 Counting Ones [难]
1049 Counting Ones (30 分) The task is simple: given any positive integer N, you are supposed to coun ...
- pat 1049. Counting Ones (30)
看别人的题解懂了一些些 参考<编程之美>P132 页<1 的数目> #include<iostream> #include<stdio.h> us ...
- PAT甲级1049. Counting Ones
PAT甲级1049. Counting Ones 题意: 任务很简单:给定任何正整数N,你应该计算从1到N的整数的十进制形式的1的总数.例如,给定N为12,在1,10, 11和12. 思路: < ...
- PAT 解题报告 1049. Counting Ones (30)
1049. Counting Ones (30) The task is simple: given any positive integer N, you are supposed to count ...
- pat 甲级 1049. Counting Ones (30)
1049. Counting Ones (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The tas ...
- PAT 甲级 1049 Counting Ones (30 分)(找规律,较难,想到了一点但没有深入考虑嫌麻烦)***
1049 Counting Ones (30 分) The task is simple: given any positive integer N, you are supposed to co ...
- PAT (Advanced Level) 1049. Counting Ones (30)
数位DP.dp[i][j]表示i位,最高位为j的情况下总共有多少1. #include<iostream> #include<cstring> #include<cmat ...
- PAT甲级1049 Counting Ones【规律】
题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805430595731456 题意: 给定n,问0~n中,1的总个数 ...
随机推荐
- 退役笔记一#MySQL = lambda sql : sql + ' Source Code 4 Explain Plan '
Mysql 查询运行过程 大致分为4个阶段吧: 语法分析(sql_parse.cc<词法分析, 语法分析, 语义检查 >) >>sql_resolver.cc # JOIN.p ...
- [Javascript] Web APIs: Persisting browser data with window.localStorage
Local Storage is a native JavaScript Web API that makes it easy to store and persist data (as key-va ...
- setTimeout()和setInterval()小结
写在前面:在写H5游戏时经常需要使用定时刷新页面实现动画效果,比较常用即setTimeout()以及setInterval() setTimeout 描述 setTimeout(code,millis ...
- vsim仿真VHDL输出fsdb格式文件
vsim(modelsim)仿真VHDL输出fsdb格式文件 1.Dump准备 (1) 将下列设置放到顶层testbench tb.vhd文件中[注意放置的位置:关系如图] library novas ...
- 在cygwin下编译c语言
#include <stdio.h> int main (void) { printf("Hello World!\n"); ; } 1.保存到cygwin工作目录下 ...
- MySql Update Select 嵌套
UPDATE `TB_CM_Dic` SET `ParentID` = (SELECT `ID` FROM (SELECT * FROM `TB_CM_Dic`) AS B WHERE `DicNam ...
- (转)@@trancount解析
在处理事务的时候,一般都用RollBack Transaction来回滚,但是如果在嵌套事务中这样使用的话,就会出现错误. 在SqlServer里,嵌套事务的层次是由@@TranCount全局变量反映 ...
- Sql Server根据表名获得所有列及其属性
select a.name columnname,c.name as typename,case when a.is_nullable =0 then 'Not Null' else 'Null' e ...
- SDK文件夹下内容介绍
Platform-Tools: 这是 adb, fastboot 等工具包.把解压出来的 platform-tools 文件夹放在 android sdk 根目录下,并把 adb所在的目录添加到系统 ...
- 18 java 代理模式 (转)
静态代理 1.新建一个接口,这个接口所提供的方法是关于数据库操作的 public interface EmployeeDao { public void updateSalary(); } 2.建一个 ...