PAT-A 1049. Counting Ones
来源:互联网 发布:java核心编程 pdf下载 编辑:程序博客网 时间:2024/05/29 19:41
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
思路参考:
http://blog.csdn.net/tiantangrenjian/article/details/19908885
程序代码:
#include<stdio.h>#include<math.h>#include<string.h>char c[11]={0};int count_1(int n);int main(){ int n,i=0; scanf("%d",&n); int sum = count(n); printf("%d",sum); return 0;}int count(int n){ int factor = 1; int sum = 0,lower,high,cur; while(n/factor!=0) { lower = n%(factor); high = n/(factor*10); cur = (n/factor)%10; switch(cur) { case 0: sum += high *factor; break; case 1: sum += high *factor + lower+1; break; default: sum += (high+1)*factor; break; } factor=factor*10; } return sum;}
0 0
- PAT-A 1049. Counting Ones
- PAT A 1049. Counting Ones (30)
- PAT 1049. Counting Ones
- PAT 1049. Counting Ones
- PAT--1049. Counting Ones
- 1049. Counting Ones (30)-PAT
- pat 1049. Counting Ones (30)
- PAT 1049. Counting Ones (30)
- PAT 1049. Counting Ones (30)
- PAT 1049. Counting Ones (30)
- 【PAT】1049. Counting Ones (30)
- PAT.1049. Counting Ones (30)
- PAT 1049. Counting Ones (30)
- 【PAT (Advanced Level)】1049. Counting Ones (30)
- 【PAT甲级】1049. Counting Ones (30)
- 1049. Counting Ones (30) PAT甲级
- PAT甲级.1049. Counting Ones (30)
- PAT甲级练习1049. Counting Ones (30)
- 基于Hadoop生态圈的数据仓库实践 —— 进阶技术(九)
- 算法讲解:ac自动机及简单衍生
- NodeJs路由设计
- 有关海量数据的处理简单总结
- C/C++struct的深层区别
- PAT-A 1049. Counting Ones
- 自定义我的拦截器
- Two Sum
- Muli3D源码分析(1) - 框架概览
- hdoj1213How Many Tables
- LeetCode Length of Last Word(最后一个单词的长度)
- BZOJ 1008 [HNOI2008]越狱
- PyGobject(七十三)Gtk.Widget之Gtk.SpinButton
- android Canvas的两种使用情形