📜  惊人的数字

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

令人惊讶的数字是一个数字N,其表示可以分解为两部分,即ab ,使得N等于从ab的整数之和,而a + b = N ,其中‘+’表示串联。

几个惊人的数字是:

检查N是否是一个令人惊讶的数字

给定数字N ,任务是检查N是否是一个令人惊讶的数字。如果N是一个惊人的数字,则打印“是”,否则打印“否”

例子:

方法:这个想法是运行i和j的两个循环,以找到i之前所有整数的和,直到和等于> =N。如果在任何时间点总和等于N,那么我们还将检查是否i和j的串联等于或不等于N。如果等于,则该数字是一个惊人的数字

下面是上述方法的实现:

C++
// C++ implementation for the
// above approach
  
#include 
using namespace std;
  
// Function to concatenate
// two integers into one
int concat(int a, int b)
{
  
    // Convert both the integers to string
    string s1 = to_string(a);
    string s2 = to_string(b);
  
    // Concatenate both strings
    string s = s1 + s2;
  
    // Convert the concatenated string
    // to integer
    int c = stoi(s);
  
    // return the formed integer
    return c;
}
  
// Function to check if N is a
// Astonishing number
bool isAstonishing(int n)
{
    // Loop to find sum of all integers
    // from i till the sum becomes >= n
    for (int i = 1; i < n; i++) {
  
        // variable to store
        // sum of all integers
        // from i to j and
        // check if sum and
        // concatenation equals n or not
        int sum = 0;
        for (int j = i; j < n; j++) {
  
            sum += j;
  
            if (sum == n) {
  
                // finding concatenation
                // of i and j
                int concatenation
                    = concat(i, j);
  
                // condition for
                // Astonishing number
                if (concatenation == n) {
                    return true;
                }
            }
        }
    }
    return false;
}
  
// Driver Code
int main()
{
    // Given Number
    int n = 429;
  
    // Function Call
    if (isAstonishing(n))
        cout << "Yes";
    else
        cout << "No";
    return 0;
}


Java
// Java implementation for the
// above approach
import java.io.*;
class GFG{
  
// Function to concatenate
// two integers into one
static int concat(int a, int b)
{
  
    // Convert both the integers to String
    String s1 = Integer.toString(a);
    String s2 = Integer.toString(b);
  
    // Concatenate both Strings
    String s = s1 + s2;
  
    // Convert the concatenated String
    // to integer
    int c = Integer.parseInt(s);
  
    // return the formed integer
    return c;
}
  
// Function to check if N is a
// Astonishing number
static boolean isAstonishing(int n)
{
    // Loop to find sum of all integers
    // from i till the sum becomes >= n
    for (int i = 1; i < n; i++) 
    {
  
        // variable to store
        // sum of all integers
        // from i to j and
        // check if sum and
        // concatenation equals n or not
        int sum = 0;
        for (int j = i; j < n; j++)
        {
            sum += j;
  
            if (sum == n) 
            {
  
                // finding concatenation
                // of i and j
                int concatenation = concat(i, j);
  
                // condition for
                // Astonishing number
                if (concatenation == n) 
                {
                    return true;
                }
            }
        }
    }
    return false;
}
  
// Driver Code
public static void main (String[] args)
{
    // Given Number
    int n = 429;
  
    // Function Call
    if (isAstonishing(n))
        System.out.println("Yes");
    else
        System.out.println("No");
}
}
  
// This code is contributed by shubhamsingh10


Python3
# Python3 implementation for the 
# above approach 
  
# Function to concatenate 
# two integers into one 
def concat(a, b):
      
    # Convert both the integers to string 
    s1 = str(a)
    s2 = str(b)
  
    # Concatenate both strings 
    s = s1 + s2
  
    # Convert the concatenated string 
    # to integer 
    c = int(s)
  
    # return the formed integer 
    return c
  
# Function to check if N is a 
# Astonishing number 
def isAstonishing(n):
      
    # Loop to find sum of all integers 
    # from i till the sum becomes >= n 
    for i in range(n):
          
        # variable to store 
        # sum of all integers 
        # from i to j and 
        # check if sum and 
        # concatenation equals n or not 
        sum = 0
        for j in range(i, n):
              
            sum += j
              
            if (sum == n):
                  
                # finding concatenation 
                # of i and j 
                concatenation = concat(i, j)
  
                # condition for 
                # Astonishing number 
                if (concatenation == n):
                    return True
    return False
      
# Driver Code 
  
# Given Number 
n = 429
  
# Function Call 
if (isAstonishing(n)):
    print('Yes')
else:
    print('No')
  
# This code is contributed by Yatin


C#
// C# implementation for the
// above approach
using System;
class GFG{
  
// Function to concatenate
// two integers into one
static int concat(int a, int b)
{
  
    // Convert both the integers to String
    String s1 = a.ToString();
    String s2 = b.ToString();
  
    // Concatenate both Strings
    String s = s1 + s2;
  
    // Convert the concatenated String
    // to integer
    int c = Int32.Parse(s);
  
    // return the formed integer
    return c;
}
  
// Function to check if N is a
// Astonishing number
static bool isAstonishing(int n)
{
    // Loop to find sum of all integers
    // from i till the sum becomes >= n
    for (int i = 1; i < n; i++) 
    {
  
        // variable to store
        // sum of all integers
        // from i to j and
        // check if sum and
        // concatenation equals n or not
        int sum = 0;
        for (int j = i; j < n; j++)
        {
            sum += j;
  
            if (sum == n) 
            {
  
                // finding concatenation
                // of i and j
                int concatenation = concat(i, j);
  
                // condition for
                // Astonishing number
                if (concatenation == n) 
                {
                    return true;
                }
            }
        }
    }
    return false;
}
  
// Driver Code
public static void Main(String[] args)
{
    // Given Number
    int n = 429;
  
    // Function Call
    if (isAstonishing(n))
        Console.WriteLine("Yes");
    else
        Console.WriteLine("No");
}
}
  
// This code is contributed by 29AjayKumar


输出:
Yes

参考: https : //oeis.org/A186074