Thuật toán kiểm tra số nguyên liên tiếp python

Một giải pháp ngắn hoạt động mà không cần nhập bổ sung. Nó chấp nhận bất kỳ lần lặp nào, sắp xếp các đầu vào chưa được sắp xếp và loại bỏ các mục trùng lặp

def ranges[nums]:
    nums = sorted[set[nums]]
    gaps = [[s, e] for s, e in zip[nums, nums[1:]] if s+1 < e]
    edges = iter[nums[:1] + sum[gaps, []] + nums[-1:]]
    return list[zip[edges, edges]]

Thí dụ

>>> ranges[[2, 3, 4, 7, 8, 9, 15]]
[[2, 4], [7, 9], [15, 15]]

>>> ranges[[-1, 0, 1, 2, 3, 12, 13, 15, 100]]
[[-1, 3], [12, 13], [15, 15], [100, 100]]

>>> ranges[range[100]]
[[0, 99]]

>>> ranges[[0]]
[[0, 0]]

>>> ranges[[]]
[]

Điều này giống như giải pháp của @ dansalmo mà tôi thấy tuyệt vời, mặc dù hơi khó đọc và áp dụng [vì nó không được cung cấp dưới dạng hàm]

Lưu ý rằng nó có thể dễ dàng được sửa đổi để tạo ra các phạm vi mở "truyền thống" [start, end], bởi e. g. thay đổi tuyên bố trở lại

Cho hai số nguyên dương N và K, nhiệm vụ là kiểm tra xem số nguyên N đã cho có thể biểu diễn dưới dạng tích của K số nguyên liên tiếp hay không?. Nếu tìm thấy là đúng, sau đó in “Yes”. Nếu không, hãy in “Không”

ví dụ

Đầu vào. N = 210, K = 3
Đầu ra. Có
Giải thích. 210 có thể được thể hiện dưới dạng 5 * 6 * 7.

Đầu vào. N = 780, K =4
Đầu ra. Không

Khuyến khích. Vui lòng thử cách tiếp cận của bạn trên {IDE} trước, trước khi chuyển sang giải pháp

Cách tiếp cận. Vấn đề đã cho có thể được giải quyết bằng cách sử dụng Kỹ thuật cửa sổ trượt. Thực hiện theo các bước dưới đây để giải quyết vấn đề

  • Khởi tạo hai số nguyên, giả sử Kthroot và tích, để lưu căn thứ K của số nguyên N và tích của K số nguyên liên tiếp tương ứng
  • Lưu tích của các số nguyên trong phạm vi [1, K] trong biến tích
  • Mặt khác, lặp lại phạm vi [2, Kthroot] và thực hiện các bước sau
    • Nếu giá trị của sản phẩm bằng N, sau đó in “Có” và thoát ra khỏi vòng lặp
    • Cập nhật giá trị của sản phẩm là [product*[i + K – 1]] / [i – 1]
  • Sau khi hoàn thành các bước trên, nếu không thỏa mãn các trường hợp trên thì in ra “Không” vì N không thể biểu diễn dưới dạng tích của K số nguyên liên tiếp

Dưới đây là việc thực hiện các phương pháp trên

C++14




// C++ program for the above approach

#include

using namespace std;

 

________số 8

// as the product of K consecutive integers

>>> ranges[[2, 3, 4, 7, 8, 9, 15]]
[[2, 4], [7, 9], [15, 15]]

>>> ranges[[-1, 0, 1, 2, 3, 12, 13, 15, 100]]
[[-1, 3], [12, 13], [15, 15], [100, 100]]

>>> ranges[range[100]]
[[0, 99]]

>>> ranges[[0]]
[[0, 0]]

>>> ranges[[]]
[]
0
>>> ranges[[2, 3, 4, 7, 8, 9, 15]]
[[2, 4], [7, 9], [15, 15]]

>>> ranges[[-1, 0, 1, 2, 3, 12, 13, 15, 100]]
[[-1, 3], [12, 13], [15, 15], [100, 100]]

>>> ranges[range[100]]
[[0, 99]]

>>> ranges[[0]]
[[0, 0]]

>>> ranges[[]]
[]
1 // C++ program for the above approach0
>>> ranges[[2, 3, 4, 7, 8, 9, 15]]
[[2, 4], [7, 9], [15, 15]]

>>> ranges[[-1, 0, 1, 2, 3, 12, 13, 15, 100]]
[[-1, 3], [12, 13], [15, 15], [100, 100]]

>>> ranges[range[100]]
[[0, 99]]

>>> ranges[[0]]
[[0, 0]]

>>> ranges[[]]
[]
1 // C++ program for the above approach2

// C++ program for the above approach3

// C++ program for the above approach4// C++ program for the above approach5 // C++ program for the above approach6 // C++ program for the above approach7

// C++ program for the above approach4

// C++ program for the above approach4#include 0

// C++ program for the above approach4

>>> ranges[[2, 3, 4, 7, 8, 9, 15]]
[[2, 4], [7, 9], [15, 15]]

>>> ranges[[-1, 0, 1, 2, 3, 12, 13, 15, 100]]
[[-1, 3], [12, 13], [15, 15], [100, 100]]

>>> ranges[range[100]]
[[0, 99]]

>>> ranges[[0]]
[[0, 0]]

>>> ranges[[]]
[]
1 #include 3
>>> ranges[[2, 3, 4, 7, 8, 9, 15]]
[[2, 4], [7, 9], [15, 15]]

>>> ranges[[-1, 0, 1, 2, 3, 12, 13, 15, 100]]
[[-1, 3], [12, 13], [15, 15], [100, 100]]

>>> ranges[range[100]]
[[0, 99]]

>>> ranges[[0]]
[[0, 0]]

>>> ranges[[]]
[]
1#include 5#include 6#include 7// C++ program for the above approach6#include 9

// C++ program for the above approach4

// C++ program for the above approach4using2

// C++ program for the above approach4using4

// C++ program for the above approach4

>>> ranges[[2, 3, 4, 7, 8, 9, 15]]
[[2, 4], [7, 9], [15, 15]]

>>> ranges[[-1, 0, 1, 2, 3, 12, 13, 15, 100]]
[[-1, 3], [12, 13], [15, 15], [100, 100]]

>>> ranges[range[100]]
[[0, 99]]

>>> ranges[[0]]
[[0, 0]]

>>> ranges[[]]
[]
1 using7

// C++ program for the above approach4

// C++ program for the above approach4namespace0

// C++ program for the above approach4namespace2____63

>>> ranges[[2, 3, 4, 7, 8, 9, 15]]
[[2, 4], [7, 9], [15, 15]]

>>> ranges[[-1, 0, 1, 2, 3, 12, 13, 15, 100]]
[[-1, 3], [12, 13], [15, 15], [100, 100]]

>>> ranges[range[100]]
[[0, 99]]

>>> ranges[[0]]
[[0, 0]]

>>> ranges[[]]
[]
1 namespace5

// C++ program for the above approach4// C++ program for the above approach3

namespace8

namespace8std;0

namespace8std;2

// C++ program for the above approach4std;4

// C++ program for the above approach4

// C++ program for the above approach4std;7

// C++ program for the above approach4std;9 // Function to check if N can be expressed0

namespace8// Function to check if N can be expressed2 // Function to check if N can be expressed3// Function to check if N can be expressed4

// C++ program for the above approach4

// C++ program for the above approach4// Function to check if N can be expressed7

// C++ program for the above approach4// C++ program for the above approach3

namespace8

namespace8// as the product of K consecutive integers2

namespace8// as the product of K consecutive integers4

namespace8namespace2____63

>>> ranges[[2, 3, 4, 7, 8, 9, 15]]
[[2, 4], [7, 9], [15, 15]]

>>> ranges[[-1, 0, 1, 2, 3, 12, 13, 15, 100]]
[[-1, 3], [12, 13], [15, 15], [100, 100]]

>>> ranges[range[100]]
[[0, 99]]

>>> ranges[[0]]
[[0, 0]]

>>> ranges[[]]
[]
1 // as the product of K consecutive integers9

namespace8// C++ program for the above approach3

>>> ranges[[2, 3, 4, 7, 8, 9, 15]]
[[2, 4], [7, 9], [15, 15]]

>>> ranges[[-1, 0, 1, 2, 3, 12, 13, 15, 100]]
[[-1, 3], [12, 13], [15, 15], [100, 100]]

>>> ranges[range[100]]
[[0, 99]]

>>> ranges[[0]]
[[0, 0]]

>>> ranges[[]]
[]
02

>>> ranges[[2, 3, 4, 7, 8, 9, 15]]
[[2, 4], [7, 9], [15, 15]]

>>> ranges[[-1, 0, 1, 2, 3, 12, 13, 15, 100]]
[[-1, 3], [12, 13], [15, 15], [100, 100]]

>>> ranges[range[100]]
[[0, 99]]

>>> ranges[[0]]
[[0, 0]]

>>> ranges[[]]
[]
02
>>> ranges[[2, 3, 4, 7, 8, 9, 15]]
[[2, 4], [7, 9], [15, 15]]

>>> ranges[[-1, 0, 1, 2, 3, 12, 13, 15, 100]]
[[-1, 3], [12, 13], [15, 15], [100, 100]]

>>> ranges[range[100]]
[[0, 99]]

>>> ranges[[0]]
[[0, 0]]

>>> ranges[[]]
[]
04

>>> ranges[[2, 3, 4, 7, 8, 9, 15]]
[[2, 4], [7, 9], [15, 15]]

>>> ranges[[-1, 0, 1, 2, 3, 12, 13, 15, 100]]
[[-1, 3], [12, 13], [15, 15], [100, 100]]

>>> ranges[range[100]]
[[0, 99]]

>>> ranges[[0]]
[[0, 0]]

>>> ranges[[]]
[]
02
>>> ranges[[2, 3, 4, 7, 8, 9, 15]]
[[2, 4], [7, 9], [15, 15]]

>>> ranges[[-1, 0, 1, 2, 3, 12, 13, 15, 100]]
[[-1, 3], [12, 13], [15, 15], [100, 100]]

>>> ranges[range[100]]
[[0, 99]]

>>> ranges[[0]]
[[0, 0]]

>>> ranges[[]]
[]
06

>>> ranges[[2, 3, 4, 7, 8, 9, 15]]
[[2, 4], [7, 9], [15, 15]]

>>> ranges[[-1, 0, 1, 2, 3, 12, 13, 15, 100]]
[[-1, 3], [12, 13], [15, 15], [100, 100]]

>>> ranges[range[100]]
[[0, 99]]

>>> ranges[[0]]
[[0, 0]]

>>> ranges[[]]
[]
02
>>> ranges[[2, 3, 4, 7, 8, 9, 15]]
[[2, 4], [7, 9], [15, 15]]

>>> ranges[[-1, 0, 1, 2, 3, 12, 13, 15, 100]]
[[-1, 3], [12, 13], [15, 15], [100, 100]]

>>> ranges[range[100]]
[[0, 99]]

>>> ranges[[0]]
[[0, 0]]

>>> ranges[[]]
[]
08

>>> ranges[[2, 3, 4, 7, 8, 9, 15]]
[[2, 4], [7, 9], [15, 15]]

>>> ranges[[-1, 0, 1, 2, 3, 12, 13, 15, 100]]
[[-1, 3], [12, 13], [15, 15], [100, 100]]

>>> ranges[range[100]]
[[0, 99]]

>>> ranges[[0]]
[[0, 0]]

>>> ranges[[]]
[]
02

>>> ranges[[2, 3, 4, 7, 8, 9, 15]]
[[2, 4], [7, 9], [15, 15]]

>>> ranges[[-1, 0, 1, 2, 3, 12, 13, 15, 100]]
[[-1, 3], [12, 13], [15, 15], [100, 100]]

>>> ranges[range[100]]
[[0, 99]]

>>> ranges[[0]]
[[0, 0]]

>>> ranges[[]]
[]
02
>>> ranges[[2, 3, 4, 7, 8, 9, 15]]
[[2, 4], [7, 9], [15, 15]]

>>> ranges[[-1, 0, 1, 2, 3, 12, 13, 15, 100]]
[[-1, 3], [12, 13], [15, 15], [100, 100]]

>>> ranges[range[100]]
[[0, 99]]

>>> ranges[[0]]
[[0, 0]]

>>> ranges[[]]
[]
11

>>> ranges[[2, 3, 4, 7, 8, 9, 15]]
[[2, 4], [7, 9], [15, 15]]

>>> ranges[[-1, 0, 1, 2, 3, 12, 13, 15, 100]]
[[-1, 3], [12, 13], [15, 15], [100, 100]]

>>> ranges[range[100]]
[[0, 99]]

>>> ranges[[0]]
[[0, 0]]

>>> ranges[[]]
[]
02std;9 // Function to check if N can be expressed0

>>> ranges[[2, 3, 4, 7, 8, 9, 15]]
[[2, 4], [7, 9], [15, 15]]

>>> ranges[[-1, 0, 1, 2, 3, 12, 13, 15, 100]]
[[-1, 3], [12, 13], [15, 15], [100, 100]]

>>> ranges[range[100]]
[[0, 99]]

>>> ranges[[0]]
[[0, 0]]

>>> ranges[[]]
[]
15// Function to check if N can be expressed2 // Function to check if N can be expressed3// Function to check if N can be expressed4

namespace8// C++ program for the above approach00

// C++ program for the above approach4std;4

// C++ program for the above approach4

// C++ program for the above approach4// C++ program for the above approach05

// C++ program for the above approach4// Function to check if N can be expressed2 // C++ program for the above approach08// Function to check if N can be expressed4

std;4

 

// C++ program for the above approach11

>>> ranges[[2, 3, 4, 7, 8, 9, 15]]
[[2, 4], [7, 9], [15, 15]]

>>> ranges[[-1, 0, 1, 2, 3, 12, 13, 15, 100]]
[[-1, 3], [12, 13], [15, 15], [100, 100]]

>>> ranges[range[100]]
[[0, 99]]

>>> ranges[[0]]
[[0, 0]]

>>> ranges[[]]
[]
1 // C++ program for the above approach13

// C++ program for the above approach3

// C++ program for the above approach4

>>> ranges[[2, 3, 4, 7, 8, 9, 15]]
[[2, 4], [7, 9], [15, 15]]

>>> ranges[[-1, 0, 1, 2, 3, 12, 13, 15, 100]]
[[-1, 3], [12, 13], [15, 15], [100, 100]]

>>> ranges[range[100]]
[[0, 99]]

>>> ranges[[0]]
[[0, 0]]

>>> ranges[[]]
[]
1 // C++ program for the above approach17

// C++ program for the above approach4

>>> ranges[[2, 3, 4, 7, 8, 9, 15]]
[[2, 4], [7, 9], [15, 15]]

>>> ranges[[-1, 0, 1, 2, 3, 12, 13, 15, 100]]
[[-1, 3], [12, 13], [15, 15], [100, 100]]

>>> ranges[range[100]]
[[0, 99]]

>>> ranges[[0]]
[[0, 0]]

>>> ranges[[]]
[]
1 // C++ program for the above approach20

 

// C++ program for the above approach4____322

 

// C++ program for the above approach4// Function to check if N can be expressed2 // C++ program for the above approach25

std;4

 

// C++ program for the above approach27

Java




// C++ program for the above approach28

// C++ program for the above approach29 // C++ program for the above approach30 // C++ program for the above approach31

 

// C++ program for the above approach32// Function to check if N can be expressed

// C++ program for the above approach32// as the product of K consecutive integers

// C++ program for the above approach32// C++ program for the above approach37 // C++ program for the above approach38

>>> ranges[[2, 3, 4, 7, 8, 9, 15]]
[[2, 4], [7, 9], [15, 15]]

>>> ranges[[-1, 0, 1, 2, 3, 12, 13, 15, 100]]
[[-1, 3], [12, 13], [15, 15], [100, 100]]

>>> ranges[range[100]]
[[0, 99]]

>>> ranges[[0]]
[[0, 0]]

>>> ranges[[]]
[]
1 // C++ program for the above approach0
>>> ranges[[2, 3, 4, 7, 8, 9, 15]]
[[2, 4], [7, 9], [15, 15]]

>>> ranges[[-1, 0, 1, 2, 3, 12, 13, 15, 100]]
[[-1, 3], [12, 13], [15, 15], [100, 100]]

>>> ranges[range[100]]
[[0, 99]]

>>> ranges[[0]]
[[0, 0]]

>>> ranges[[]]
[]
1 // C++ program for the above approach42

 

// C++ program for the above approach4// C++ program for the above approach5 // C++ program for the above approach45______346 // C++ program for the above approach47

 

// C++ program for the above approach4#include 0

// C++ program for the above approach4

>>> ranges[[2, 3, 4, 7, 8, 9, 15]]
[[2, 4], [7, 9], [15, 15]]

>>> ranges[[-1, 0, 1, 2, 3, 12, 13, 15, 100]]
[[-1, 3], [12, 13], [15, 15], [100, 100]]

>>> ranges[range[100]]
[[0, 99]]

>>> ranges[[0]]
[[0, 0]]

>>> ranges[[]]
[]
1 #include 3____11// C++ program for the above approach54

 

// C++ program for the above approach4using2

// C++ program for the above approach4using4

// C++ program for the above approach4

>>> ranges[[2, 3, 4, 7, 8, 9, 15]]
[[2, 4], [7, 9], [15, 15]]

>>> ranges[[-1, 0, 1, 2, 3, 12, 13, 15, 100]]
[[-1, 3], [12, 13], [15, 15], [100, 100]]

>>> ranges[range[100]]
[[0, 99]]

>>> ranges[[0]]
[[0, 0]]

>>> ranges[[]]
[]
1 // C++ program for the above approach61______362 // Function to check if N can be expressed4

 

// C++ program for the above approach4namespace0

// C++ program for the above approach4namespace2 namespace3

>>> ranges[[2, 3, 4, 7, 8, 9, 15]]
[[2, 4], [7, 9], [15, 15]]

>>> ranges[[-1, 0, 1, 2, 3, 12, 13, 15, 100]]
[[-1, 3], [12, 13], [15, 15], [100, 100]]

>>> ranges[range[100]]
[[0, 99]]

>>> ranges[[0]]
[[0, 0]]

>>> ranges[[]]
[]
1 // C++ program for the above approach70// C++ program for the above approach62// C++ program for the above approach72// C++ program for the above approach62// C++ program for the above approach74

// C++ program for the above approach75std;0

// C++ program for the above approach75std;2

// C++ program for the above approach4std;4

 

// C++ program for the above approach4std;7

// C++ program for the above approach4std;9____80

// C++ program for the above approach75// Function to check if N can be expressed2 // Function to check if N can be expressed3// Function to check if N can be expressed4

 

// C++ program for the above approach4// Function to check if N can be expressed7 // C++ program for the above approach3

// C++ program for the above approach75// as the product of K consecutive integers2

// C++ program for the above approach75// as the product of K consecutive integers4

// C++ program for the above approach75namespace2 namespace3

>>> ranges[[2, 3, 4, 7, 8, 9, 15]]
[[2, 4], [7, 9], [15, 15]]

>>> ranges[[-1, 0, 1, 2, 3, 12, 13, 15, 100]]
[[-1, 3], [12, 13], [15, 15], [100, 100]]

>>> ranges[range[100]]
[[0, 99]]

>>> ranges[[0]]
[[0, 0]]

>>> ranges[[]]
[]
1 #include 01#include 02#include 03// C++ program for the above approach62#include 05

 

namespace8

>>> ranges[[2, 3, 4, 7, 8, 9, 15]]
[[2, 4], [7, 9], [15, 15]]

>>> ranges[[-1, 0, 1, 2, 3, 12, 13, 15, 100]]
[[-1, 3], [12, 13], [15, 15], [100, 100]]

>>> ranges[range[100]]
[[0, 99]]

>>> ranges[[0]]
[[0, 0]]

>>> ranges[[]]
[]
04

_______68____409____362#include 11

namespace8#include 13____362#include 11

 

namespace8

>>> ranges[[2, 3, 4, 7, 8, 9, 15]]
[[2, 4], [7, 9], [15, 15]]

>>> ranges[[-1, 0, 1, 2, 3, 12, 13, 15, 100]]
[[-1, 3], [12, 13], [15, 15], [100, 100]]

>>> ranges[range[100]]
[[0, 99]]

>>> ranges[[0]]
[[0, 0]]

>>> ranges[[]]
[]
11

namespace8std;9// Function to check if N can be expressed0

#include 21// Function to check if N can be expressed2 // Function to check if N can be expressed3 // Function to check if N can be expressed4

// C++ program for the above approach75// C++ program for the above approach00

// C++ program for the above approach4std;4

 

// C++ program for the above approach4// C++ program for the above approach05

// C++ program for the above approach4// Function to check if N can be expressed2 // C++ program for the above approach08 // Function to check if N can be expressed4

// C++ program for the above approach32std;4

 

_______332____438

// C++ program for the above approach32// C++ program for the above approach29 // C++ program for the above approach37 #include 42 #include 43

 

// C++ program for the above approach4

>>> ranges[[2, 3, 4, 7, 8, 9, 15]]
[[2, 4], [7, 9], [15, 15]]

>>> ranges[[-1, 0, 1, 2, 3, 12, 13, 15, 100]]
[[-1, 3], [12, 13], [15, 15], [100, 100]]

>>> ranges[range[100]]
[[0, 99]]

>>> ranges[[0]]
[[0, 0]]

>>> ranges[[]]
[]
1 #include 46#include 47// Function to check if N can be expressed4

// C++ program for the above approach4

>>> ranges[[2, 3, 4, 7, 8, 9, 15]]
[[2, 4], [7, 9], [15, 15]]

>>> ranges[[-1, 0, 1, 2, 3, 12, 13, 15, 100]]
[[-1, 3], [12, 13], [15, 15], [100, 100]]

>>> ranges[range[100]]
[[0, 99]]

>>> ranges[[0]]
[[0, 0]]

>>> ranges[[]]
[]
1 #include 51____452// Function to check if N can be expressed4

 

_______34____455

// C++ program for the above approach32std;4

std;4

 

#include 59

Python3




#include 60

 

#include 61

#include 62

#include 63 #include 64

 

// C++ program for the above approach4#include 66

// C++ program for the above approach4#include 68#include 69

>>> ranges[[2, 3, 4, 7, 8, 9, 15]]
[[2, 4], [7, 9], [15, 15]]

>>> ranges[[-1, 0, 1, 2, 3, 12, 13, 15, 100]]
[[-1, 3], [12, 13], [15, 15], [100, 100]]

>>> ranges[range[100]]
[[0, 99]]

>>> ranges[[0]]
[[0, 0]]

>>> ranges[[]]
[]
1____471#include 72#include 72namespace3// C++ program for the above approach62 #include 76 #include 77

 

// C++ program for the above approach4#include 79

// C++ program for the above approach4#include 81

// C++ program for the above approach4#include 83____469 // C++ program for the above approach62

// C++ program for the above approach4

_______34____488

// C++ program for the above approach4namespace2 #include 91#include 92 #include 93namespace3// C++ program for the above approach62#include 96#include 97 // C++ program for the above approach62#include 99

// C++ program for the above approach75

namespace8using02

namespace8#include 83#include 69 #include 83#include 72using50#include 97 using52using53// C++ program for the above approach62#include 5

Chủ Đề