zoukankan      html  css  js  c++  java
  • 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 (<=230).

    Output Specification:

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

    Sample Input:
    12
    
    Sample Output:
    5
    
    
    
    特别经典的算法,借鉴学习了
    
    
    #include <iostream>
    #include <map>
    #include <queue>
    #include <stdio.h>
    #include <vector>
    #include <algorithm>
    #include <stack>
    #include <stdlib.h>
    #include <string.h>
    #include <math.h>
    #include <string>
    using namespace std;
    
    
    int c,base;
    int main()
    {	
    	int	n;
    	while(scanf("%d",&n)!=EOF){
    		c=0;base=1;     
    		while(n/base!=0){
    			int low=n%base;         //当前权值的低位
    			int high=(n/base)/10;   //当前权值的高位
    			int now=(n/base)%10;    //当前权值位
    			if(now==0)
    				c+=high*base;
    			else if(now==1)
    				c+=high*base+low+1;
    			else
    				c+=(high+1)*base;
    			base*=10;    //注意
    		}
    		printf("%d
    ",c);
    	}
        return 0;
    }

  • 相关阅读:
    提高代码质量:如何编写函数
    如何写自我评价
    写简历注意事项
    Android开发注意细节
    Android:onNewIntent()触发机制及注意事项
    Atom与markdown
    Java程序性能优化总结
    Java中的继承与组合
    Fragment生命周期总结
    C# 生成随机姓名
  • 原文地址:https://www.cnblogs.com/zh9927/p/4099042.html
Copyright © 2011-2022 走看看