📜  N的前X位和后X位之间的绝对差

📅  最后修改于: 2021-04-29 03:06:39             🧑  作者: Mango

给定两个整数NX。任务是打印的N第一个X和最后一个X位之差的绝对值。考虑到位数至少为2 * x。

例子:

Input: N = 21546, X = 2
Output: 25
The first two digit in 21546 is 21.
The last two digit in 21546 is 46.
The absolute difference of 21 and 46 is 25.

Input: N = 351684617, X = 3
Output: 266

简单方法:

  • 最后存储数字的最后x位数字。
  • 在数字中找到数字。
  • 删除除第一个x之外的所有数字。
  • 首先存储数字的前x个整数。

下面是上述方法的实现:

C++
// C++ implementation of the above approach
#include 
using namespace std;
  
// Function to find the
// number of digits in the integer
long long digitsCount(long long n)
{
    int len = 0;
    while (n > 0) {
        len++;
        n /= 10;
    }
    return len;
}
  
// Function to find the absolute difference
long long absoluteFirstLast(long long n, int x)
{
    // Store the last x digits in last
    int i = 0, mod = 1;
    while (i < x) {
        mod *= 10;
        i++;
    }
    int last = n % mod;
  
    // Count the no. of digits in N
    long long len = digitsCount(n);
  
    // Remove the digits except the first x
    while (len != x) {
        n /= 10;
        len--;
    }
  
    // Store the first x digits in first
    int first = n;
  
    // Return the absolute difference between
    // the first and last
    return abs(first - last);
}
  
// Driver code
int main()
{
    long long n = 21546, x = 2;
    cout << absoluteFirstLast(n, x);
  
    return 0;
}


Java
// Java implementation of the above approach
import java.util.*;
  
class GFG
{
  
// Function to find the
// number of digits in the integer
static int digitsCount(int n)
{
    int len = 0;
    while (n > 0) 
    {
        len++;
        n /= 10;
    }
    return len;
}
  
// Function to find the absolute difference
static int absoluteFirstLast(int n, int x)
{
    // Store the last x digits in last
    int i = 0, mod = 1;
    while (i < x) 
    {
        mod *= 10;
        i++;
    }
    int last = n % mod;
  
    // Count the no. of digits in N
    int len = digitsCount(n);
  
    // Remove the digits except the first x
    while (len != x)
    {
        n /= 10;
        len--;
    }
  
    // Store the first x digits in first
    int first = n;
  
    // Return the absolute difference between
    // the first and last
    return Math.abs(first - last);
}
  
// Driver code
public static void main(String args[])
{
    int n = 21546, x = 2;
    System.out.println(absoluteFirstLast(n, x));
}
}
  
// This code is contributed by
// Surendra_Gangwar


Python3
# Python3 implementation of the above approach 
  
# Function to find the 
# number of digits in the integer 
def digitsCount(n) :
    length = 0; 
    while (n > 0) : 
        length += 1; 
        n //= 10; 
          
    return length; 
  
  
# Function to find the absolute difference 
def absoluteFirstLast(n, x) :
      
    # Store the last x digits in last 
    i = 0 ;
    mod = 1;
    while (i < x) :
        mod *= 10;
        i += 1; 
      
    last = n % mod;
      
    # Count the no. of digits in N
    length = digitsCount(n); 
      
    # Remove the digits except the first x
    while (length != x) :
        n //= 10;
        length -= 1; 
      
    # Store the first x digits in first 
    first = n; 
      
    # Return the absolute difference between
    # the first and last 
    return abs(first - last); 
  
# Driver code 
if __name__ == "__main__" : 
      
    n = 21546 ;
    x = 2; 
    print(absoluteFirstLast(n, x)); 
      
# This code is contributed by Ryuga


C#
// C# implementation of the approach 
using System;
  
class GFG 
{ 
  
// Function to find the 
// number of digits in the integer 
static int digitsCount(int n) 
{ 
    int len = 0; 
    while (n > 0) 
    { 
        len++; 
        n /= 10; 
    } 
    return len; 
} 
  
// Function to find the absolute difference 
static int absoluteFirstLast(int n, int x) 
{ 
    // Store the last x digits in last 
    int i = 0, mod = 1; 
    while (i < x) 
    { 
        mod *= 10; 
        i++; 
    } 
    int last = n % mod; 
  
    // Count the no. of digits in N 
    int len = digitsCount(n); 
  
    // Remove the digits except the first x 
    while (len != x) 
    { 
        n /= 10; 
        len--; 
    } 
  
    // Store the first x digits in first 
    int first = n; 
  
    // Return the absolute difference between 
    // the first and last 
    return Math.Abs(first - last); 
} 
  
// Driver code 
public static void Main(String []args) 
{ 
    int n = 21546, x = 2; 
    Console.Write(absoluteFirstLast(n, x)); 
} 
} 
  
// This code has been contributed by 29AjayKumar


PHP
 0) 
    {
        $len++;
        $n = (int)($n / 10);
    }
    return $len;
}
  
// Function to find the absolute difference
function absoluteFirstLast($n, $x)
{
    // Store the last x digits in last
    $i = 0;
    $mod = 1;
    while ($i < $x)
    {
        $mod *= 10;
        $i++;
    }
    $last = $n % $mod;
  
    // Count the no. of digits in N
    $len = digitsCount($n);
  
    // Remove the digits except the first x
    while ($len != $x) 
    {
        $n = (int)($n / 10);
        $len--;
    }
  
    // Store the first x digits in first
    $first = $n;
  
    // Return the absolute difference 
    // between the first and last
    return abs($first - $last);
}
  
// Driver code
$n = 21546;
$x = 2;
echo absoluteFirstLast($n, $x);
  
// This code is contributed by mits
?>


输出:
25