Viết chương trình Python để tìm các số chia hết cho 7 và bội số của 5, trong khoảng từ 1500 đến 2700 [bao gồm cả hai]
Trình bày bằng hình ảnh
Giải pháp mẫu
Mã Python
nl=[]
for x in range[1500, 2701]:
if [x%7==0] and [x%5==0]:
nl.append[str[x]]
print [','.join[nl]]
Đầu ra mẫu
1505,1540,1575,1610,1645,1680,1715,1750,1785,1820,1855,1890,1925,1960,1995,2030,2065,2100,2135,2170,2205,2240, 2275,2310,2345,2380,2415,2450,2485,2520,2555,2590,2625,2660,2695
Sơ đồ
Trực quan hóa việc thực thi mã Python
Công cụ sau đây trực quan hóa những gì máy tính đang làm từng bước khi nó thực thi chương trình nói trên
Trình chỉnh sửa mã Python
Có một cách khác để giải quyết giải pháp này?
Trước. Bài tập câu lệnh điều kiện và vòng lặp trong Python Trang chủ
Kế tiếp. Viết chương trình Python để chuyển đổi nhiệt độ sang và từ độ C, độ F
Mức độ khó của bài tập này là gì?
Dễ dàng trung bình khóKiểm tra kỹ năng Lập trình của bạn với bài kiểm tra của w3resource
Theo dõi chúng tôi trên Facebook và Twitter để cập nhật thông tin mới nhất.
con trăn. Lời khuyên trong ngày
lớp liệt kê
Đây là một cách tiếp cận sang trọng khác từ Python, không có ý định chơi chữ. Lớp enum của Python sẽ cho phép bạn tạo các thành viên đối tượng enum với các giá trị không đổi và duy nhất mà sau đó có thể được liệt kê, so sánh hoặc sử dụng để nhận dạng
Tiếp cận. Ví dụ: giả sử N = 20 làm giới hạn, thì chương trình sẽ in tất cả các số nhỏ hơn 20 chia hết cho cả 3 và 5. Đối với điều này, hãy chia mỗi số từ 0 đến N cho cả 3 và 5 và kiểm tra phần còn lại của chúng. Nếu phần còn lại là 0 trong cả hai trường hợp thì chỉ cần in số đó
Dưới đây là việc thực hiện.
C++
// C++ program to print all the numbers
// divisible by 3 and 5 for a given number
#include
using
namespace
std;
// Result function with N
0 15 30 450
0 15 30 451
0 15 30 452
0 15 30 453
0 15 30 454
0 15 30 455_______2_______6
0 15 30 455_______2_______8
0 15 30 459
0 15 30 452
0 15 30 451
0 15 30 455_______2_______4
0 15 30 454_______2_______5
0 15 30 454_______2_______7
0 15 30 458
0 15 30 459
// C++ program to print all the numbers
0// C++ program to print all the numbers
1// C++ program to print all the numbers
20 15 30 455_______338_______4
// C++ program to print all the numbers
4
// C++ program to print all the numbers
6
0 15 30 452
// C++ program to print all the numbers
80 15 30 454
0 15 30 455
// divisible by 3 and 5 for a given number
10 15 30 455_______2_______2
// divisible by 3 and 5 for a given number
40 15 30 455
0 15 30 455_______339_______7
0 15 30 455_______339_______9
0 15 30 455_______340_______1
#include
2// C++ program to print all the numbers
4
#include
4
#include
5
Java
#include
6
// divisible by 3 and 5 for a given number
#include
8 #include
9
0 15 30 455
0 15 30 455
// Result function with N
0 15 30 455_______341_______4
0 15 30 450
0 15 30 451
0 15 30 452
0 15 30 453
0 15 30 455_______2_______4
0 15 30 454_______2_______6
0 15 30 454_______2_______8
0 15 30 459
0 15 30 452
namespace
7namespace
8namespace
90 15 30 454_______2_______4
0 15 30 459
0 15 30 455
0 15 30 459
0 15 30 457
std;
6std;
7 std;
8namespace
8 // Result function with N
0// Result function with N
1 std;
8namespace
8// Result function with N
4// Result function with N
5_______344_______6// C++ program to print all the numbers
1// Result function with N
8
0 15 30 454_______338_______4
0 15 30 455_______338_______4
0 15 30 4503
0 15 30 455
// C++ program to print all the numbers
60 15 30 455_______2_______07
using
4 0 15 30 450
0 15 30 4510
0 15 30 455_______2_______12
0 15 30 454_______339_______1
0 15 30 454_______2_______2
0 15 30 4517_______2_______18_______338_______2
0 15 30 4520
0 15 30 454_______339_______7
0 15 30 454_______339_______9
0 15 30 455_______338_______4
// C++ program to print all the numbers
4
Python3
0 15 30 4528
0 15 30 4529
0 15 30 4530
0 15 30 4531
0 15 30 4532
0 15 30 455
0 15 30 455_______2_______35
0 15 30 455_______2_______8
0 15 30 4538
0 15 30 4539
0 15 30 4540
0 15 30 4541
0 15 30 454
0 15 30 459
0 15 30 4544
0 15 30 459
0 15 30 457
0 15 30 4538_______2_______48
std;
7 0 15 30 4550
0 15 30 4550
namespace
8 0 15 30 4553
0 15 30 4538
0 15 30 4548
// Result function with N
1 0 15 30 4550
0 15 30 4550
namespace
80 15 30 4560
// Result function with N
5_______2_______62
0 15 30 459
0 15 30 4564
0 15 30 4565
0 15 30 4566
// C++ program to print all the numbers
10 15 30 4568
0 15 30 4550
0 15 30 4570
// Result function with N
5
0 15 30 459
0 15 30 4573
0 15 30 4560
// Result function with N
5____2_______76
0 15 30 4577
0 15 30 457
0 15 30 4579
0 15 30 4550
0 15 30 4550
0 15 30 4582
0 15 30 4560
0 15 30 455
0 15 30 455_______2_______86
0 15 30 455_______2_______88_______2_______50
0 15 30 4518
0 15 30 455
0 15 30 455_______2_______93
0 15 30 455_______2_______95
C#
0 15 30 4596
// divisible by 3 and 5 for a given number
using
0 15 30 4599
0 15 30 4507
#include
8 #include
90 15 30 455
0 15 30 455
// Result function with N
0 15 30 455_______341_______4
0 15 30 450
0 15 30 451
0 15 30 452
0 15 30 453
0 15 30 455_______2_______4
0 15 30 454_______2_______6
0 15 30 454_______2_______8
0 15 30 459
0 15 30 452
0 15 30 451
0 15 30 454_______2_______4
0 15 30 459
0 15 30 455
0 15 30 459
0 15 30 457
0 15 30 458
// Result function with N
5_______2_______29// C++ program to print all the numbers
1// Result function with N
8
0 15 30 454_______338_______4
0 15 30 455_______338_______4
0 15 30 455
0 15 30 455
// C++ program to print all the numbers
60 15 30 455_______341_______4
0 15 30 4507
0 15 30 450
0 15 30 4543
0 15 30 454_______339_______1
0 15 30 454_______2_______2
// divisible by 3 and 5 for a given number
40 15 30 454_______339_______7
0 15 30 454_______339_______9
0 15 30 455_______338_______4
0 15 30 4555
// C++ program to print all the numbers
4
PHP
0 15 30 4557
0 15 30 4558
// divisible by 3 and 5 for a given number
// Result function with N
0 15 30 4561
0 15 30 451
0 15 30 4563
// Result function with N
40 15 30 4512
0 15 30 455_______2_______6
0 15 30 455_______2_______8
0 15 30 459
0 15 30 4571
0 15 30 4572
0 15 30 4571
0 15 30 4574
0 15 30 4563
// C++ program to print all the numbers
20 15 30 4571
0 15 30 4578
0 15 30 455_______2_______12
0 15 30 454_______2_______5
0 15 30 454_______2_______7
0 15 30 459
0 15 30 4571
0 15 30 4587
0 15 30 4571
0 15 30 4589
0 15 30 459
0 15 30 4591
0 15 30 4571
0 15 30 4593
// C++ program to print all the numbers
1// C++ program to print all the numbers
20 15 30 455_______338_______4
// C++ program to print all the numbers
4
// C++ program to print all the numbers
6
0 15 30 455
// divisible by 3 and 5 for a given number
1
0 15 30 4563
// C++ program to print all the numbers
03
// divisible by 3 and 5 for a given number
7
0 15 30 451
0 15 30 4563
// Result function with N
8
// C++ program to print all the numbers
08
// C++ program to print all the numbers
09
Javascript
// C++ program to print all the numbers
10
// C++ program to print all the numbers
11
// C++ program to print all the numbers
12
// C++ program to print all the numbers
13
// C++ program to print all the numbers
14
// Result function with N
0 15 30 4561
0 15 30 4595
0 15 30 4512
0 15 30 455_______2_______6
0 15 30 455_______2_______8
// C++ program to print all the numbers
230 15 30 455_______2_______12
0 15 30 454_______2_______5
0 15 30 454_______2_______7
0 15 30 458
0 15 30 459
// C++ program to print all the numbers
32// C++ program to print all the numbers
1// Result function with N
80 15 30 455_______338_______4
// C++ program to print all the numbers
4
// C++ program to print all the numbers
6
0 15 30 455
// divisible by 3 and 5 for a given number
1
// C++ program to print all the numbers
41
// divisible by 3 and 5 for a given number
7
// divisible by 3 and 5 for a given number
9
// C++ program to print all the numbers
44
// C++ program to print all the numbers
45
Đầu ra
0 15 30 45 60 75 90
Thời gian phức tạp. TRÊN]
Không gian phụ trợ. Ô[1]
Phương pháp. Điều này cũng có thể được thực hiện bằng cách kiểm tra xem số đó có chia hết cho 15 hay không, vì BCNN của 3 và 5 là 15 và bất kỳ số nào chia hết cho 15 cũng chia hết cho 3 và 5 và ngược lại.
C++
// C++ program to print all the numbers
46
// C++ program to print all the numbers
47
#include
using
namespace
std;
0 15 30 452
// C++ program to print all the numbers
80 15 30 4512
// C++ program to print all the numbers
55______2_______2 // C++ program to print all the numbers
57
// C++ program to print all the numbers
55_______2_______8
0 15 30 459
0 15 30 452
// C++ program to print all the numbers
62// C++ program to print all the numbers
55______2_______12
0 15 30 455_______338_______66
0 15 30 455_______2_______7
// C++ program to print all the numbers
69// C++ program to print all the numbers
70______338_______71______338_______1// C++ program to print all the numbers
2
0 15 30 455_______338_______4
// C++ program to print all the numbers
55_______338_______4
// C++ program to print all the numbers
55_______340_______1 #include
2
// C++ program to print all the numbers
4
// C++ program to print all the numbers
82
Java
// C++ program to print all the numbers
83
// C++ program to print all the numbers
84 // C++ program to print all the numbers
85
#include
8 // C++ program to print all the numbers
87
0 15 30 455_______2_______07
using
4 0 15 30 450
// C++ program to print all the numbers
92// C++ program to print all the numbers
70
// C++ program to print all the numbers
94_______2_______2 // C++ program to print all the numbers
96// C++ program to print all the numbers
97// C++ program to print all the numbers
2
0 15 30 454_______2_______8
0 15 30 459
0 15 30 452
// divisible by 3 and 5 for a given number
03namespace
8// divisible by 3 and 5 for a given number
05// C++ program to print all the numbers
97// divisible by 3 and 5 for a given number
07
0 15 30 459
// divisible by 3 and 5 for a given number
090 15 30 459
0 15 30 457
// divisible by 3 and 5 for a given number
12_______339_______13 std;
8namespace
8// divisible by 3 and 5 for a given number
16// Result function with N
5_______339_______18// C++ program to print all the numbers
1// Result function with N
8
0 15 30 459
// C++ program to print all the numbers
40 15 30 454_______338_______4
// C++ program to print all the numbers
70
0 15 30 455_______338_______4
// C++ program to print all the numbers
4
// C++ program to print all the numbers
82
Python3
// divisible by 3 and 5 for a given number
30
// divisible by 3 and 5 for a given number
31
// divisible by 3 and 5 for a given number
32_______2_______50// C++ program to print all the numbers
97
0 15 30 458
// divisible by 3 and 5 for a given number
360 15 30 4539
0 15 30 4540
0 15 30 459
namespace
8// divisible by 3 and 5 for a given number
41// C++ program to print all the numbers
55_______339_______43
// C++ program to print all the numbers
55_______2_______7 // divisible by 3 and 5 for a given number
36
0 15 30 4548
// divisible by 3 and 5 for a given number
130 15 30 4550
0 15 30 4550
namespace
80 15 30 4560
0 15 30 455_______2_______62
// divisible by 3 and 5 for a given number
550 15 30 4550
// C++ program to print all the numbers
1// Result function with N
4C#
using
0 15 30 4599
0 15 30 4507
#include
8 // C++ program to print all the numbers
87
0 15 30 455_______341_______4
0 15 30 4507
0 15 30 450
// divisible by 3 and 5 for a given number
680 15 30 455_______2_______12
0 15 30 454_______2_______2
// C++ program to print all the numbers
570 15 30 454_______2_______8
0 15 30 459
0 15 30 452
// divisible by 3 and 5 for a given number
78
0 15 30 459
// divisible by 3 and 5 for a given number
090 15 30 459
0 15 30 457
// divisible by 3 and 5 for a given number
83// Result function with N
5_______339_______85// C++ program to print all the numbers
1// Result function with N
8
0 15 30 459
// C++ program to print all the numbers
40 15 30 454_______338_______4
0 15 30 455_______338_______4
// C++ program to print all the numbers
4
// C++ program to print all the numbers
55
// C++ program to print all the numbers
55_______338_______82
Javascript
// C++ program to print all the numbers
10
// divisible by 3 and 5 for a given number
99
// C++ program to print all the numbers
55_______2_______8#include
02
// C++ program to print all the numbers
55______2_______12
0 15 30 455_______338_______66
0 15 30 455_______2_______7
// C++ program to print all the numbers
69// C++ program to print all the numbers
70_______340_______11____338_______1// Result function with N
8
0 15 30 455_______338_______4
// C++ program to print all the numbers
55_______338_______4
// C++ program to print all the numbers
45
Đầu ra
0 15 30 45
Thời gian phức tạp. Trên]
Không gian phụ trợ. Ô[1]
Phương pháp 3. chúng tôi nhận thấy rằng LCM của 3 & 5 là 15, vì vậy chúng tôi không cần lặp lại toàn bộ vòng lặp từ 0 đến n nhưng chúng tôi cần lặp lại từ 0 và mỗi lần tăng i lên 15 để theo cách này chúng tôi có thể giảm độ phức tạp của thời gian khi chúng tôi
C++
#include
19
#include
using
namespace
std;
0 15 30 452
// C++ program to print all the numbers
80 15 30 4512
0 15 30 455_______2_______2
// C++ program to print all the numbers
570 15 30 455_______339_______09
0 15 30 455_______340_______33
0 15 30 455_______340_______35
0 15 30 455_______2_______8
0 15 30 459
0 15 30 452
#include
400 15 30 454_______338_______71____338_______1
// C++ program to print all the numbers
20 15 30 455_______338_______4
0 15 30 455_______340_______1
#include
2// C++ program to print all the numbers
4
Java
#include
51
#include
8 // C++ program to print all the numbers
87
// C++ program to print all the numbers
55_______2_______07 using
4
0 15 30 450
#include
580 15 30 455_______2_______2
// C++ program to print all the numbers
96// C++ program to print all the numbers
97// C++ program to print all the numbers
2
0 15 30 455_______339_______09
0 15 30 455_______340_______33
0 15 30 455_______340_______35
0 15 30 455_______2_______8
0 15 30 459
0 15 30 452
// divisible by 3 and 5 for a given number
03namespace
8#include
76// divisible by 3 and 5 for a given number
13// divisible by 3 and 5 for a given number
16// C++ program to print all the numbers
70______340_______80// C++ program to print all the numbers
1// Result function with N
8
0 15 30 455_______338_______4
// C++ program to print all the numbers
55_______338_______4
// C++ program to print all the numbers
4
#include
88
C#
#include
89
using
0 15 30 4599
0 15 30 4507
#include
8 // C++ program to print all the numbers
870 15 30 455_______2_______07
using
4 0 15 30 450
#include
99using
00using
010 15 30 454_______2_______2
// C++ program to print all the numbers
57
0 15 30 454_______339_______09
0 15 30 454_______340_______33
0 15 30 454_______340_______35
0 15 30 454_______2_______8
0 15 30 459
0 15 30 452
#include
400 15 30 459
using
17_______338_______1// Result function with N
80 15 30 454_______338_______4
0 15 30 455_______338_______4
// C++ program to print all the numbers
4
using
25
Đầu ra
0 15 30 45
Thời gian phức tạp. O[n/15] ~= O[n] [tốt hơn nhiều so với cả hai phương pháp trên vì chúng ta chỉ cần lặp lại i trong n/15 lần]