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

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

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

  2. 【PAT甲级】1049 Counting Ones (30 分)(类似数位DP思想的模拟)

    题意: 输入一个正整数N(N<=2^30),输出从1到N共有多少个数字包括1. AAAAAccepted code: #define HAVE_STRUCT_TIMESPEC #include& ...

  3. PAT 解题报告 1049. Counting Ones (30)

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

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

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

  5. PAT 1004 Counting Leaves (30分)

    1004 Counting Leaves (30分) A family hierarchy is usually presented by a pedigree tree. Your job is t ...

  6. 1004 Counting Leaves (30分) DFS

    1004 Counting Leaves (30分)   A family hierarchy is usually presented by a pedigree tree. Your job is ...

  7. pat 1049. Counting Ones (30)

    看别人的题解懂了一些些    参考<编程之美>P132 页<1 的数目> #include<iostream> #include<stdio.h> us ...

  8. PAT (Advanced Level) 1049. Counting Ones (30)

    数位DP.dp[i][j]表示i位,最高位为j的情况下总共有多少1. #include<iostream> #include<cstring> #include<cmat ...

  9. 1049. Counting Ones (30)

    题目如下: The task is simple: given any positive integer N, you are supposed to count the total number o ...

随机推荐

  1. ES6语法:var、let、const的区别详解

    今天来说说es6的语法,最基础的也就是var,let,const 的用法与区别了,我们来看看他们之间的恩怨情仇. 首先来说说var,这个只要是学过js的都知道,它是用来声明一个变量的,但是它在开发中也 ...

  2. php+apache 环境配置(window环境)

    最近,小主从事PHP开发.特将最近如何搭建php7的过程记录在此!希望有需要,可以借鉴!( 电脑必须win7 sp1以上, .netframework4 ) Windows7安装php7,Win7+p ...

  3. vue中数据请求的三种方法

    注意请求可能存在跨域问题,需要去配置好 这三种建议使用axios 1.resource Vue 要实现异步加载需要使用到 vue-resource 库. Vue.js 2.0 版本推荐使用 axios ...

  4. Effective Go中文版(更新中)

    原文链接:https://golang.org/doc/effective_go.html Introduction Go是一种新兴的编程语言.虽然它借鉴了现有语言的思想,但它具有不同寻常的特性,使得 ...

  5. 简单配置Vue路由

    简单配置Vue路由 1.  创建一个单文件组件Test.vue <template> <div>Test</div> </template> <s ...

  6. 【原创】Java并发编程系列1:大纲

    [原创]Java并发编程系列1:大纲 一个人能力当中所蕴藏的潜能,远超过自己想象以外. 为什么要学习并发编程 随着现今互联网行业的迅猛发展,其业务复杂度.并发量也在不断增加,对程序的要求变得越来越高, ...

  7. Airport Simulation (数据结构与算法 – 队列 / Queue 的应用)

    Airport Simulation 是数据结构与算法教材中用于演示Queue的一个小程序(大多数教师似乎会跳过这个练习).主程序会通过输入总的运行时间.队列里可以等待的最多飞机数量,平均每个时间单元 ...

  8. Python3学习之路~10.1 多进程、进程间通信、进程池

    一 多进程multiprocessing multiprocessing is a package that supports spawning processes using an API simi ...

  9. C# 微信 生活助手 空气质量 天气预报等 效果展示 数据抓取 (二)

    此文主要是 中国天气网和中国环境监测总站的数据抓取 打算开放全部数据抓取源代码 已在服务器上 稳定运行半个月 webapi http://api.xuzhiheng.cn/ 常量 /// <su ...

  10. Mol Cell Proteomics. | Mapping Spatio-temporal Microproteomics Landscape in Experimental Model of Traumatic Brain Injury Unveils a link to Parkinson’s Disease

    期刊:Molecular & Cellular Proteomics 发表时间:June 16, 2019 通讯作者:Michel Salzet,Isabelle Fournier 一.  概 ...