📜  求平方并加X时不以T1或T2结尾的整数

📅  最后修改于: 2021-04-27 19:30:25             🧑  作者: Mango

给定一个由N个整数组成的数组。给定两个数字T1和T2以及一个数字X。任务是找出它们之间的整数,当它们被平方并加X时,这些整数不以T1T2结尾。如果不存在这样的整数,则打印-1

例子:

方法:

  • 将布尔变量标志初始化为true
  • 遍历数组a [n]中的元素。
  • X的和与a [i]平方存储在变量temp中
  • 检查temp中的最后一位数字是否既不是T1也不是T2
  • 如果是,则打印temp中的值,并将标志更改为false
  • 遍历数组中的所有元素后,如果标志true,则打印-1

下面是上述方法的实现:

C++
// C++ program to find the integers
// that ends with either T1 or T2
// when squared and added X
  
#include 
using namespace std;
  
// Function to print the elements
// Not ending with T1 or T2
void findIntegers(int n, int a[],
                   int x, int t1, int t2)
{
  
    // Flag to check if none of the elements
    // Do not end with t1 or t2
    bool flag = true;
  
    // Traverse through all the elements
    for (int i = 0; i < n; i++) {
  
        // Temporary variable to store the value
        int temp = pow(a[i], 2) + x;
  
        // If the last digit is neither t1
        // nor t2 then
        if (temp % 10 != t1 && temp % 10 != t2) {
  
            // Print the number
            cout << temp << " ";
  
            // Set the flag as False
            flag = false;
        }
    }
  
    // If none of the elements
    // meets the specification
    if (flag)
        cout << "-1";
}
  
// Driver Code
int main()
{
    // Test case 1
    int N = 4, X = 10, T1 = 5, T2 = 6;
    int a[N] = { 3, 1, 4, 7 };
  
    // Call the function
    findIntegers(N, a, X, T1, T2);
    cout << endl;
  
    // Test case 2
    N = 4, X = 2, T1 = 5, T2 = 6;
    int b[N] = { 2, 18, 22, 8 };
  
    // Call the function
    findIntegers(N, b, X, T1, T2);
  
    return 0;
}


Java
// Java program to find the integers 
// that ends with either T1 or T2 
// when squared and added X 
class GFG 
{
  
    // Function to print the elements 
    // Not ending with T1 or T2 
    static void findIntegers(int n, int a[], 
                             int x, int t1, int t2) 
    { 
      
        // Flag to check if none of the elements 
        // Do not end with t1 or t2 
        boolean flag = true; 
      
        // Traverse through all the elements 
        for (int i = 0; i < n; i++) 
        { 
      
            // Temporary variable to store the value 
            int temp = (int)Math.pow(a[i], 2) + x; 
      
            // If the last digit is neither t1 
            // nor t2 then 
            if (temp % 10 != t1 && temp % 10 != t2) 
            { 
      
                // Print the number 
                System.out.print(temp + " "); 
      
                // Set the flag as False 
                flag = false; 
            } 
        } 
      
        // If none of the elements 
        // meets the specification 
        if (flag)
        {
            System.out.println();
            System.out.print("-1"); 
        }
    } 
      
    // Driver Code 
    public static void main(String args[]) 
    { 
        // Test case 1 
        int N = 4;
        int X = 10;
        int T1 = 5; 
        int T2 = 6; 
        int a[] = { 3, 1, 4, 7 }; 
      
        // Call the function 
        findIntegers(N, a, X, T1, T2); 
      
        // Test case 2 
        N = 4; X = 2; T1 = 5; T2 = 6; 
        int b[] = { 2, 18, 22, 8 }; 
      
        // Call the function 
        findIntegers(N, b, X, T1, T2); 
    } 
}
  
// This code is contributed by AnkitRai01


Python3
# Python3 program to find the integers 
# that ends with either T1 or T2 
# when squared and added X 
  
# Function to print the elements 
# Not ending with T1 or T2 
def findIntegers(n, a, x, t1, t2): 
  
    # Flag to check if none of the elements 
    # Do not end with t1 or t2 
    flag = True
  
    # Traverse through all the elements 
    for i in range(n): 
  
        # Temporary variable to store the value 
        temp = pow(a[i], 2) + x 
  
        # If the last digit is neither t1 
        # nor t2 then 
        if(temp % 10 != t1 and 
           temp % 10 != t2): 
  
            # Print the number 
            print(temp, end = " ") 
  
            # Set the flag as False 
            flag = False
  
    # If none of the elements 
    # meets the specification 
    if flag: 
        print(-1) 
  
# Driver Code 
  
# Test case 1 
N , X , T1 , T2 = 4 , 10 , 5 , 6
a = [ 3, 1, 4, 7 ] 
  
# Call the function 
findIntegers(N, a, X, T1, T2); 
print() 
  
# Test case 2 
N , X , T1 , T2 = 4 , 2 , 5 , 6
b = [ 2, 18, 22, 8 ]
      
# Call the function 
findIntegers(N, b, X, T1, T2)
  
# This code is contributed by divyamohan123


C#
// C# program to find the integers 
// that ends with either T1 or T2 
// when squared and added X 
using System;
      
class GFG 
{
  
    // Function to print the elements 
    // Not ending with T1 or T2 
    static void findIntegers(int n, int []a, 
                             int x, int t1, int t2) 
    { 
      
        // Flag to check if none of the elements 
        // Do not end with t1 or t2 
        bool flag = true; 
      
        // Traverse through all the elements 
        for (int i = 0; i < n; i++) 
        { 
      
            // Temporary variable to store the value 
            int temp = (int)Math.Pow(a[i], 2) + x; 
      
            // If the last digit is neither t1 
            // nor t2 then 
            if (temp % 10 != t1 && 
                temp % 10 != t2) 
            { 
      
                // Print the number 
                Console.Write(temp + " "); 
      
                // Set the flag as False 
                flag = false; 
            } 
        } 
      
        // If none of the elements 
        // meets the specification 
        if (flag)
        {
            Console.WriteLine();
            Console.Write("-1"); 
        }
    } 
      
    // Driver Code 
    public static void Main(String []args) 
    { 
        // Test case 1 
        int N = 4;
        int X = 10;
        int T1 = 5; 
        int T2 = 6; 
        int []a = { 3, 1, 4, 7 }; 
      
        // Call the function 
        findIntegers(N, a, X, T1, T2); 
      
        // Test case 2 
        N = 4; X = 2; T1 = 5; T2 = 6; 
        int []b = { 2, 18, 22, 8 }; 
      
        // Call the function 
        findIntegers(N, b, X, T1, T2); 
    } 
}
  
// This code is contributed by 29AjayKumar


输出:
19 11 59 
-1