首页 > 代码库 > codeforces 204(Div.1 A) Little Elephant and Interval(贪心)

codeforces 204(Div.1 A) Little Elephant and Interval(贪心)

题意:
有一种个位数与最高位数字相等的数字,求在l,r的范围内,这样的数字的个数。


思路:
找下规律就知道当当n>10的时候除去个位以后的答案等于n/10,然后考虑第一个数字是否小于最后一个。小于减一,还要加上个位一定存在的9位数


import java.util.Scanner;

public class xxz {


    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long   l = sc.nextLong();
        long   r = sc.nextLong();
        long ans = solve(r) - solve(l-1);
        System.out.println(ans);

    }

    public static long solve(long x){
        String _x = String.valueOf(x);
        return  x/10 -1 + Math.min(9,x)+(_x.charAt(0) <= _x.charAt(_x.length() - 1) ?

1 : 0); } }

<script type="text/javascript"> $(function () { $(‘pre.prettyprint code‘).each(function () { var lines = $(this).text().split(‘\n‘).length; var $numbering = $(‘
    ‘).addClass(‘pre-numbering‘).hide(); $(this).addClass(‘has-numbering‘).parent().append($numbering); for (i = 1; i <= lines; i++) { $numbering.append($(‘
  • ‘).text(i)); }; $numbering.fadeIn(1700); }); }); </script>

codeforces 204(Div.1 A) Little Elephant and Interval(贪心)