Viết chương trình tìm số chia hết cho 5 trong python

Cho một số, nhiệm vụ là kiểm tra xem số đó có chia hết cho 5 không. Số đầu vào có thể lớn và không thể lưu trữ ngay cả khi chúng ta sử dụng long long int

ví dụ.  

Input  : n = 56945255
Output : Yes

Input  : n = 1234567589333150
Output : Yes

Input  : n = 3635883959606670431112222
Output : No

Đề nghị thực hành

Kiểm tra xem có chia hết cho 5 không

Thử nó

Vì số đầu vào có thể rất lớn, chúng tôi không thể sử dụng n % 5 để kiểm tra xem một số có chia hết cho 5 hay không, đặc biệt là trong các ngôn ngữ như C/C++. Ý tưởng dựa trên thực tế sau đây.  

   A number is divisible by 5 if its  
   digits last digit will be 0 or 5 .

Hình minh họa.   

For example, let us consider 769555 
Number formed by last digit is = 5
Since 5 is divisible by 5 , answer is YES.

Cái này hoạt động ra sao?

Let us consider 5335, we can write it as
5335 = 5*1000 + 3*100 + 3*10 + 5

The proof is based on below observation:
Remainder of 10i divided by 5 is 0 if i greater 
than or equal to one. Note than 10, 100, 1000,
.. etc lead to remainder 0 when divided by 5.

So remainder of " 5*1000 + 3*100 + 
3*10 + 5" divided by 5 is equivalent to remainder 
of following : 
0 + 0 + 0 + 5 = 5

Since 5 is divisible by 5, answer is yes.

Dưới đây là việc thực hiện ý tưởng trên

C++




   A number is divisible by 5 if its  
   digits last digit will be 0 or 5 .
05

   A number is divisible by 5 if its  
   digits last digit will be 0 or 5 .
06

   A number is divisible by 5 if its  
   digits last digit will be 0 or 5 .
07

   A number is divisible by 5 if its  
   digits last digit will be 0 or 5 .
08
   A number is divisible by 5 if its  
   digits last digit will be 0 or 5 .
09
   A number is divisible by 5 if its  
   digits last digit will be 0 or 5 .
0

 

   A number is divisible by 5 if its  
   digits last digit will be 0 or 5 .
1

   A number is divisible by 5 if its  
   digits last digit will be 0 or 5 .
2

   A number is divisible by 5 if its  
   digits last digit will be 0 or 5 .
3

   A number is divisible by 5 if its  
   digits last digit will be 0 or 5 .
4
   A number is divisible by 5 if its  
   digits last digit will be 0 or 5 .
5

   A number is divisible by 5 if its  
   digits last digit will be 0 or 5 .
6

   A number is divisible by 5 if its  
   digits last digit will be 0 or 5 .
7____18
   A number is divisible by 5 if its  
   digits last digit will be 0 or 5 .
9

 

   A number is divisible by 5 if its  
   digits last digit will be 0 or 5 .
7
For example, let us consider 769555 
Number formed by last digit is = 5
Since 5 is divisible by 5 , answer is YES.
1
For example, let us consider 769555 
Number formed by last digit is = 5
Since 5 is divisible by 5 , answer is YES.
2
For example, let us consider 769555 
Number formed by last digit is = 5
Since 5 is divisible by 5 , answer is YES.
3
For example, let us consider 769555 
Number formed by last digit is = 5
Since 5 is divisible by 5 , answer is YES.
4

For example, let us consider 769555 
Number formed by last digit is = 5
Since 5 is divisible by 5 , answer is YES.
5
For example, let us consider 769555 
Number formed by last digit is = 5
Since 5 is divisible by 5 , answer is YES.
6
For example, let us consider 769555 
Number formed by last digit is = 5
Since 5 is divisible by 5 , answer is YES.
3
For example, let us consider 769555 
Number formed by last digit is = 5
Since 5 is divisible by 5 , answer is YES.
8

For example, let us consider 769555 
Number formed by last digit is = 5
Since 5 is divisible by 5 , answer is YES.
9

 

Let us consider 5335, we can write it as
5335 = 5*1000 + 3*100 + 3*10 + 5

The proof is based on below observation:
Remainder of 10i divided by 5 is 0 if i greater 
than or equal to one. Note than 10, 100, 1000,
.. etc lead to remainder 0 when divided by 5.

So remainder of " 5*1000 + 3*100 + 
3*10 + 5" divided by 5 is equivalent to remainder 
of following : 
0 + 0 + 0 + 5 = 5

Since 5 is divisible by 5, answer is yes.
0

   A number is divisible by 5 if its  
   digits last digit will be 0 or 5 .
8
Let us consider 5335, we can write it as
5335 = 5*1000 + 3*100 + 3*10 + 5

The proof is based on below observation:
Remainder of 10i divided by 5 is 0 if i greater 
than or equal to one. Note than 10, 100, 1000,
.. etc lead to remainder 0 when divided by 5.

So remainder of " 5*1000 + 3*100 + 
3*10 + 5" divided by 5 is equivalent to remainder 
of following : 
0 + 0 + 0 + 5 = 5

Since 5 is divisible by 5, answer is yes.
2

   A number is divisible by 5 if its  
   digits last digit will be 0 or 5 .
6

   A number is divisible by 5 if its  
   digits last digit will be 0 or 5 .
7__
Let us consider 5335, we can write it as
5335 = 5*1000 + 3*100 + 3*10 + 5

The proof is based on below observation:
Remainder of 10i divided by 5 is 0 if i greater 
than or equal to one. Note than 10, 100, 1000,
.. etc lead to remainder 0 when divided by 5.

So remainder of " 5*1000 + 3*100 + 
3*10 + 5" divided by 5 is equivalent to remainder 
of following : 
0 + 0 + 0 + 5 = 5

Since 5 is divisible by 5, answer is yes.
5
Let us consider 5335, we can write it as
5335 = 5*1000 + 3*100 + 3*10 + 5

The proof is based on below observation:
Remainder of 10i divided by 5 is 0 if i greater 
than or equal to one. Note than 10, 100, 1000,
.. etc lead to remainder 0 when divided by 5.

So remainder of " 5*1000 + 3*100 + 
3*10 + 5" divided by 5 is equivalent to remainder 
of following : 
0 + 0 + 0 + 5 = 5

Since 5 is divisible by 5, answer is yes.
6
Let us consider 5335, we can write it as
5335 = 5*1000 + 3*100 + 3*10 + 5

The proof is based on below observation:
Remainder of 10i divided by 5 is 0 if i greater 
than or equal to one. Note than 10, 100, 1000,
.. etc lead to remainder 0 when divided by 5.

So remainder of " 5*1000 + 3*100 + 
3*10 + 5" divided by 5 is equivalent to remainder 
of following : 
0 + 0 + 0 + 5 = 5

Since 5 is divisible by 5, answer is yes.
7

   A number is divisible by 5 if its  
   digits last digit will be 0 or 5 .
7__
Let us consider 5335, we can write it as
5335 = 5*1000 + 3*100 + 3*10 + 5

The proof is based on below observation:
Remainder of 10i divided by 5 is 0 if i greater 
than or equal to one. Note than 10, 100, 1000,
.. etc lead to remainder 0 when divided by 5.

So remainder of " 5*1000 + 3*100 + 
3*10 + 5" divided by 5 is equivalent to remainder 
of following : 
0 + 0 + 0 + 5 = 5

Since 5 is divisible by 5, answer is yes.
9
Let us consider 5335, we can write it as
5335 = 5*1000 + 3*100 + 3*10 + 5

The proof is based on below observation:
Remainder of 10i divided by 5 is 0 if i greater 
than or equal to one. Note than 10, 100, 1000,
.. etc lead to remainder 0 when divided by 5.

So remainder of " 5*1000 + 3*100 + 
3*10 + 5" divided by 5 is equivalent to remainder 
of following : 
0 + 0 + 0 + 5 = 5

Since 5 is divisible by 5, answer is yes.
40
Let us consider 5335, we can write it as
5335 = 5*1000 + 3*100 + 3*10 + 5

The proof is based on below observation:
Remainder of 10i divided by 5 is 0 if i greater 
than or equal to one. Note than 10, 100, 1000,
.. etc lead to remainder 0 when divided by 5.

So remainder of " 5*1000 + 3*100 + 
3*10 + 5" divided by 5 is equivalent to remainder 
of following : 
0 + 0 + 0 + 5 = 5

Since 5 is divisible by 5, answer is yes.
41

Let us consider 5335, we can write it as
5335 = 5*1000 + 3*100 + 3*10 + 5

The proof is based on below observation:
Remainder of 10i divided by 5 is 0 if i greater 
than or equal to one. Note than 10, 100, 1000,
.. etc lead to remainder 0 when divided by 5.

So remainder of " 5*1000 + 3*100 + 
3*10 + 5" divided by 5 is equivalent to remainder 
of following : 
0 + 0 + 0 + 5 = 5

Since 5 is divisible by 5, answer is yes.
42
Let us consider 5335, we can write it as
5335 = 5*1000 + 3*100 + 3*10 + 5

The proof is based on below observation:
Remainder of 10i divided by 5 is 0 if i greater 
than or equal to one. Note than 10, 100, 1000,
.. etc lead to remainder 0 when divided by 5.

So remainder of " 5*1000 + 3*100 + 
3*10 + 5" divided by 5 is equivalent to remainder 
of following : 
0 + 0 + 0 + 5 = 5

Since 5 is divisible by 5, answer is yes.
43
Let us consider 5335, we can write it as
5335 = 5*1000 + 3*100 + 3*10 + 5

The proof is based on below observation:
Remainder of 10i divided by 5 is 0 if i greater 
than or equal to one. Note than 10, 100, 1000,
.. etc lead to remainder 0 when divided by 5.

So remainder of " 5*1000 + 3*100 + 
3*10 + 5" divided by 5 is equivalent to remainder 
of following : 
0 + 0 + 0 + 5 = 5

Since 5 is divisible by 5, answer is yes.
44
Let us consider 5335, we can write it as
5335 = 5*1000 + 3*100 + 3*10 + 5

The proof is based on below observation:
Remainder of 10i divided by 5 is 0 if i greater 
than or equal to one. Note than 10, 100, 1000,
.. etc lead to remainder 0 when divided by 5.

So remainder of " 5*1000 + 3*100 + 
3*10 + 5" divided by 5 is equivalent to remainder 
of following : 
0 + 0 + 0 + 5 = 5

Since 5 is divisible by 5, answer is yes.
7

   A number is divisible by 5 if its  
   digits last digit will be 0 or 5 .
7____21
Let us consider 5335, we can write it as
5335 = 5*1000 + 3*100 + 3*10 + 5

The proof is based on below observation:
Remainder of 10i divided by 5 is 0 if i greater 
than or equal to one. Note than 10, 100, 1000,
.. etc lead to remainder 0 when divided by 5.

So remainder of " 5*1000 + 3*100 + 
3*10 + 5" divided by 5 is equivalent to remainder 
of following : 
0 + 0 + 0 + 5 = 5

Since 5 is divisible by 5, answer is yes.
48

For example, let us consider 769555 
Number formed by last digit is = 5
Since 5 is divisible by 5 , answer is YES.
9

Java




   A number is divisible by 5 if its  
   digits last digit will be 0 or 5 .
050

   A number is divisible by 5 if its  
   digits last digit will be 0 or 5 .
06

   A number is divisible by 5 if its  
   digits last digit will be 0 or 5 .
052
   A number is divisible by 5 if its  
   digits last digit will be 0 or 5 .
053

   A number is divisible by 5 if its  
   digits last digit will be 0 or 5 .
6

   A number is divisible by 5 if its  
   digits last digit will be 0 or 5 .
7____11

   A number is divisible by 5 if its  
   digits last digit will be 0 or 5 .
7____12

   A number is divisible by 5 if its  
   digits last digit will be 0 or 5 .
7____13

________ 17 ________ 1062 ________ 1063 ________ 1064

   A number is divisible by 5 if its  
   digits last digit will be 0 or 5 .
7
   A number is divisible by 5 if its  
   digits last digit will be 0 or 5 .
6

   A number is divisible by 5 if its  
   digits last digit will be 0 or 5 .
067
   A number is divisible by 5 if its  
   digits last digit will be 0 or 5 .
8
   A number is divisible by 5 if its  
   digits last digit will be 0 or 5 .
9

   A number is divisible by 5 if its  
   digits last digit will be 0 or 5 .
070

   A number is divisible by 5 if its  
   digits last digit will be 0 or 5 .
067
For example, let us consider 769555 
Number formed by last digit is = 5
Since 5 is divisible by 5 , answer is YES.
1
   A number is divisible by 5 if its  
   digits last digit will be 0 or 5 .
073
   A number is divisible by 5 if its  
   digits last digit will be 0 or 5 .
074
   A number is divisible by 5 if its  
   digits last digit will be 0 or 5 .
075
For example, let us consider 769555 
Number formed by last digit is = 5
Since 5 is divisible by 5 , answer is YES.
3
   A number is divisible by 5 if its  
   digits last digit will be 0 or 5 .
077
   A number is divisible by 5 if its  
   digits last digit will be 0 or 5 .
078
   A number is divisible by 5 if its  
   digits last digit will be 0 or 5 .
079

   A number is divisible by 5 if its  
   digits last digit will be 0 or 5 .
080
   A number is divisible by 5 if its  
   digits last digit will be 0 or 5 .
081
   A number is divisible by 5 if its  
   digits last digit will be 0 or 5 .
074
   A number is divisible by 5 if its  
   digits last digit will be 0 or 5 .
075
For example, let us consider 769555 
Number formed by last digit is = 5
Since 5 is divisible by 5 , answer is YES.
3
   A number is divisible by 5 if its  
   digits last digit will be 0 or 5 .
077
   A number is divisible by 5 if its  
   digits last digit will be 0 or 5 .
086
   A number is divisible by 5 if its  
   digits last digit will be 0 or 5 .
087

   A number is divisible by 5 if its  
   digits last digit will be 0 or 5 .
7
For example, let us consider 769555 
Number formed by last digit is = 5
Since 5 is divisible by 5 , answer is YES.
9

   A number is divisible by 5 if its  
   digits last digit will be 0 or 5 .
7

   A number is divisible by 5 if its  
   digits last digit will be 0 or 5 .
7____1092

   A number is divisible by 5 if its  
   digits last digit will be 0 or 5 .
7____1094
   A number is divisible by 5 if its  
   digits last digit will be 0 or 5 .
062
   A number is divisible by 5 if its  
   digits last digit will be 0 or 5 .
096
   A number is divisible by 5 if its  
   digits last digit will be 0 or 5 .
097

   A number is divisible by 5 if its  
   digits last digit will be 0 or 5 .
7
   A number is divisible by 5 if its  
   digits last digit will be 0 or 5 .
6

   A number is divisible by 5 if its  
   digits last digit will be 0 or 5 .
067
   A number is divisible by 5 if its  
   digits last digit will be 0 or 5 .
01____36
Let us consider 5335, we can write it as
5335 = 5*1000 + 3*100 + 3*10 + 5

The proof is based on below observation:
Remainder of 10i divided by 5 is 0 if i greater 
than or equal to one. Note than 10, 100, 1000,
.. etc lead to remainder 0 when divided by 5.

So remainder of " 5*1000 + 3*100 + 
3*10 + 5" divided by 5 is equivalent to remainder 
of following : 
0 + 0 + 0 + 5 = 5

Since 5 is divisible by 5, answer is yes.
7

________ 1067 ________ 105 ________ 106

   A number is divisible by 5 if its  
   digits last digit will be 0 or 5 .
07
   A number is divisible by 5 if its  
   digits last digit will be 0 or 5 .
08
Let us consider 5335, we can write it as
5335 = 5*1000 + 3*100 + 3*10 + 5

The proof is based on below observation:
Remainder of 10i divided by 5 is 0 if i greater 
than or equal to one. Note than 10, 100, 1000,
.. etc lead to remainder 0 when divided by 5.

So remainder of " 5*1000 + 3*100 + 
3*10 + 5" divided by 5 is equivalent to remainder 
of following : 
0 + 0 + 0 + 5 = 5

Since 5 is divisible by 5, answer is yes.
40
   A number is divisible by 5 if its  
   digits last digit will be 0 or 5 .
10

   A number is divisible by 5 if its  
   digits last digit will be 0 or 5 .
067
   A number is divisible by 5 if its  
   digits last digit will be 0 or 5 .
12

   A number is divisible by 5 if its  
   digits last digit will be 0 or 5 .
07
   A number is divisible by 5 if its  
   digits last digit will be 0 or 5 .
08
   A number is divisible by 5 if its  
   digits last digit will be 0 or 5 .
15
   A number is divisible by 5 if its  
   digits last digit will be 0 or 5 .
10

   A number is divisible by 5 if its  
   digits last digit will be 0 or 5 .
7
For example, let us consider 769555 
Number formed by last digit is = 5
Since 5 is divisible by 5 , answer is YES.
9

For example, let us consider 769555 
Number formed by last digit is = 5
Since 5 is divisible by 5 , answer is YES.
9

Python3




   A number is divisible by 5 if its  
   digits last digit will be 0 or 5 .
20

   A number is divisible by 5 if its  
   digits last digit will be 0 or 5 .
21

 

   A number is divisible by 5 if its  
   digits last digit will be 0 or 5 .
22

   A number is divisible by 5 if its  
   digits last digit will be 0 or 5 .
23

   A number is divisible by 5 if its  
   digits last digit will be 0 or 5 .
24

   A number is divisible by 5 if its  
   digits last digit will be 0 or 5 .
25
   A number is divisible by 5 if its  
   digits last digit will be 0 or 5 .
26

   A number is divisible by 5 if its  
   digits last digit will be 0 or 5 .
7____128
   A number is divisible by 5 if its  
   digits last digit will be 0 or 5 .
29
   A number is divisible by 5 if its  
   digits last digit will be 0 or 5 .
30
   A number is divisible by 5 if its  
   digits last digit will be 0 or 5 .
31

   A number is divisible by 5 if its  
   digits last digit will be 0 or 5 .
7__
For example, let us consider 769555 
Number formed by last digit is = 5
Since 5 is divisible by 5 , answer is YES.
1
   A number is divisible by 5 if its  
   digits last digit will be 0 or 5 .
34
   A number is divisible by 5 if its  
   digits last digit will be 0 or 5 .
35
   A number is divisible by 5 if its  
   digits last digit will be 0 or 5 .
074
   A number is divisible by 5 if its  
   digits last digit will be 0 or 5 .
37
   A number is divisible by 5 if its  
   digits last digit will be 0 or 5 .
29
   A number is divisible by 5 if its  
   digits last digit will be 0 or 5 .
29
For example, let us consider 769555 
Number formed by last digit is = 5
Since 5 is divisible by 5 , answer is YES.
3
   A number is divisible by 5 if its  
   digits last digit will be 0 or 5 .
41
   A number is divisible by 5 if its  
   digits last digit will be 0 or 5 .
42

Chủ Đề