Pat(Advanced Level)Practice--1049(Counting Ones)
来源:互联网 发布:python中的单例模式 编辑:程序博客网 时间:2024/06/08 09:43
Pat1049代码
题目描述:
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:12Sample Output:
5
#include<cstdio>using namespace std;int CountOneNum(int n){int ret=0;int factor=1;int low=0;int cur=0;int high=0;while(n/factor!=0){low=n-(n/factor)*factor;cur=(n/factor)%10;high=n/(factor*10);switch (cur){case 0:ret+=high*factor;break;case 1:ret+=high*factor+low+1;break;default:ret+=(high+1)*factor;break;}factor*=10;}return ret;}int main(int argc,char *argv[]){int n;scanf("%d",&n);printf("%d\n",CountOneNum(n));return 0;}
更详细的分析,请看博客链接:http://blog.csdn.net/cstopcoder/article/details/22760793
0 0
- Pat(Advanced Level)Practice--1049(Counting Ones)
- PAT (Advanced Level) Practise 1049 Counting Ones (30)
- PAT (Advanced Level) Practise 1049 Counting Ones (30)
- 【PAT (Advanced Level)】1049. Counting Ones (30)
- 【PAT】【Advanced Level】1049. Counting Ones (30)
- Pat(Advanced Level)Practice--1004(Counting Leaves)
- PAT (Advanced Level) Practice 1006
- pat 1049 Counting Ones
- PAT 1049 Counting Ones
- pat 1049 Counting Ones
- pat 1049 Counting ones
- PAT (Advanced Level) 1049. Counting Ones (30) 1到N中1出现的次数
- 1049. Counting Ones (30)【计算1 的个数】——PAT (Advanced Level) Practise
- Pat(Advanced Level)Practice--1025(PAT Ranking)
- Pat(Advanced Level)Practice--1075(PAT Judge)
- Pat(Advanced Level)Practice--1003(Emergency)
- Pat(Advanced Level)Practice--1008(Elevator)
- Pat(Advanced Level)Practice--1010(Radix)
- C++ cin cout
- SQL语法的重要知识点总结
- DIY激光雕刻机之位图雕刻
- uva 1526 - Edge Detection(二分+排序)
- struts2返回json数据
- Pat(Advanced Level)Practice--1049(Counting Ones)
- hadoop异常java heap space
- winform程序中那些你不注意会被坑的奇葩们。。
- 【CSDN常见问题解答】Javap 的使用
- JAVA大数类练手
- HDU 4598 Difference
- 在rubymine中启动应用。产生 Library not loaded: libmysqlclient.18.dylib (LoadError) 错误的解决办法
- qt 下scrollArea 显示图像的问题
- sax解析原理与案例