ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

PAT.1049 Counting Ones(排列组合)

2020-05-29 21:52:15  阅读:246  来源: 互联网

标签:10 cnt now temp Ones ans Counting PAT.1049 left


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

思路:排列组合,考虑每一位为1时,其他位置的可能性,按照排列组合公式即可计算出。

假设对于303这个数字,令now为当前处理的这一位数,left为n刨去now及其他左边位数的值,
similarly right为右,再假设cnt为1e(now所在的位数,假设从数字最右边第零位开始)。
so
当now > 1时,左边可取0 ~ left的任意数字,右边 0 ~ cnt - 1的任意数字,因此
此时ans += (1 + left) * cnt。
当now == 1时,左边取0~left - 1任意数字时右边任取0~cnt - 1的数字,左边为left时,右边
仅能取 0~right的数字,因此 ans += left * cnt + right + 1。
当now == 0时,左边取0~left - 1任意数字,右边任取0~cnt - 1的人数,因此ans += left * cnt;
  


 1 #include <cstdio>
 2 using namespace std;
 3 
 4 int main() {
 5     int n;
 6     scanf("%d", &n);
 7     int temp = n;
 8     int cnt = 1;
 9     long long ans = 0;
10     int yu = 0;
11     while(temp) {
12         if(temp % 10 > 1) {
13             ans += (temp / 10 + 1) * cnt;
14         }
15         else if(temp % 10 == 1) {
16             ans += (temp / 10) * cnt + (yu + 1);
17         }
18         else {
19             ans += (temp / 10) * cnt; 
20         }
21         yu = (temp % 10) * cnt + yu;
22         cnt *= 10;
23         temp /= 10;
24     }
25     printf("%lld\n", ans);
26     return 0;
27 }

 



标签:10,cnt,now,temp,Ones,ans,Counting,PAT.1049,left
来源: https://www.cnblogs.com/bianjunting/p/12989740.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有