pat 甲级 1049. Counting Ones (30)
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 (<=230).
Output Specification:
For each test case, print the number of 1's in one line.
Sample Input:
12
Sample Output:
5
思路:
对于一个数的abcde,先计算其中的某一位上出现1的个数,不妨考虑百位上的c,若c为0,则百位上出现1的个数和c的高位ab有关,具体个数是ab*digit,这里c是在百位上,那么digit为100;
若c=1,则百位上出现1的个数为(ab*digit)+de+1,其中de为abcde中c的低位.9=>c>=2时,百位上出现1的个数为(ab+1)*digit.
AC代码:
#define _CRT_SECURE_NO_DEPRECATE
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<set>
#include<queue>
#include<map>
using namespace std;
#define INF 0x3f3f3f
#define N_MAX 200+5
#define M_MAX 100000+20
typedef long long ll;
int n ,cnt = ;
int Count(int n) {
int cnt = ,digit=;
while (n / digit != ) {
int higher = n / (digit * );
int lower = n - (n / digit)*digit;//!!
int cur = n / digit % ;
switch (cur){
case :
cnt += higher*digit;
break;
case :
cnt += higher*digit + lower + ;
break;
default:
cnt += (higher + )*digit;
break;
}
digit *= ;
}
return cnt;
} int main() {
while (cin>>n) {
cnt = Count(n);
cout << cnt << endl;
}
return ;
}
pat 甲级 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
PAT甲级1049. Counting Ones 题意: 任务很简单:给定任何正整数N,你应该计算从1到N的整数的十进制形式的1的总数.例如,给定N为12,在1,10, 11和12. 思路: < ...
- PAT甲级1049 Counting Ones【规律】
题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805430595731456 题意: 给定n,问0~n中,1的总个数 ...
- PAT Advanced 1049 Counting Ones (30) [数学问题-简单数学问题]
题目 The task is simple: given any positive integer N, you are supposed to count the total number of 1 ...
- 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 分)(类似数位DP思想的模拟)
题意: 输入一个正整数N(N<=2^30),输出从1到N共有多少个数字包括1. AAAAAccepted code: #define HAVE_STRUCT_TIMESPEC #include& ...
- 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 ...
- 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 ...
随机推荐
- 使用 RuPengGame游戏引擎包 建立游戏窗体 如鹏游戏引擎包下载地址 Thread Runnable 卖票实例
package com.swift; import com.rupeng.game.GameCore;//导入游戏引擎包 //实现Runnable接口 public class Game_RuPeng ...
- JavaScript深拷贝与浅拷贝的理解
个人是这么理解深拷贝和浅拷贝的:就是假设B复制了A,当修改A时,看B是否会发生变化,如果B也跟着变了,说明这是浅拷贝,拿人手短,如果B没变,那就是深拷贝,自食其力. 一起看看我举的浅拷贝栗子: let ...
- CentOS7下Mysql5.7安装
下载并安装MySQL官方的 Yum Repository wget -i -c http://dev.mysql.com/get/mysql57-community-release-el7-10.no ...
- linux正则表达式扩展部分
扩展的正则表达式(Extended Regular Expressions): 使用的命令为:grep -E以及egrep [了解即可] 1)+ 表示重复“一个或一个以上”前面的字符(*是0或多 ...
- js数组删除(splice和delete)
最近一直在写js的数组,然后就发现了很奇怪的问题,后来才发现了规律. 删除数据的一行,一般有两种方法,一个是splice,一个是delete: splice:删除了数组后,数组的长度会自动变化.用法: ...
- 阻止touchslider事件冒泡,防止左右滑动时出发全局滑动事件
最近适用mui开发一个项目,全局有个侧滑菜单offCanvasSideScroll,首页用了一个touchslider插件来实现行的左右滚动 touchslider的当滚动方向与侧滑菜单滚动方向一致时 ...
- 如何禁止用户连续点击一个按钮事件详细JS
<input type="button" id="submit" value="提交"> <script> $(do ...
- url传参及重定向
成功跳转$this -> success('提示语',跳转路径,返回的数据,时间,发送的 Header 信息)跳转失败$this -> error('提示语',跳转路径,返回的数据,时间, ...
- hibernate的get() load() 和find()区别
如果找不到符合条件的纪录,get()方法将返回null.如果找不到符合条件的纪录,find()方法将返回null.如果找不到符合 条件的纪录,load()将会报出ObjectNotFoundEccep ...
- Python3中的列表用法,看这一篇就够了
类似C语言中的列表用法 ---------------------------------------------------------------------------------------- ...