📌  相关文章
📜  计算具有相同的第一位和最后一位数字的数字

📅  最后修改于: 2021-04-27 18:15:11             🧑  作者: Mango

给定一个间隔,任务是对具有相同的第一位和最后一位数字的数字进行计数。例如, 1 23 1具有相同的第一位和最后一位数字。
例子:

Input  : start = 7,  end : 68
Output : 9
Number with same first and last digits are,
7 8 9 11 22 33 44 55 66.

Input  : start = 5,  end : 40
Output : 8

让我们首先考虑以下示例以了解该方法。

From 120 to 130, only 121 has same starting and ending digit
From 440 to 450, only 444 has same starting and ending digit
From 1050 to 1060, only 1051 has same starting and ending digit

从上面的示例中,我们可以看到,在每个十个数字范围中,只有一个具有给定属性的数字,除了1到10之外,该数字中有9个数字(所有一位数字)具有相同的起始和结束数字。
使用上面的属性,我们可以解决给定的问题,首先将给定的间隔分为两部分,即如果间隔是从l到r,则将其分为1到l和1到r,我们的答案是通过从中减去第一个查询的结果而获得的第二个查询。
现在,我们仍然要查找具有给定属性的范围为1到x的数字计数,为此,我们将x除以10,得到10个跨度的数字。我们在跨度上加9以考虑一位数字。如果x的最后一位小于x的第一位,那么在最终答案中应将1减小以丢弃最后十个跨度数字,因为该数字超出范围。

C++
// C++ program to get count of numbers with
// same start and end digit in an interval
#include 
using namespace std;
 
// Utility method to get first digit of x
int getFirstDigit(int x)
{
    while (x >= 10)
        x /= 10;
    return x;
}
 
// method to return count of numbers with same
// starting and ending digit from 1 upto x
int getCountWithSameStartAndEndFrom1(int x)
{
    if (x < 10)
        return x;
 
    // get ten-spans from 1 to x
    int tens = x / 10;
 
    // add 9 to consider all 1 digit numbers
    int res = tens + 9;
 
    // Find first and last digits
    int firstDigit = getFirstDigit(x);
    int lastDigit = x % 10;
 
    // If last digit is greater than first digit
    // then decrease count by 1
    if (lastDigit < firstDigit)
        res--;
 
    return res;
}
 
// Method to return count of numbers with same starting
// and ending digit between start and end
int getCountWithSameStartAndEnd(int start, int end)
{
    return getCountWithSameStartAndEndFrom1(end)
    - getCountWithSameStartAndEndFrom1(start - 1);
}
 
// Driver code to test above methods
int main()
{
    int start = 5, end = 40;
 
    cout << getCountWithSameStartAndEnd(start, end);
 
    return 0;
}


Java
// Java program to get count of numbers with
// same start and end digit in an interval
import java.util.*;
 
class Digits {
    // Utility method to get first digit of x
    public static int getFirstDigit(int x)
    {
        while (x >= 10)
            x /= 10;
        return x;
    }
 
    // method to return count of numbers with same
    // starting and ending digit from 1 upto x
    public static int getCountWithSameStartAndEndFrom1(int x)
    {
        if (x < 10)
            return x;
 
        // get ten-spans from 1 to x
        int tens = x / 10;
 
        // add 9 to consider all 1 digit numbers
        int res = tens + 9;
 
        // Find first and last digits
        int firstDigit = getFirstDigit(x);
        int lastDigit = x % 10;
 
        // If last digit is greater than first
        // digit then decrease count by 1
        if (lastDigit < firstDigit)
            res--;
 
        return res;
    }
 
    // Method to return count of numbers with same
    // starting and ending digit between start and end
    public static int getCountWithSameStartAndEnd(int start,
                                                  int end)
    {
        return getCountWithSameStartAndEndFrom1(end)
        - getCountWithSameStartAndEndFrom1(start - 1);
    }
 
    // driver code
    public static void main(String[] args)
    {
        int start = 5, end = 40;
        System.out.print(getCountWithSameStartAndEnd(start,
                                                     end));
    }
}
 
// This code is contributed by rishabh_jain


Python3
# Python3 program to get count of numbers with
# same start and end digit in an interval
 
# Utility method to get first digit of x
def getFirstDigit(x) :
    while (x >= 10) :
        x /= 10
    return x
 
# method to return count of numbers with same
# starting and ending digit from 1 upto x
def getCountWithSameStartAndEndFrom1(x) :
    if (x < 10):
        return x
 
    # get ten-spans from 1 to x
    tens = x / 10
 
    # add 9 to consider all 1 digit numbers
    res = tens + 9
 
    # Find first and last digits
    firstDigit = getFirstDigit(x)
    lastDigit = x % 10
 
    # If last digit is greater than first digit
    # then decrease count by 1
    if (lastDigit < firstDigit) :
        res = res - 1
 
    return res
 
# Method to return count of numbers with same starting
# and ending digit between start and end
def getCountWithSameStartAndEnd(start, end) :
    return (getCountWithSameStartAndEndFrom1(end) -
           getCountWithSameStartAndEndFrom1(start - 1))
 
# Driver Code
start = 5
end = 40
print(getCountWithSameStartAndEnd(start, end))
 
# This code is contributed by rishabh_jain


C#
// C# program to get count of numbers with
// same start and end digit in an interval
using System;
 
class GFG {
     
    // Utility method to get first digit
    // of x
    public static int getFirstDigit(int x)
    {
        while (x >= 10)
            x /= 10;
             
        return x;
    }
 
    // method to return count of numbers
    // with same starting and ending digit
    // from 1 upto x
    public static int getCountWithSameStartAndEndFrom1(
                                                  int x)
    {
         
        if (x < 10)
            return x;
 
        // get ten-spans from 1 to x
        int tens = x / 10;
 
        // add 9 to consider all 1 digit
        // numbers
        int res = tens + 9;
 
        // Find first and last digits
        int firstDigit = getFirstDigit(x);
        int lastDigit = x % 10;
 
        // If last digit is greater than
        // first digit then decrease count
        // by 1
        if (lastDigit < firstDigit)
            res--;
 
        return res;
    }
 
    // Method to return count of numbers
    // with same starting and ending digit
    // between start and end
    public static int getCountWithSameStartAndEnd(
                                 int start, int end)
    {
        return getCountWithSameStartAndEndFrom1(end)
        - getCountWithSameStartAndEndFrom1(start - 1);
    }
 
    // driver code
    public static void Main()
    {
        int start = 5, end = 40;
         
        Console.Write(getCountWithSameStartAndEnd(start,
                                                end));
    }
}
 
// This code is contributed by nitin mittal.


PHP
= 10)
        $x /= 10;
    return $x;
}
 
// method to return count
// of numbers with same
// starting and ending
// digit from 1 upto x
function getCountWithSameStartAndEndFrom1($x)
{
    if ($x < 10)
        return $x;
 
    // get ten-spans from 1 to x
    $tens = $x / 10;
 
    // add 9 to consider all
    // 1 digit numbers
    $res = $tens + 9;
 
    // Find first and last digits
    $firstDigit = getFirstDigit($x);
    $lastDigit = $x % 10;
 
    // If last digit is greater
    // than first digit
    // then decrease count by 1
    if ($lastDigit < $firstDigit)
        $res--;
 
    return $res;
}
 
// Method to return count of
// numbers with same starting
// and ending digit between
// start and end
function getCountWithSameStartAndEnd($start, $end)
{
    return getCountWithSameStartAndEndFrom1($end)
          - getCountWithSameStartAndEndFrom1($start - 1);
}
 
    // Driver Code
    $start = 5;
    $end = 40;
    echo getCountWithSameStartAndEnd($start, $end);
 
// This code is contributed by nitin mittal.
?>


Javascript


输出:

8