PAT甲级1049. Counting Ones
PAT甲级1049. Counting Ones
题意:
任务很简单:给定任何正整数N,你应该计算从1到N的整数的十进制形式的1的总数。例如,给定N为12,在1,10, 11和12。
思路:
《编程之美》2.4。
计算每位出现1的次数。所有的加起来就是答案了。
- 如果该位为0。如12012的百位数。 说明永远取不到121xx的形式。那么这个就相当于12000以下的数所有的可能。所以就是就是这样的形式 n(1)xx ,n为[0,11]所以就是12 * 100 ,即1200种可能。
- 如果为1。如12112的百位数。除了之前的1200中,还有121n,n为[0,12]即1212种可能。
- 如果大于1。比如12212的百位数。除了1200种以外。还有12100~12199的100种。总共1300种。
ac代码:
C++
#include<iostream>
#include<cstdio>
#include<string>
#include<algorithm>
#include<queue>
#include<vector>
#include<cstring>
#include<stdio.h>
#include<map>
#include<cmath>
#include<unordered_map>
using namespace std;
int main()
{
int nums;
scanf("%d", &nums);
//handle problem
int res = 0,temp,index = 1,low = 0;
while (nums)
{
temp = nums % 10;
nums /= 10;
if (temp == 0) res += nums * index;
else if (temp == 1) res += nums*index + low + 1;
else res += (nums + 1) * index;
low += temp * index;
index *= 10;
}
//output
printf("%d\n", res);
return 0;
}
PAT甲级1049. Counting Ones的更多相关文章
- 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甲级1049 Counting Ones【规律】
题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805430595731456 题意: 给定n,问0~n中,1的总个数 ...
- 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 ...
- PAT甲级——A1049 Counting Ones
The task is simple: given any positive integer N, you are supposed to count the total number of 1's ...
随机推荐
- Automation Testing - Best Practice(书写规范)
Coding Standards Coding Standards are suggestions that will help us to write automation Scripts code ...
- BAT-快速切换JDK1.6/1.7
Code: @echo OFF rem 修改背景/字体颜色为黑/绿 color 0A rem 自动设置JDK环境变量,本程序不会对 [系统环境变量] 造成破环!不需要重启!! rem 没有%%JAVA ...
- BZOJ - Problem 3622 - 已经没有什么好害怕的了
题意: 给定两个序列$a$和$b$,让它们进行匹配,求出使得$a_i > b_j$的个数比$a_i < b_j$的个数恰好多$k$,求这样的匹配方法数 题解: 这题的各种表示有一点相似又截 ...
- Deploy Openstack with RDO and Change VNC console to Spice
Deploy Openstack with RDO and Change VNC console to Spice host os: centOS 7 server config network an ...
- VMW虚拟机生成的文件说明
VMDK(VMWare Virtual Machine Disk Format)是虚拟机VMware创建的虚拟硬格式,文件存在于VMware文件系统中,被称为VMFS(虚拟机文件系统) NVRAM 非 ...
- TCP封包解包---如有错误,请纠正!
最近遇见很多的关于TCP中封包解包的数据,在TCP节点之间的信息传递,每次传送的内容是结构体,所以每次在传送的时候,要将结构体中的数据进行封包,然后当一端接收到数据之后,要对接收到的buf参数中的数据 ...
- Construct Binary Tree from Inorder and Postorder Traversal (&&Preorder and Inorder Traversal )——数据结构和算法的基本问题
Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume tha ...
- java中Property类的基本用法
1 配置.properties文件 2 获取输入流的方法 1)FileInputStream fi = new FileInputStream(properties文件路径); 2)InputStre ...
- python脚本传入参数--精讲(getopt模块)
1.最常用的sys.argv[],这个不多谈 2.形如 dahu@dahu-OptiPlex-:~/json_folder$ python sub1.py -abb -oaaa --output=ou ...
- mongoDB学习第二天之常用方法
mongoDB LIMIT 和 SKIP 方法 db.colName.find().limit(num) # limit 方法接收一个数字参数,该参数指定读取的记录条数 (db.colName.fi ...