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的总个数 ...
随机推荐
- 设计模式之Application Programs and Toolkits
Application Programs 应用程序 If you're building an application programsuch as a document editor or spre ...
- QlikView实现部分载入数据的功能(Partial Load)
问题背景: 一直非常想不通,公司花了N多钱请了一帮QlikView的Consultant做出来的solution居然没有涉及Reload的部分,以至于每次刷新数据都须要刷新整个Data Model,之 ...
- android AsyncTask 详细例子(2)
超时处理 001 import java.util.Timer; 002 import java.util.TimerTask; 003 004 import android.app.Activi ...
- WebSphere之wasprofile.sh使用
概要文件(profile) 6.0版本以后才有profile,目的是将用户数据和was本身的文件分开,这样可以定义多个profile,每个profile相当于一个用户,相当于提供了多用户的支持. pr ...
- IPMI 配置BMC用户设置
IPMI 配置BMC用户设置 本文档共介绍5条ipmi设置user的命令,这些命令需要使用root权限才能使用,其中- H为需要操作的BMC ip,-I lanplus为使用rmcp+协议发送命令,- ...
- (转)web.config详解之在文件中配置网站默认页面
在<configuration></configuration>中添加下面的配置 <system.webServer> <defaultDocu ...
- C#中new和override的区别
override是指“覆盖”,是指子类覆盖了父类的方法.子类的对象无法再访问父类中的该方法.(签名必须相同) new是指“隐藏”,是指子类隐藏了父类的方法,当然,通过一定的转换,可以在子类的对象中访问 ...
- Geodatabase - 打开要素类
string dbPath = @"G:\doc\gis\1.400\data\pdb.mdb"; ESRI.ArcGIS.Geodatabase.IWorkspaceFactor ...
- Jquery去除从数据库中查询到的内容含有的p标签
$("#topic_content").html($("#topic_content").text()); 如果这个数据是通过循环遍历出的数据,就需要下面这个代 ...
- SurfaceFlinger
D:\linux\ubuntu\touch\libhybris\libhybris_0.1.0+git20130606+c5d897a.orig\libhybris-0.1.0+git20130606 ...