📜  找到使用连续数字绝对差数组的第一位数字形成的手机号码

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

找到使用连续数字绝对差数组的第一位数字形成的手机号码

给定一个字符串ph[] ,任务是根据以下条件为用户找到一个新号码:

  1. 新号码也将从与原号码相同的数字开始。
  2. 新数字的数字将是一系列连续元素绝对差数组的第一个数字。

例子:

方法:考虑以下步骤来解决此问题:

  1. 将字符串中的每个字符转换为整数,并使用列表推导将其存储到数组ph1[]中。
  2. 声明一个空字符串ph2
  3. 将数组ph1[ ]的第一个元素转换为字符串并将其添加到ph2
  4. 使用列表推导通过存储连续元素的绝对差来创建一个数组。
  5. 将此数组分配给ph1
  6. 重复步骤 3-5,十次,因为电话号码有十位数。

下面是上述方法的实现。

Python3
# Function to find lucky phone number
def phone(ph, n):
 
    # Converting char to int and storing into array.
    ph1 = [int(i) for i in ph]
 
    # Empty string to store lucky number.
    ph2 = ""
 
    # Loop for performing action
    # and adding digit to ph2.
    for _ in range(n):
 
        # Convert first element into
        # string and adding to ph2.
        ph2 += str(ph1[0])
 
        # Creating new ph1 by subtracting
        # consecutive element.
        ph1 = [abs(ph1[j]-ph1[j + 1]) \
              for j in range(len(ph1)-1)]
 
    # Return lucky number ph2
    return ph2
 
 
# Original number
ph = "9827218706"
 
# Calling phone function.
num = phone(ph, len(ph))
 
# Print the lucky number
print(num)


Javascript


C++
// C++ code for the above approach
#include 
using namespace std;
 
// Function to find lucky number.
string phone(string ph, int n)
{
 
    // ph2 is empty string to store lucky number.
    string ph2 = "";
 
    // For loop for finding lucky number
    for (int i = 0; i < ph.length(); i++) {
 
        // Add first element of ph to ph2
        ph2 += ph[0];
 
        // S for storing the difference
        string S = "";
 
        // Loop to calculate the absolute difference
        for (int j = 0; j < ph.length(); j++) {
            int x = abs(int(ph[j]) - int(ph[j + 1]));
            S += x + '0';
        }
 
        // Assigning S to ph.
        ph = S;
    }
 
    // Return the lucky number
    return ph2;
}
 
// Driver Code
int main()
{
 
    // Original number
    string ph = "9827218706";
 
    // Call phone function
    string num = phone(ph, ph.length());
 
    // Printing lucky number
    cout << (num);
}
 
// This code is contributed by Potta Lokesh


Java
// Java program for the above approach
import java.util.*;
 class GFG {
 
  // Function to find lucky number.
  static String phone(String ph, int n)
  {
 
    // ph2 is empty string to store lucky number.
    String ph2 = "";
 
    // For loop for finding lucky number
    for (int i = 0; i < ph.length(); i++)
    {
 
      // Add first element of ph to ph2
      ph2 += ph.charAt(0);
 
      // S for storing the difference
      String S = "";
 
      // Loop to calculate the absolute difference
      for (int j = 0; j < ph.length()-1; j++)
      {
        int x = Math.abs(ph.charAt(j) - ph.charAt(j+1));
        S += (char)(x + '0');
      }
 
      // Assigning S to ph.
      ph = S;
    }
 
    // Return the lucky number
    return ph2;
     
  }
 
  // Driver Code
  public static void main(String args[])
  {
    // Original number
    String ph = "9827218706";
 
    // Call phone function
    String num = phone(ph, ph.length());
 
    // Printing lucky number
    System.out.println(num);
  }
}
 
// This code is contributed by avijitmondal1998


Python3
# Function to find lucky number.
def phone(ph, n):
 
    # ph2 is empty string to store lucky number.
    ph2 = ""
 
    # For loop for finding lucky number
    for i in range(len(ph)):
 
        # Add first element of ph to ph2
        ph2 += ph[0]
 
        # S for storing the difference
        S = ""
 
        # Loop to calculate the absolute difference
        for j in range(len(ph)-1):
            x = abs(int(ph[j])-int(ph[j + 1]))
            S += str(x)
 
        # Assigning S to ph.
        ph = S
 
    # Return the lucky number
    return ph2
 
 
# Original number
ph = "9827218706"
 
# Call phone function
num = phone(ph, len(ph))
 
# Printing lucky number
print(num)


C#
// C# code for the above approach
using System;
class GFG
{
 
  // Function to find lucky number.
  static string phone(string ph, int n)
  {
 
    // ph2 is empty string to store lucky number.
    string ph2 = "";
 
    // For loop for finding lucky number
    for (int i = 0; i < ph.Length; i++)
    {
 
      // Add first element of ph to ph2
      ph2 += ph[0];
 
      // S for storing the difference
      string S = "";
 
      // Loop to calculate the absolute difference
      for (int j = 0; j < ph.Length; j++)
      {
        int x = Math.Abs(ph[j] - ph[j + 1]);
        S += x + '0';
      }
 
      // Assigning S to ph.
      ph = S;
    }
 
    // Return the lucky number
    return ph2;
  }
 
  // Driver Code
  public static void Main()
  {
 
    // Original number
    string ph = "9827218706";
 
    // Call phone function
    string num = phone(ph, ph.Length);
 
    // Printing lucky number
    Console.WriteLine (num);
  }
}
 
// This code is contributed by ukasp.


Javascript


输出
9154301011

时间复杂度: O(N*N)
辅助空间: O(N)

高效方法:在这种方法中,不需要额外的空间来存储数组中的元素。首先,声明一个空字符串ph2 ,其中将存储幸运数字,现在创建一个 for 循环,其中将字符串的第一个字符添加到ph2并再次创建另一个 for 循环以查找连续元素的绝对差。现在绝对差的字符串将被分配给原始数字ph1 ,并且将遵循相同的步骤。请按照以下步骤解决问题:

  • 将字符串变量ph2[]初始化为空字符串。
  • 使用变量i遍历范围[0, N)并执行以下任务:
    • ph[0]添加到变量ph2[]。
    • 将字符串变量S[]初始化为空字符串。
    • 使用变量j遍历范围[0, N-1)并执行以下任务:
      • str(abs(int(ph[j])-int(ph[j+1])))的值添加到变量S[]。
    • ph的值设置为S[]。
  • 执行上述步骤后,打印ph2[]的值作为答案。

下面是上述方法的实现。

C++

// C++ code for the above approach
#include 
using namespace std;
 
// Function to find lucky number.
string phone(string ph, int n)
{
 
    // ph2 is empty string to store lucky number.
    string ph2 = "";
 
    // For loop for finding lucky number
    for (int i = 0; i < ph.length(); i++) {
 
        // Add first element of ph to ph2
        ph2 += ph[0];
 
        // S for storing the difference
        string S = "";
 
        // Loop to calculate the absolute difference
        for (int j = 0; j < ph.length(); j++) {
            int x = abs(int(ph[j]) - int(ph[j + 1]));
            S += x + '0';
        }
 
        // Assigning S to ph.
        ph = S;
    }
 
    // Return the lucky number
    return ph2;
}
 
// Driver Code
int main()
{
 
    // Original number
    string ph = "9827218706";
 
    // Call phone function
    string num = phone(ph, ph.length());
 
    // Printing lucky number
    cout << (num);
}
 
// This code is contributed by Potta Lokesh

Java

// Java program for the above approach
import java.util.*;
 class GFG {
 
  // Function to find lucky number.
  static String phone(String ph, int n)
  {
 
    // ph2 is empty string to store lucky number.
    String ph2 = "";
 
    // For loop for finding lucky number
    for (int i = 0; i < ph.length(); i++)
    {
 
      // Add first element of ph to ph2
      ph2 += ph.charAt(0);
 
      // S for storing the difference
      String S = "";
 
      // Loop to calculate the absolute difference
      for (int j = 0; j < ph.length()-1; j++)
      {
        int x = Math.abs(ph.charAt(j) - ph.charAt(j+1));
        S += (char)(x + '0');
      }
 
      // Assigning S to ph.
      ph = S;
    }
 
    // Return the lucky number
    return ph2;
     
  }
 
  // Driver Code
  public static void main(String args[])
  {
    // Original number
    String ph = "9827218706";
 
    // Call phone function
    String num = phone(ph, ph.length());
 
    // Printing lucky number
    System.out.println(num);
  }
}
 
// This code is contributed by avijitmondal1998

Python3

# Function to find lucky number.
def phone(ph, n):
 
    # ph2 is empty string to store lucky number.
    ph2 = ""
 
    # For loop for finding lucky number
    for i in range(len(ph)):
 
        # Add first element of ph to ph2
        ph2 += ph[0]
 
        # S for storing the difference
        S = ""
 
        # Loop to calculate the absolute difference
        for j in range(len(ph)-1):
            x = abs(int(ph[j])-int(ph[j + 1]))
            S += str(x)
 
        # Assigning S to ph.
        ph = S
 
    # Return the lucky number
    return ph2
 
 
# Original number
ph = "9827218706"
 
# Call phone function
num = phone(ph, len(ph))
 
# Printing lucky number
print(num)

C#

// C# code for the above approach
using System;
class GFG
{
 
  // Function to find lucky number.
  static string phone(string ph, int n)
  {
 
    // ph2 is empty string to store lucky number.
    string ph2 = "";
 
    // For loop for finding lucky number
    for (int i = 0; i < ph.Length; i++)
    {
 
      // Add first element of ph to ph2
      ph2 += ph[0];
 
      // S for storing the difference
      string S = "";
 
      // Loop to calculate the absolute difference
      for (int j = 0; j < ph.Length; j++)
      {
        int x = Math.Abs(ph[j] - ph[j + 1]);
        S += x + '0';
      }
 
      // Assigning S to ph.
      ph = S;
    }
 
    // Return the lucky number
    return ph2;
  }
 
  // Driver Code
  public static void Main()
  {
 
    // Original number
    string ph = "9827218706";
 
    // Call phone function
    string num = phone(ph, ph.Length);
 
    // Printing lucky number
    Console.WriteLine (num);
  }
}
 
// This code is contributed by ukasp.

Javascript


输出
9154301011

时间复杂度: O(N*N)
辅助空间: O(N)