zoukankan      html  css  js  c++  java
  • nyoj--514--1的个数(贪心)

    

    1的个数

    时间限制:1000 ms  |  内存限制:65535 KB
    难度:3
    描述
    给你两个数a和b,你的任务是计算出1在a和b之间出现的次数,比如说,如果a=1024,b=1032,那么a和b之间的数就是:
    1024 1025 1026 1027 1028 1029 1030 1031 1032
    则有10个1出现在这些数中。
    输入
    输入不会超过500行。每一行有两个数a和b,a和b的范围是0 <= a, b <= 100000000。输入两个0时程序结束,两个0不作为输入样例。
    输出
    对于每一对输入的a和b,输出一个数,代表1出现的个数。
    样例输入
    1 10
    44 497
    346 542
    0 0
    样例输出
    2
    185
    40
    #include<iostream> 
    #include<string.h>
    #include<string.h> 
    #include<algorithm> 
    using namespace std;  
    int low,high;  
    int Pow[10]={1,10,100,1000,10000,100000,1000000,10000000,100000000,
    			1000000000};  
    int Count(int n)  
    {   
    	int digit=0,temp=n,sum=0;  
        while(temp)  
        {   
    		if(temp%10>1) 
    		sum+=Pow[digit];  
            else if(temp%10==1) 
    		sum+=n%Pow[digit]+1; 
    		
            sum+=temp%10*digit*Pow[digit-1];  
            temp/=10,digit++;  
        }  
        return sum;
    }   
    int main()  
    {   while(cin>>low>>high,low+high)  
        {   
    		if(low>high) swap(low,high);
            cout<<Count(high)-Count(low-1)<<endl;  
        }
        return 0;
    }

  • 相关阅读:
    网络请求Request
    HTML元素
    你所不知的 CSS ::before 和 ::after 伪元素用法
    DOM理解
    为什么你应该尝试全栈
    程序员菜鸟的常用网站
    前端零基础学习提纲
    JavaScript,调用函数的5种方法
    json数据转化及应用
    浅谈ajax中的GET和POST
  • 原文地址:https://www.cnblogs.com/playboy307/p/5273562.html
Copyright © 2011-2022 走看看