📜  生成矩形内的所有积分点

📅  最后修改于: 2021-04-24 19:53:22             🧑  作者: Mango

给定一个长度为L且宽度为W的矩形,任务是生成所有整数坐标(X,Y),该整数位于尺寸为L * W的矩形中,该矩形的顶点之一为原点(0,0)

例子:

方法:该问题可以通过从范围0生成所有积分数为L解出X坐标-和从0W用于Y使用rand()函数坐标- 。请按照以下步骤解决问题:

  1. 创建一组对以存储位于矩形内的所有坐标(X,Y)。
  2. 使用方程rand()%L生成介于0L之间的所有整数,使用rand()%W生成介于0W之间的所有整数。
  3. 打印矩形内所有可能的L×W坐标(X,Y)。

下面是上述方法的实现:

C++
// C++ program to implement
// the above approach
#include 
using namespace std;
 
// Function to generate coordinates
// lying within the rectangle
void generatePoints(int L, int W)
{
    // Store all possible coordinates
    // that lie within the rectangle
    set > hash;
 
    // Stores the number of possible
    // coordinates that lie within
    // the rectangle
    int total = (L * W);
 
    // Use current time as seed
    // for random generator
    srand(time(0));
 
    // Generate all possible
    // coordinates
    while (total--) {
        // Generate all possible
        // X-coordinates
        int X = rand() % L;
 
        // Generate all possible
        // Y-coordinates
        int Y = rand() % W;
 
        // If coordinates(X, Y) has
        // not been generated already
        while (hash.count({ X, Y })) {
            X = rand() % L;
            Y = rand() % W;
        }
 
        // Insert the coordinates(X, Y)
        hash.insert({ X, Y });
    }
 
    // Print the coordinates
    for (auto points : hash) {
        cout << "(" << points.first << ", "
             << points.second << ") ";
    }
}
 
// Driver Code
int main()
{
    // Rectangle dimensions
    int L = 3, W = 2;
 
    generatePoints(L, W);
}


Python3
# Python3 program to implement
# the above approach
import time
import random
 
random.seed(time.time())
 
# Function to generate coordinates
# lying within the rectangle
def generatePoints(L, W):
     
    # Store all possible coordinates
    # that lie within the rectangle
    hash = {}
 
    # Stores the number of possible
    # coordinates that lie within
    # the rectangle
    total = (L * W)
 
    # Generate all possible
    # coordinates
    for i in range(total):
         
        # Generate all possible
        # X-coordinates
        X = random.randint(0, L) % L
 
        # Generate all possible
        # Y-coordinates
        Y = random.randint(0, W) % W
 
        # If coordinates(X, Y) has
        # not been generated already
        while ((X, Y) in hash):
            X = random.randint(0, L) % L
            Y = random.randint(0, W) % W
 
        # Insert the coordinates(X, Y)
        hash[(X, Y)] = 1
 
    # Print the coordinates
    for points in sorted(hash):
        print("(", points[0],
             ", ", points[1],
             ") ", end = "")
 
# Driver code
if __name__ == '__main__':
     
    # Rectangle dimensions
    L, W = 3, 2
 
    generatePoints(L, W)
 
# This code is contributed by mohit kumar 29


C#
// C# program to implement
// the above approach
using System;
using System.Collections;
using System.Collections.Generic;
 
class GFG{
 
public class store : IComparer>
{
    public int Compare(KeyValuePair x,
                       KeyValuePair y)
    {
        if (x.Key != y.Key)
        {
            return x.Key.CompareTo(y.Key);   
        }
        else
        {
            return x.Value.CompareTo(y.Value);   
        }
    }
}
      
// Function to generate coordinates
// lying within the rectangle
static void generatePoints(int L, int W)
{
     
    // Store all possible coordinates
    // that lie within the rectangle
    SortedSet> hash = new SortedSet>(new store());
  
    // Stores the number of possible
    // coordinates that lie within
    // the rectangle
    int total = (L * W);
  
    // For random generator
    Random rand = new Random();
  
    // Generate all possible
    // coordinates
    while ((total--) != 0)
    {
         
        // Generate all possible
        // X-coordinates
        int X = rand.Next() % L;
  
        // Generate all possible
        // Y-coordinates
        int Y = rand.Next() % W;
  
        // If coordinates(X, Y) has
        // not been generated already
        while (hash.Contains(
            new KeyValuePair(X, Y)))
        {
            X = rand.Next() % L;
            Y = rand.Next() % W;
        }
  
        // Insert the coordinates(X, Y)
        hash.Add(new KeyValuePair(X, Y));
    }
  
    // Print the coordinates
    foreach(KeyValuePair x in hash)
    {
        Console.Write("(" + x.Key + ", " +
                          x.Value + ") ");
    }
}
 
// Driver Code
public static void Main(string[] args)
{
     
    // Rectangle dimensions
    int L = 3, W = 2;
  
    generatePoints(L, W);
}
}
 
// This code is contributed by rutvik_56


输出:
(0, 0) (0, 1) (1, 0) (1, 1) (2, 0) (2, 1)




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