📌  相关文章
📜  在给定范围内找到一个不同的对(x,y),以使x除以y

📅  最后修改于: 2021-06-25 18:41:18             🧑  作者: Mango

给定从l到r的正整数范围。找出这样的一对整数(x,y):l <= x,y <= r,x!= y,x除以y。
如果有多个对,则需要找到其中任何一个。

例子:

Input : 1 10 
Output : 1 2

Input : 2 4
Output : 2 4

蛮力解决方案是遍历(l,r)的给定范围,并找到x除以y且x!= y的第一个出现点,如果l与r之间的差很小,则此解决方案是可行的。
该解决方案的时间复杂度为O((rl)*(rl))。

以下是基于蛮力解决方案的代码。

C++
// C++ implementation of the approach
#include 
using namespace std;
  
void findpair(int l, int r)
{
    int c = 0;
    for (int i = l; i <= r; i++) {
        for (int j = i + 1; j <= r; j++) {
            if (j % i == 0 && j != i) {
                cout << i << ", " << j;
                c = 1;
                break;
            }
        }
        if (c == 1)
            break;
    }
}
  
int main()
{
    int l = 1, r = 10;
    findpair(l, r);
}


Java
// Java implementation of the approach 
class GFG
{
  
static void findpair(int l, int r) 
{ 
    int c = 0; 
    for (int i = l; i <= r; i++) 
    { 
        for (int j = i + 1; j <= r; j++) 
        { 
            if (j % i == 0 && j != i) 
            { 
                System.out.println( i +", " + j); 
                c = 1; 
                break; 
            } 
        } 
        if (c == 1) 
            break; 
    } 
} 
  
// Driver code
public static void main(String args[]) 
{ 
    int l = 1, r = 10; 
    findpair(l, r); 
} 
}
  
// This code is contributed by Arnab Kundu


Python 3
# Python 3 implementation of the approach
def findpair(l, r):
    c = 0
    for i in range(l, r + 1):
        for j in range(i + 1, r + 1):
            if (j % i == 0 and j != i):
                print( i, ", ", j)
                c = 1
                break
        if (c == 1):
            break
      
# Driver Code
if __name__ == "__main__":
  
    l = 1
    r = 10
    findpair(l, r)
  
# This code is contributed by ita_c


C#
// C# implementation of the approach
using System;
  
class GFG
{
      
static void findpair(int l, int r)
{
    int c = 0;
    for (int i = l; i <= r; i++) 
    {
        for (int j = i + 1; j <= r; j++) 
        {
            if (j % i == 0 && j != i) 
            {
                Console.Write( i + ", " + j);
                c = 1;
                break;
            }
        }
        if (c == 1)
            break;
    }
}
  
// Driver code
static void Main()
{
    int l = 1, r = 10;
    findpair(l, r);
}
}
  
// This code is contributed by mits


PHP


C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function to return the possible pair
void findpair(int l, int r)
{
    // ans1, ans2 store value of x 
    // and y respectively
    int ans1 = l;
    int ans2 = 2 * l;
  
    cout << ans1 << ", " << ans2 << endl;
}
  
// Driver Code
int main()
{
    int l = 1, r = 10;
    findpair(l, r);
}


Java
// Java implementation of the approach 
class GFG
{
  
// Function to return the possible pair 
static void findpair(int l, int r) 
{ 
    // ans1, ans2 store value of x 
    // and y respectively 
    int ans1 = l; 
    int ans2 = 2 * l; 
  
    System.out.println( ans1 + ", " + ans2 ); 
} 
  
// Driver Code 
public static void main(String args[])
{ 
    int l = 1, r = 10; 
    findpair(l, r); 
} 
}
  
// This code is contruibuted by Arnab Kundu


Python3
# Python3 implementation of the approach
  
# Function to return the possible pair
def findpair(l, r):
      
    # ans1, ans2 store value of x
    # and y respectively
    ans1 = l
    ans2 = 2 * l
  
    print(ans1, ", ", ans2)
  
# Driver Code
if __name__ == '__main__':
    l, r = 1, 10
    findpair(l, r)
  
# This code is contributed 
# by 29AjayKumar


C#
// C# implementation of the approach 
using System;
  
class GFG
{
  
    // Function to return the possible pair 
    static void findpair(int l, int r) 
    { 
        // ans1, ans2 store value of x 
        // and y respectively 
        int ans1 = l; 
        int ans2 = 2 * l; 
      
        Console.WriteLine( ans1 + ", " + ans2 ); 
    } 
      
    // Driver Code 
    public static void Main()
    { 
        int l = 1, r = 10; 
        findpair(l, r); 
    } 
}
  
// This code is contruibuted by Ryuga


PHP


输出:
1, 2

高效的解决方案:
如果找到l和2l的值,则可以通过O(1)时间复杂度来解决该问题。

解释:
1)我们知道,您可以拥有的y / x最小值为2,如果在该范围内,则2也在给定范围内。

2)当您增加x的值时,x和2x之间的差异也会增加,因此l和2l将是落入给定范围内的最小对。

下面是上述方法的实现:

C++

// C++ implementation of the approach
#include 
using namespace std;
  
// Function to return the possible pair
void findpair(int l, int r)
{
    // ans1, ans2 store value of x 
    // and y respectively
    int ans1 = l;
    int ans2 = 2 * l;
  
    cout << ans1 << ", " << ans2 << endl;
}
  
// Driver Code
int main()
{
    int l = 1, r = 10;
    findpair(l, r);
}

Java

// Java implementation of the approach 
class GFG
{
  
// Function to return the possible pair 
static void findpair(int l, int r) 
{ 
    // ans1, ans2 store value of x 
    // and y respectively 
    int ans1 = l; 
    int ans2 = 2 * l; 
  
    System.out.println( ans1 + ", " + ans2 ); 
} 
  
// Driver Code 
public static void main(String args[])
{ 
    int l = 1, r = 10; 
    findpair(l, r); 
} 
}
  
// This code is contruibuted by Arnab Kundu

Python3

# Python3 implementation of the approach
  
# Function to return the possible pair
def findpair(l, r):
      
    # ans1, ans2 store value of x
    # and y respectively
    ans1 = l
    ans2 = 2 * l
  
    print(ans1, ", ", ans2)
  
# Driver Code
if __name__ == '__main__':
    l, r = 1, 10
    findpair(l, r)
  
# This code is contributed 
# by 29AjayKumar

C#

// C# implementation of the approach 
using System;
  
class GFG
{
  
    // Function to return the possible pair 
    static void findpair(int l, int r) 
    { 
        // ans1, ans2 store value of x 
        // and y respectively 
        int ans1 = l; 
        int ans2 = 2 * l; 
      
        Console.WriteLine( ans1 + ", " + ans2 ); 
    } 
      
    // Driver Code 
    public static void Main()
    { 
        int l = 1, r = 10; 
        findpair(l, r); 
    } 
}
  
// This code is contruibuted by Ryuga

的PHP


输出:
1, 2

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。