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 approach
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 approach
2// C++ program for the above approach
3
// C++ program for the above approach
4// C++ program for the above approach
5 // C++ program for the above approach
6 // C++ program for the above approach
7
// C++ program for the above approach
4
// C++ program for the above approach
4#include
0
// C++ program for the above approach
4
>>> 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 approach
6#include
9// C++ program for the above approach
4
// C++ program for the above approach
4using
2
// C++ program for the above approach
4using
4
// C++ program for the above approach
4
>>> 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 using
7// C++ program for the above approach
4
// C++ program for the above approach
4namespace
0
// C++ program for the above approach
4namespace
2____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 namespace
5// C++ program for the above approach
4// C++ program for the above approach
3
namespace
8
namespace
8std;
0
namespace
8std;
2
// C++ program for the above approach
4std;
4
// C++ program for the above approach
4
// C++ program for the above approach
4std;
7
// C++ program for the above approach
4std;
9 // Function to check if N can be expressed
0
namespace
8// Function to check if N can be expressed
2 // Function to check if N can be expressed
3// Function to check if N can be expressed
4
// C++ program for the above approach
4
// C++ program for the above approach
4// Function to check if N can be expressed
7
// C++ program for the above approach
4// C++ program for the above approach
3
namespace
8
namespace
8// as the product of K consecutive integers
2
namespace
8// as the product of K consecutive integers
4
namespace
8namespace
2____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 integers
9namespace
8// C++ program for the above approach
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[[]]
[]
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 expressed
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[[]]
[]
15// Function to check if N can be expressed
2 // Function to check if N can be expressed
3// Function to check if N can be expressed
4namespace
8// C++ program for the above approach
00
// C++ program for the above approach
4std;
4
// C++ program for the above approach
4
// C++ program for the above approach
4// C++ program for the above approach
05
// C++ program for the above approach
4// Function to check if N can be expressed
2 // C++ program for the above approach
08// Function to check if N can be expressed
4
std;
4
// C++ program for the above approach
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[[]]
[]
1 // C++ program for the above approach
13// C++ program for the above approach
3
// C++ program for the above approach
4
>>> 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 approach
17// C++ program for the above approach
4
>>> 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 approach
20
// C++ program for the above approach
4____322
// C++ program for the above approach
4// Function to check if N can be expressed
2 // C++ program for the above approach
25
std;
4
// C++ program for the above approach
27
Java
// C++ program for the above approach
28
// C++ program for the above approach
29 // C++ program for the above approach
30 // C++ program for the above approach
31
// C++ program for the above approach
32// Function to check if N can be expressed
// C++ program for the above approach
32// as the product of K consecutive integers
// C++ program for the above approach
32// C++ program for the above approach
37 // C++ program for the above approach
38
>>> 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 approach
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 approach
42
// C++ program for the above approach
4// C++ program for the above approach
5 // C++ program for the above approach
45______346 // C++ program for the above approach
47
// C++ program for the above approach
4#include
0
// C++ program for the above approach
4
>>> 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 approach
54
// C++ program for the above approach
4using
2
// C++ program for the above approach
4using
4
// C++ program for the above approach
4
>>> 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 approach
61______362 // Function to check if N can be expressed
4
// C++ program for the above approach
4namespace
0
// C++ program for the above approach
4namespace
2 namespace
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 // C++ program for the above approach
70// C++ program for the above approach
62// C++ program for the above approach
72// C++ program for the above approach
62// C++ program for the above approach
74// C++ program for the above approach
75std;
0
// C++ program for the above approach
75std;
2
// C++ program for the above approach
4std;
4
// C++ program for the above approach
4std;
7
// C++ program for the above approach
4std;
9____80
// C++ program for the above approach
75// Function to check if N can be expressed
2 // Function to check if N can be expressed
3// Function to check if N can be expressed
4
// C++ program for the above approach
4// Function to check if N can be expressed
7 // C++ program for the above approach
3
// C++ program for the above approach
75// as the product of K consecutive integers
2
// C++ program for the above approach
75// as the product of K consecutive integers
4
// C++ program for the above approach
75namespace
2 namespace
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
01#include
02#include
03// C++ program for the above approach
62#include
05
namespace
8
>>> 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
namespace
8#include
13____362#include
11
namespace
8
>>> 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[[]]
[]
11namespace
8std;
9// Function to check if N can be expressed
0
#include
21// Function to check if N can be expressed
2 // Function to check if N can be expressed
3 // Function to check if N can be expressed
4
// C++ program for the above approach
75// C++ program for the above approach
00
// C++ program for the above approach
4std;
4
// C++ program for the above approach
4// C++ program for the above approach
05
// C++ program for the above approach
4// Function to check if N can be expressed
2 // C++ program for the above approach
08 // Function to check if N can be expressed
4
// C++ program for the above approach
32std;
4
_______332____438
// C++ program for the above approach
32// C++ program for the above approach
29 // C++ program for the above approach
37 #include
42 #include
43
// C++ program for the above approach
4
>>> 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 expressed
4// C++ program for the above approach
4
>>> 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 expressed
4
_______34____455
// C++ program for the above approach
32std;
4
std;
4
#include
59
Python3
#include
60
#include
61
#include
62
#include
63 #include
64
// C++ program for the above approach
4#include
66
// C++ program for the above approach
4#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
72namespace
3// C++ program for the above approach
62 #include
76 #include
77
// C++ program for the above approach
4#include
79
// C++ program for the above approach
4#include
81
// C++ program for the above approach
4#include
83____469 // C++ program for the above approach
62
// C++ program for the above approach
4
_______34____488
// C++ program for the above approach
4namespace
2 #include
91#include
92 #include
93namespace
3// C++ program for the above approach
62#include
96#include
97 // C++ program for the above approach
62#include
99
// C++ program for the above approach
75
namespace
8using
02
namespace
8#include
83#include
69 #include
83#include
72using
50#include
97 using
52using
53// C++ program for the above approach
62#include
5