PAT 1049 Counting Ones [难]

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 (≤2?30??).

Output Specification:

For each test case, print the number of 1‘s in one line.

Sample Input:

12

Sample Output:

5

题目大意:给出一个数m,计算1~m中的数包含的所有的1的个数。

//我绝对是看过这道题,但是真的想不起来怎么做了,当时就没怎么看懂。数据量这么大,指定不能一个一个算,需要技巧。

//个位是1+十位是1+。。。。。这样会不会有重复?不会吧。

//这个真的好难,看了好久才明白。

如何计算left和now和right也值得注意!自己求的话就不太会求的。

原文地址:https://www.cnblogs.com/BlueBlueSea/p/9862926.html

时间: 2024-10-03 10:30:30

PAT 1049 Counting Ones [难]的相关文章

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. Inpu

PAT 1049 Counting Ones

#include <cstdio> #include <cstdlib> using namespace std; #define COL 10 #define ROW 10 int tbl[ROW][COL]; void print(int* tbl) { for (int i=0; i<ROW; i++) { for (int j=0; j<COL; j++) { printf("%11d", tbl[i * COL + j]); } print

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. Inp

PAT 1004. Counting Leaves (30)

A family hierarchy is usually presented by a pedigree tree.  Your job is to count those family members who have no child. Input Each input file contains one test case. Each case starts with a line containing 0 < N < 100, the number of nodes in a tre

PAT甲题题解-1049. Counting Ones-数学问题

n位数,总共有0~10^n-1共计10^n个数那么所有数出现的总次数变为n*(10^n)个数1出现的次数便是十分之一,所以n位数中,1出现的次数为n*10^(n-1)知道这一个后,接下来就方便求了. 举个例子就方便理解了 3125 从头到尾for一遍 3:那么便有三组1000以内的:0~999,1000~1999,2000~29991000以内的1的个数为300,所以共有3*300=900但是又因为1000~1999中千位上的1也要算进去,有1000个所以0~2999中总共有900+1000=1

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

题意: 输入一个正整数N(N<=2^30),输出从1到N共有多少个数字包括1. 代码: #define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using namespace std;int main(){ ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n; cin>>n; int ans=0; int l=0,r=0,low_bit=1,yushu

PAT (Advanced Level) 1049 Counting Ones

题解 now - 当前位的数值 left - 在now左边的所有数字 right - 在now右边的所有数字 mul - right的数量级,如 10,100,100 ① 如果 now == 0 ,当now可以为1时,左边的数值必须为(0 ~ (left-1)),右边的数值可以是 0 ~ 999… ,所以情况有 left*mul ② 如果 now == 1 ,在第①种情况下,再加上right+1,因为当前位已经为1,所以右边的数right在当前范围下是什么无所谓,所以情况为 left*mul+r

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'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

PAT 1103 Integer Factorization[难]

1103 Integer Factorization(30 分) The K?P factorization of a positive integer N is to write N as the sum of the P-th power of K positive integers. You are supposed to write a program to find the K?P factorization of N for any positive integers N, K an