1049 Counting Ones (30分)
The task is simple: given any positive integer N, you are supposed to count the total number of 1's in the decimal form of the integers from 1 to N. For example, given N being 12, there are five 1's in 1, 10, 11, and 12.
Input Specification:
Each input file contains one test case which gives the positive N (≤).
Output Specification:
For each test case, print the number of 1's in one line.
Sample Input:
12
Sample Output:
5题目分析:一道数学题 题目据说是《编程之美》上的一道题
具体的推法好像很麻烦的样子 之后有时间会看看
#define _CRT_SECURE_NO_WARNINGS
#include <climits>
#include<iostream>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<stack>
#include<algorithm>
#include<string>
#include<cmath>
using namespace std; int main()
{
int N;
cin >> N;
int count = ,left=,right=,now=,a=;
while (N/a)
{
left = N / (a * );
now = N / a % ;
right = N % a;
if (now == )count += left * a;
else if (now ==)count += left * a + right + ;
else if (now > )count += (left + ) * a;
a *= ;
}
cout << count;
}
1049 Counting Ones (30分)的更多相关文章
- 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 分)(类似数位DP思想的模拟)
题意: 输入一个正整数N(N<=2^30),输出从1到N共有多少个数字包括1. AAAAAccepted code: #define HAVE_STRUCT_TIMESPEC #include& ...
- PAT 解题报告 1049. Counting Ones (30)
1049. Counting Ones (30) The task is simple: given any positive integer N, you are supposed to count ...
- pat 甲级 1049. Counting Ones (30)
1049. Counting Ones (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The tas ...
- PAT 1004 Counting Leaves (30分)
1004 Counting Leaves (30分) A family hierarchy is usually presented by a pedigree tree. Your job is t ...
- 1004 Counting Leaves (30分) DFS
1004 Counting Leaves (30分) A family hierarchy is usually presented by a pedigree tree. Your job is ...
- pat 1049. Counting Ones (30)
看别人的题解懂了一些些 参考<编程之美>P132 页<1 的数目> #include<iostream> #include<stdio.h> us ...
- PAT (Advanced Level) 1049. Counting Ones (30)
数位DP.dp[i][j]表示i位,最高位为j的情况下总共有多少1. #include<iostream> #include<cstring> #include<cmat ...
- 1049. Counting Ones (30)
题目如下: The task is simple: given any positive integer N, you are supposed to count the total number o ...
随机推荐
- Simulink仿真入门到精通(二) Simulink模块
2.1 Simulink模块的组成要素 用户构建系统模型时无需直接面对成千上万行的代码,而是通过模块化图形界面以模块化的方式构建,能够使理解变得容易,让大脑减负.通过层次化模块分布将系统功能模块化,而 ...
- php里面的一些面试经典的函数
<?php /* 这是一个多线程的读取解决的函数 @param1 $fle 传入要读取的文件名 */ function filelock($fle){ $fp=fopen($fls,'w+'); ...
- Cinemachine中噪音的应用
两种默认产生噪音的方式 Nosie阶段的Component Component在流水线中主要通过MuteCameraState来处理对State的计算. 对于Noise类型的Component ...
- XMind ZEN 2020 (10.0.2) 全平台 完美破解版(ubuntu)
XMind ZEN 2020 (10.0.2) 全平台 完美破解版(ubuntu) https://www.ghpym.com/xmindzen.html/comment-page-8?replyto ...
- 编译Zookeeper3.4.6源代码并通过Intellij IDEA运行(2020年)
一.问题背景 生产环境Windows机器上用的Zookeeper 3.4.6,最近经常报如下异常: 经过搜索,看到一篇帖子可以解决:https://www.jianshu.com/p/73eec030 ...
- 【JAVA进阶架构师指南】之三:深入了解类加载机制
前言 在上一篇文章中,我们知道了JVM的内存划分,其中在说到方法区的时候说到方法区中存放的信息包括[已被JVM加载的类信息,常量,静态变量,即时编译的代码等],整个方法区其实就和类加载有关. 类加 ...
- 【视频+图文】Java经典基础练习题(三):输入3个整数,并将其由小到大输出
目录 一.视频讲解 二.思路分析 总结: 三.代码+详解+结果 四.彩蛋 能解决题目的代码并不是一次就可以写好的 我们需要根据我们的思路写出后通过debug模式找到不足再进行更改 多次测试后才可得到能 ...
- Inception V1-V4
2019-05-29 20:56:02 一.Inception V1 当不知道在卷积神经网络中该使用1 * 1卷积还是3 * 3的卷积还是5 * 5的卷积或者是否需要进行pooling操作的时候,我们 ...
- 数据源管理 | 主从库动态路由,AOP模式读写分离
本文源码:GitHub·点这里 || GitEE·点这里 一.多数据源应用 1.基础描述 在相对复杂的应用服务中,配置多个数据源是常见现象,例如常见的:配置主从数据库用来写数据,再配置一个从库读数据, ...
- Ubuntu 18.04 将gcc版本降级为5.5版本
Remark: Polynomial algebra 程序由于版本问题只能在gcc 5.0 版本运行, 而ubuntu更新会将gcc 更新到7.0版本,出现冲突(报错:如下) collect2: er ...