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的更多相关文章

  1. pat 甲级 1049. Counting Ones (30)

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

  2. PAT 甲级 1049 Counting Ones (30 分)(找规律,较难,想到了一点但没有深入考虑嫌麻烦)***

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

  3. PAT甲级1049 Counting Ones【规律】

    题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805430595731456 题意: 给定n,问0~n中,1的总个数 ...

  4. PAT 甲级 1115 Counting Nodes in a BST

    https://pintia.cn/problem-sets/994805342720868352/problems/994805355987451904 A Binary Search Tree ( ...

  5. PAT 甲级 1004 Counting Leaves

    https://pintia.cn/problem-sets/994805342720868352/problems/994805521431773184 A family hierarchy is ...

  6. PAT甲级 1004.Counting Leaves

    参考:https://blog.csdn.net/qq278672818/article/details/54915636 首先贴上我一开始的部分正确代码: #include<bits/stdc ...

  7. 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 ...

  8. PAT甲级——A1004 Counting Leaves

    A family hierarchy is usually presented by a pedigree tree. Your job is to count those family member ...

  9. PAT甲级——A1049 Counting Ones

    The task is simple: given any positive integer N, you are supposed to count the total number of 1's ...

随机推荐

  1. ASLR

    @author:dlive ASLR address space layout randomization 微软从windows vista/windows server 2008(kernel ve ...

  2. IT人员必备linux安全运维之Ssh用途、安全性、身份认证以及配置……【转】

    SSH一般用途 提供shell,解决telnet不安全的传输 1.修改默认ssh默认端口 vi /etc/ssh/sshd_config 修改之后重启 >systemctl restart ss ...

  3. 有关mysql的innodb_flush_log_at_trx_commit参数【转】

    一.参数解释 0:log buffer将每秒一次地写入log file中,并且log file的flush(刷到磁盘)操作同时进行.该模式下在事务提交的时候,不会主动触发写入磁盘的操作. 1:每次事务 ...

  4. Nginx-1.6.3源码安装、虚拟主机

    源码安装nginx cat /etc/redhat-release uname -rm yum install pcre-devel openssl-devel -y rpm -qa pcre pcr ...

  5. java并发编程实战笔记---(第三章)对象的共享

    3.1 可见性 synchronized 不仅实现了原子性操作或者确定了临界区,而且确保内存可见性. *****必须在同步中才能保证:当一个线程修改了对象状态之后,另一个线程可以看到发生的状态变化. ...

  6. hbase学习(二)hbase单机和高可用完全分布式安装部署

    hbase版本 2.0.4  与hadoop兼容表http://hbase.apache.org/book.html#hadoop  我的 hadoop版本是3.1   1.单机版hbase 1.1解 ...

  7. HTTP协议头注射漏洞实例

    HTTP 响应头文件中包含未经验证的数据会引发 cache-poisoning.cross-site scripting.cross-user defacement.page hijacking.co ...

  8. [BZOJ3672][Noi2014]购票 斜率优化+点分治+cdq分治

    3672: [Noi2014]购票 Time Limit: 30 Sec  Memory Limit: 512 MBSubmit: 1749  Solved: 885[Submit][Status][ ...

  9. Python2中input()、raw_input()和Python3中input()

    听了ALEX的某节课,说input()和raw_input()函数在Python2中没有区别,现在来探讨一下Python2中的input().raw_input()函数和Pyhont3中的input( ...

  10. shell-sed命令详解(转)

    (转自http://blog.csdn.net/wl_fln/article/details/7281986) Sed简介 sed是一种在线编辑器,它一次处理一行内容.处理时,把当前处理的行存储在临时 ...