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. Sqlmap与burpsuite动态更新某些参数

    有如下注入点: http://localhost/id=1&order_nu=1 情况说明: id为注入点,  每一次注入时, order_nu不能跟上次的一样(假说这个order_nu为一个 ...

  2. perl6正则 3: 行开头与结尾与多行开头,多行结尾

    ^ $ 匹配一行的开头或结尾, 可以用 ^ 或 $. > so 'abcde' ~~ /e$/ True > so 'abcdef' ~~ /e$/ False > so 'abcd ...

  3. 南邮综合题writeup

    http://teamxlc.sinaapp.com/web3/b0b0ad119f425408fc3d45253137d33d/index.php fuckjs直接console得到地址 http: ...

  4. angular导出文件保存在本地

    $scope.ev_click = function(obj){ var ev = document.createEvent("MouseEvents"); ev.initMous ...

  5. 不老的神器:安全扫描器Nmap渗透使用指南【转】

    介绍 nmap是用来探测计算机网络上的主机和服务的一种安全扫描器.为了绘制网络拓扑图Nmap的发送特制的数据包到目标主机然后对返回数据包进行分析.Nmap是一款枚举和测试网络的强大工具. 特点 主机探 ...

  6. 【会装】kylin的安装(填坑)和简单使用

     1.简介 kylin的设计思想是空间换时间,将hive上的大表的维度全部排列组合计算也将度量提前计算然后存入HBase库,这个步骤在kylin中称之为build cube. 在查询的时候已经建立cu ...

  7. JavaScript 中typeof、instanceof 与 constructor 的区别?

    typeof.instanceof 与 constructor 详解 typeof  一元运算符 返回一个表达式的数据类型的字符串,返回结果为js基本的数据类型,包括number,boolean,st ...

  8. Java门派的风险

    Java门派的风险 正在看周思博(www.joelonsoftware.com)的新文章.这次是疯狂攻击Java.主要论点是:Java不够难,作为工业语言不错,但作为学校的教学语言,就忒差了.学校应该 ...

  9. 洛谷P1094纪念品分组 题解

    题目传送门 首先的思路就是贪心.先将所有的纪念品按照价格从低到高进行排序.在分别从左到右.从右到左合并纪念品.如果两端纪念品价格超过了上上限,那么就将较大的那一个纪念品独自放入.否则将两个纪念品一起放 ...

  10. you have to first modify the default Eclipse configuration to avoid XML cosmetic errors:

    Configure XML Validation to Avoid Cosmetic Errors Navigate to: Window->Preferences->XML->XM ...