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. Inp…
1049 Counting Ones (30)(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.…
看别人的题解懂了一些些    参考<编程之美>P132 页<1 的数目> #include<iostream> #include<stdio.h> using namespace std; int getone(int n) { int ans=0,base=1,right,left,now; while(n/base) { right=n%base; left=n/(base*10); now=(n/base)%10; if(now==0)ans+=lef…
要统计1到N之间‘1’的个数,如数11包含2个1.所以当N=12时,答案为5. 思想: 找规律,假设ans[N]表示1到N的‘1’的个数,则有a[100]=(a[10]-1)*9+10+a[10]-1+1; 先打表求出1ek的答案: 然后对N由高到低逐位拆分. 有种情况要特别注意: 当N=100001时,高位出现1时要累加到后面第一个非0位数上. #include<iostream> #include<cstring> #include<cstdio> #include…
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. I…
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.如12…
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…
1049. Counting Ones (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue 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 exam…
数位DP.dp[i][j]表示i位,最高位为j的情况下总共有多少1. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> #include<cstdio> #include<map> #include<queue> #include<string> #include<vector> using namesp…
题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805430595731456 题意: 给定n,问0~n中,1的总个数是多少. 思路: 问的是总个数,所以不需要考虑重复,只用考虑每一位上的贡献就行了. 将数字分成三部分,left(共i位),now和right(共j位) 如果当前now是0, 那么所有前i位是[0,left)的数字都+1个贡献,这些数一共有$left*10^j$个 如果当前now是[2,9],那么所有…