📜  通过重复减去相邻数字将数字减少到单个数字

📅  最后修改于: 2022-05-13 01:57:52.574000             🧑  作者: Mango

通过重复减去相邻数字将数字减少到单个数字

给定一个数字N ,任务是通过重复减去相邻数字将其减少为一位数。即在第一次迭代中,将所有相邻的数字相减,生成一个新的数字,如果这个数字包含多于一个数字,则重复相同的过程,直到变成一个数字。

例子:

方法:为了简单起见,这里我们使用Array来表示初始数字 N。

  1. 计算N中的位数并将值存储在l中。
  2. 创建一个大小为l的数组a[]
  3. 将给定的数字复制到数组a[]中。
  4. 通过减去数组a的连续数字来计算RSF

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function to return the resultant digit
// after performing the given operations
int RSF(int n)
{
    while (n >= 10) {
 
        // Creating an extra copy of n
        int x = n;
        int l = 0;
 
        // Counting the number of digits in n
        while (n > 0) {
            n = n / 10;
            l++;
        }
 
        // Now n is 0
        // Creating an array of length l
        int a[l];
 
        // Initializing i with the last index of array
        int i = l - 1;
        while (x > 0) {
 
            // Filling array from right to left
            a[i] = x % 10;
            x = x / 10;
            i--;
        }
 
        // Calculating the absolute consecutive difference
        for (int j = 0; j < l - 1; j++) {
 
            // Updating the value of n in every loop
            n = n * 10 + abs(a[j] - a[j + 1]);
        }
    }
 
    // While loop ends here and we have found
    // the RSF value of N
    return n;
}
 
// Driver code
int main()
{
    int n = 614;
 
    // Passing n to RSF function and getting answer
    int ans = RSF(n);
 
    // Printing the value stored in ans
    cout << ans;
 
    return 0;
}


Java
// Java implementation of the approach
class GFG {
 
    // Function to return the resultant digit
    // after performing the given operations
    static int RSF(int n)
    {
        while (n >= 10) {
 
            // Creating an extra copy of n
            int x = n;
            int l = 0;
 
            // Counting the number of digits in n
            while (n > 0) {
                n = n / 10;
                l++;
            }
 
            // Now n is 0
            // Creating an array of length l
            int a[] = new int[l];
 
            // Initializing i with the last index of array
            int i = l - 1;
 
            while (x > 0) {
 
                // Filling array from right to left
                a[i] = x % 10;
                x = x / 10;
                i--;
            }
 
            // Calculating the absolute consecutive difference
            for (int j = 0; j < l - 1; j++) {
 
                // Updating the value of n in every loop
                n = n * 10 + Math.abs(a[j] - a[j + 1]);
            }
        }
 
        // While loop ends here and we have found
        // the RSF value of N
        return n;
    }
 
    // Driver code
    public static void main(String[] arg)
    {
        int n = 6972;
 
        // Passing n to RSF function and getting answer
        int ans = RSF(n);
 
        // Printing the value stored in ans
        System.out.println(ans);
    }
}


Python3
# Python3 implementation of the approach
 
# Function to return the resultant digit
# after performing the given operations
def RSF(n):
    while (n >= 10):
 
        # Creating an extra copy of n
        x = n;
        l = 0;
 
        # Counting the number of digits in n
        while (n > 0):
            n = n // 10;
            l += 1;
 
        # Now n is 0
        # Creating an array of length l
        a = [0] * l;
 
        # Initializing i with the last index of array
        i = l - 1;
        while (x > 0):
 
            # Filling array from right to left
            a[i] = x % 10;
            x = x // 10;
            i -= 1;
 
        # Calculating the absolute
        # consecutive difference
        for j in range(0, l - 1):
 
            # Updating the value of n in every loop
            n = n * 10 + abs(a[j] - a[j + 1]);
             
    # While loop ends here and we have found
    # the RSF value of N
    return n;
 
# Driver code
if __name__ == '__main__':
    n = 614;
 
    # Passing n to RSF function
    # and getting answer
    ans = RSF(n);
 
    # Printing the value stored in ans
    print(ans);
 
# This code is contributed by Rajput-Ji


C#
// C# implementation of the approach
using System;
class GFG
{
 
// Function to return the resultant digit
// after performing the given operations
static int RSF(int n)
{
    while (n >= 10)
    {
 
        // Creating an extra copy of n
        int x = n;
        int l = 0;
 
        // Counting the number of digits in n
        while (n > 0)
        {
            n = n / 10;
            l++;
        }
 
        // Now n is 0
        // Creating an array of length l
        int []a = new int[l];
 
        // Initializing i with the last index of array
        int i = l - 1;
 
        while (x > 0)
        {
 
            // Filling array from right to left
            a[i] = x % 10;
            x = x / 10;
            i--;
        }
 
        // Calculating the absolute
        // consecutive difference
        for (int j = 0; j < l - 1; j++)
        {
 
            // Updating the value of n in every loop
            n = n * 10 + Math.Abs(a[j] - a[j + 1]);
        }
    }
 
    // While loop ends here and we have found
    // the RSF value of N
    return n;
}
 
// Driver code
public static void Main(String[] arg)
{
    int n = 6972;
 
    // Passing n to RSF function and getting answer
    int ans = RSF(n);
 
    // Printing the value stored in ans
    Console.WriteLine(ans);
}
}
 
// This code is contributed by Princi Singh


Javascript


输出:
2

时间复杂度:O(n logn)

辅助空间:O(1)