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. 关于select联动的两种做法

    第一种方法: function dong(){      var getSheng = document.getElementById("sheng");      var get ...

  2. 未找到与约束 ContractName Microsoft.VisualStudio.Utilitues.IContentTypeRegistryService......

    1.问题提出 用VS 2013 with Update5 开发项目,点击项目中的文件,发现打不开,抛出如下的错误. 错误提示: 未找到与约束 ContractName Microsoft.Visual ...

  3. AWS 使用总结

    A.升配置的流程: 1.新开一台配置较高的机器; 2.将新机器和老机器的磁盘都取消关联,注意需要记录下老机器的磁盘分区设备名,如:/dev/sda1: 3.将老机器的磁盘挂载到新机器上,磁盘分区设备名 ...

  4. 移动端touch滑屏事件

    <script> var windowHeight = $(window).height(), $body = $("body");// console.log($(w ...

  5. Dagger:快速的依赖注入for 安卓&Java

    Dagger:快速的依赖注入for 安卓&Java 2014年5月8日 星期四 15:29 官网: http://square.github.io/dagger/ GitHub: https: ...

  6. wxPython 画图板

    终于开始Python学习之旅了,姑且以一个“画图板”小项目开始吧.放慢脚步,一点一点地学习. 1月28日更新 第一次遇到的麻烦便是“重绘”,查了好多资料,终于重绘成功了. #-*- encoding: ...

  7. 洛谷 P1469 找筷子 题解

    题目传送门 先排序一遍,再一个一个判断是否有偶数个.注意for循环要i+=2. #include<bits/stdc++.h> using namespace std; ]; int ma ...

  8. SGU 217. Two Cylinders

    题意:给空间内两根圆柱,求轴线垂直相交时公共部分的体积. 暴力积分即可. ID: Date'n'Time: Name: Task: .Ext: Status: Time: Memory: 158937 ...

  9. SGU 205. Quantization Problem

    205. Quantization Problem time limit per test: 0.25 sec. memory limit per test: 65536 KB input: stan ...

  10. Git & GitHub 学习

    学习资料: Git版本控制软件结合GitHub从入门到精通常用命令学习手册:http://www.ihref.com/read-16369.html 官方中文手册:http://git-scm.com ...