PAT甲级1049 Counting Ones【规律】
题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805430595731456
题意:
给定n,问0~n中,1的总个数是多少。
思路:
问的是总个数,所以不需要考虑重复,只用考虑每一位上的贡献就行了。
将数字分成三部分,left(共i位),now和right(共j位)
如果当前now是0, 那么所有前i位是[0,left)的数字都+1个贡献,这些数一共有$left*10^j$个
如果当前now是[2,9],那么所有前i位是[0,left]的数字都+1个贡献,这些数一共有$(left+1)*10^j$个
如果当前now是1,那么这一位是1的数可以是,前i位为[0,left)的所有数,或是前i位刚好是left而后j位是[0,right]的。
一共有$left*10^j+right+1$个
#include<cstdio>
#include<cstdlib>
#include<map>
#include<set>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
#include<cmath>
#include<stack>
#include<queue> #define inf 0x7fffffff
using namespace std;
typedef long long LL;
typedef pair<string, string> pr; LL n;
LL r; int main()
{
LL p = ;
cin>>n;
LL ans = ;
LL tmp = n;
while(n){
if(n % == ){
//cout<<n / 10 * p<<endl;
ans += n / * p;
}
else if(n % == ){
//cout<<(n / 10 * p) + r + 1<<endl;
ans += (n / * p) + r + ;
}
else{
//cout<<(n / 10 + 1) * p<<endl;
ans += (n / + ) * p;
} n /= ;
p *= ;
r = tmp % p;
} cout<<ans<<endl;
return ;
}
PAT甲级1049 Counting Ones【规律】的更多相关文章
- 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 co ...
- pat 甲级 1049. Counting Ones (30)
1049. Counting Ones (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The tas ...
- PAT甲级——A1049 Counting Ones
The task is simple: given any positive integer N, you are supposed to count the total number of 1's ...
- PAT 甲级 1115 Counting Nodes in a BST
https://pintia.cn/problem-sets/994805342720868352/problems/994805355987451904 A Binary Search Tree ( ...
- PAT 甲级 1004 Counting Leaves
https://pintia.cn/problem-sets/994805342720868352/problems/994805521431773184 A family hierarchy is ...
- PAT甲级 1004.Counting Leaves
参考:https://blog.csdn.net/qq278672818/article/details/54915636 首先贴上我一开始的部分正确代码: #include<bits/stdc ...
- PAT甲级——A1115 Counting Nodes in a BST【30】
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...
- PAT甲级——A1004 Counting Leaves
A family hierarchy is usually presented by a pedigree tree. Your job is to count those family member ...
随机推荐
- Linux之搭建远程数据库[Ubuntu:全过程]
1.mariadb在Linux中首次进入mysql (因为此时还没有创建任何用户,mysql的root并不等效于linux中的root用户) sudo apt-get install mysql-se ...
- 出现Failed to get convolution algorithm的解决方法
当运行卷积神经时出现了问题:Failed to get convolution algorithm. This is probably because cuDNN failed to initiali ...
- 【JS】长轮询
循环请求服务器数据 function send () { xhr.open('GET', '/clock', true); xhr.onreadystatechange = function () { ...
- Ch02 课堂作业
测试一:运行结果: 测试二:运行结果: 测试三:运行结果:
- Pycharm新建模板默认添加作者时间等信息
在pycharm使用过程中,对于每次新建文件的shebang行和关于代码编写者的一些个人信息快捷填写,使用模板的方式比较方便. 方法如下: 1.打开pycharm,选择File-Settings 2. ...
- vue app混合开发蓝牙串口连接(报错java.io.IOException: read failed, socket might closed or timeout, read ret: -1;at android.bluetooth.BluetoothSocket.connect at js/BluetoothTool.js:329)
我使用的uni-app <template> <view class="bluetooth"> <!-- 发送数据 --> <view c ...
- Vertx eventbus模块解析
eventbus 事件總線 協議棧 TCP分包,粘包解決採用方案: 消息定长(定義消息体總长度),消息分为消息头和消息体 dataType bytes description int 4 包体总大小 ...
- #Node.js的fs导入遇到的问题和解决方案
一直在使用VS Code,今天打算用Node.js进行文件IO时候遇到了一些问题,fs是Node.js的核心功能之一,一开始我用Javascript编写fs模块的导入. var fs = requir ...
- ionic 3 icon和splash screen生成和设置
官方文档中介绍 ionic cordova resources命令可以生成应用的图标和启动画面图片(前提是你必须在resources 目录下放icon源文件和splash源文件,格式可以为png, p ...
- labview使用了报表模块,在生成exe时需要添加以下内容,否则打包后不能开启excel功能
1.在你的安装目录下找到文件夹(D:\Program Files (x86)\National Instruments\LabVIEW 2016\vi.lib\Utility\NIReport.llb ...