Đếm số 0 trong chuỗi Python

Từ "khác không" có liên quan đến Python 2. phương thức tích hợp x __nonzero__[] [được đổi tên thành __bool__[] trong Python 3. x] của các đối tượng Python kiểm tra “tính trung thực” của một đối tượng. Ví dụ: bất kỳ số nào được coi là đúng nếu nó khác không, trong khi bất kỳ chuỗi nào được coi là đúng nếu nó không phải là chuỗi rỗng. Do đó, hàm này [đệ quy] đếm xem có bao nhiêu phần tử trong a [và trong các mảng con của chúng] có phương thức __nonzero__[] hoặc __bool__[] của chúng được đánh giá là True

Thông số . a mảng_like

Mảng để đếm các số khác không

axis int hoặc tuple, tùy chọn

Trục hoặc bộ trục dọc theo đó để đếm các giá trị khác không. Mặc định là Không, nghĩa là các số khác không sẽ được tính dọc theo phiên bản phẳng của a

Mới trong phiên bản 1. 12. 0

keepdims bool, tùy chọn

Nếu điều này được đặt thành True, thì các trục được đếm sẽ được để lại trong kết quả dưới dạng thứ nguyên có kích thước là một. Với tùy chọn này, kết quả sẽ phát chính xác đối với mảng đầu vào

Mới trong phiên bản 1. 19. 0

Trả về . đếm int hoặc mảng của int

Số lượng giá trị khác 0 trong mảng dọc theo một trục đã cho. Mặt khác, tổng số giá trị khác 0 trong mảng được trả về

Chương trình Python để đếm các số 0 ở cuối trong giai thừa của một số

Cải thiện bài viết

Lưu bài viết

Thích bài viết

  • Độ khó. Trung bình
  • Cập nhật lần cuối. 31 tháng 1 năm 2023

  • Đọc
  • Bàn luận
  • khóa học
  • Luyện tập
  • Băng hình
  • Cải thiện bài viết

    Lưu bài viết

    Cho số nguyên n, hãy viết hàm trả về số lượng các số 0 ở cuối trong n.  

    ví dụ

    Input: n = 5
    Output: 1 
    Factorial of 5 is 120 which has one trailing 0.
    
    Input: n = 20
    Output: 4
    Factorial of 20 is 2432902008176640000 which has
    4 trailing zeroes.
    
    Input: n = 100
    Output: 24
    Trailing 0s in n! = Count of 5s in prime factors of n!
                      = floor[n/5] + floor[n/25] + floor[n/125] + ....

    Phương pháp 1

    Python3




    # Python3 program to

    # count trailing 0s

    # in n !

     

    # Function to return

    # trailing 0s in

    # factorial of n

     

     

    Trailing 0s in n! = Count of 5s in prime factors of n!
                      = floor[n/5] + floor[n/25] + floor[n/125] + ....
    0
    Trailing 0s in n! = Count of 5s in prime factors of n!
                      = floor[n/5] + floor[n/25] + floor[n/125] + ....
    1

     

    Trailing 0s in n! = Count of 5s in prime factors of n!
                      = floor[n/5] + floor[n/25] + floor[n/125] + ....
    2
    Trailing 0s in n! = Count of 5s in prime factors of n!
                      = floor[n/5] + floor[n/25] + floor[n/125] + ....
    3

    Trailing 0s in n! = Count of 5s in prime factors of n!
                      = floor[n/5] + floor[n/25] + floor[n/125] + ....
    2
    Trailing 0s in n! = Count of 5s in prime factors of n!
                      = floor[n/5] + floor[n/25] + floor[n/125] + ....
    5_______1_______6
    Trailing 0s in n! = Count of 5s in prime factors of n!
                      = floor[n/5] + floor[n/25] + floor[n/125] + ....
    7

     

    Trailing 0s in n! = Count of 5s in prime factors of n!
                      = floor[n/5] + floor[n/25] + floor[n/125] + ....
    2
    Trailing 0s in n! = Count of 5s in prime factors of n!
                      = floor[n/5] + floor[n/25] + floor[n/125] + ....
    9

    Trailing 0s in n! = Count of 5s in prime factors of n!
                      = floor[n/5] + floor[n/25] + floor[n/125] + ....
    2
    Count of trailing 0s in 100 ! is 24
    1

    Trailing 0s in n! = Count of 5s in prime factors of n!
                      = floor[n/5] + floor[n/25] + floor[n/125] + ....
    2
    Count of trailing 0s in 100 ! is 24
    3

    Trailing 0s in n! = Count of 5s in prime factors of n!
                      = floor[n/5] + floor[n/25] + floor[n/125] + ....
    2
    Count of trailing 0s in 100 ! is 24
    5
    Trailing 0s in n! = Count of 5s in prime factors of n!
                      = floor[n/5] + floor[n/25] + floor[n/125] + ....
    6
    Count of trailing 0s in 100 ! is 24
    7

    Trailing 0s in n! = Count of 5s in prime factors of n!
                      = floor[n/5] + floor[n/25] + floor[n/125] + ....
    2
    Count of trailing 0s in 100 ! is 24
    9
    Count of trailing 0s in 100 ! is 24
    0
    Count of trailing 0s in 100 ! is 24
    1
    Count of trailing 0s in 100 ! is 24
    2
    Trailing 0s in n! = Count of 5s in prime factors of n!
                      = floor[n/5] + floor[n/25] + floor[n/125] + ....
    6
    Count of trailing 0s in 100 ! is 24
    4
    Count of trailing 0s in 100 ! is 24
    5

    Count of trailing 0s in 100 ! is 24
    6_______1_______5
    Count of trailing 0s in 100 ! is 24
    8
    Trailing 0s in n! = Count of 5s in prime factors of n!
                      = floor[n/5] + floor[n/25] + floor[n/125] + ....
    6 # Python3 program to0
    Count of trailing 0s in 100 ! is 24
    0
    Count of trailing 0s in 100 ! is 24
    1 # Python3 program to3

    Count of trailing 0s in 100 ! is 24
    6_______13_______5# Python3 program to6
    Trailing 0s in n! = Count of 5s in prime factors of n!
                      = floor[n/5] + floor[n/25] + floor[n/125] + ....
    6
    Count of trailing 0s in 100 ! is 24
    7

     

    Trailing 0s in n! = Count of 5s in prime factors of n!
                      = floor[n/5] + floor[n/25] + floor[n/125] + ....
    2# count trailing 0s0 # Python3 program to0# count trailing 0s2

     

     

    # count trailing 0s3

    # count trailing 0s4_______1_______6 # count trailing 0s6

    # count trailing 0s7# count trailing 0s8# count trailing 0s9

    Count of trailing 0s in 100 ! is 24
    8

    # in n !1____85_______2# in n !3

    đầu ra.

    Count of trailing 0s in 100 ! is 24

    Thời gian phức tạp. O[log5n]

    Không gian phụ trợ. Ô[1]

    Vui lòng tham khảo bài viết đầy đủ về Đếm các số 0 ở cuối trong giai thừa của một số để biết thêm chi tiết

    Phương pháp 2. Sử dụng toán học. giai thừa[] và vòng lặp for

    Python3




    # Python3 program to

    # count trailing 0s

    # in n !

     

    # Function to return

    # trailing 0s in

    # factorial of n

     

     

    Trailing 0s in n! = Count of 5s in prime factors of n!
                      = floor[n/5] + floor[n/25] + floor[n/125] + ....
    0
    Trailing 0s in n! = Count of 5s in prime factors of n!
                      = floor[n/5] + floor[n/25] + floor[n/125] + ....
    1

    Trailing 0s in n! = Count of 5s in prime factors of n!
                      = floor[n/5] + floor[n/25] + floor[n/125] + ....
    2# Function to return3 # Function to return4

    Trailing 0s in n! = Count of 5s in prime factors of n!
                      = floor[n/5] + floor[n/25] + floor[n/125] + ....
    2# Function to return6
    Trailing 0s in n! = Count of 5s in prime factors of n!
                      = floor[n/5] + floor[n/25] + floor[n/125] + ....
    6
    Trailing 0s in n! = Count of 5s in prime factors of n!
                      = floor[n/5] + floor[n/25] + floor[n/125] + ....
    7

    Trailing 0s in n! = Count of 5s in prime factors of n!
                      = floor[n/5] + floor[n/25] + floor[n/125] + ....
    2# trailing 0s in0_______1_______6 # trailing 0s in2

    Trailing 0s in n! = Count of 5s in prime factors of n!
                      = floor[n/5] + floor[n/25] + floor[n/125] + ....
    2# trailing 0s in4
    Trailing 0s in n! = Count of 5s in prime factors of n!
                      = floor[n/5] + floor[n/25] + floor[n/125] + ....
    6 # trailing 0s in6# trailing 0s in7

    Trailing 0s in n! = Count of 5s in prime factors of n!
                      = floor[n/5] + floor[n/25] + floor[n/125] + ....
    2# trailing 0s in9
    Trailing 0s in n! = Count of 5s in prime factors of n!
                      = floor[n/5] + floor[n/25] + floor[n/125] + ....
    6 # factorial of n1_______88_______2
    Count of trailing 0s in 100 ! is 24
    4# factorial of n4

    Trailing 0s in n! = Count of 5s in prime factors of n!
                      = floor[n/5] + floor[n/25] + floor[n/125] + ....
    2# factorial of n6
    Count of trailing 0s in 100 ! is 24
    5# factorial of n8 # factorial of n9

    Count of trailing 0s in 100 ! is 24
    6_______1_______01
    Trailing 0s in n! = Count of 5s in prime factors of n!
                      = floor[n/5] + floor[n/25] + floor[n/125] + ....
    02
    Trailing 0s in n! = Count of 5s in prime factors of n!
                      = floor[n/5] + floor[n/25] + floor[n/125] + ....
    6
    Trailing 0s in n! = Count of 5s in prime factors of n!
                      = floor[n/5] + floor[n/25] + floor[n/125] + ....
    04
    Count of trailing 0s in 100 ! is 24
    5

    Trailing 0s in n! = Count of 5s in prime factors of n!
                      = floor[n/5] + floor[n/25] + floor[n/125] + ....
    06
    Trailing 0s in n! = Count of 5s in prime factors of n!
                      = floor[n/5] + floor[n/25] + floor[n/125] + ....
    07

    Count of trailing 0s in 100 ! is 24
    6_______1_______09
    Trailing 0s in n! = Count of 5s in prime factors of n!
                      = floor[n/5] + floor[n/25] + floor[n/125] + ....
    10

    Trailing 0s in n! = Count of 5s in prime factors of n!
                      = floor[n/5] + floor[n/25] + floor[n/125] + ....
    06# Function to return6
    Count of trailing 0s in 100 ! is 24
    8
    Trailing 0s in n! = Count of 5s in prime factors of n!
                      = floor[n/5] + floor[n/25] + floor[n/125] + ....
    6
    Count of trailing 0s in 100 ! is 24
    4

    Trailing 0s in n! = Count of 5s in prime factors of n!
                      = floor[n/5] + floor[n/25] + floor[n/125] + ....
    2# count trailing 0s0 # Function to return6

     

     

    # count trailing 0s3

    # count trailing 0s4_______1_______6 # count trailing 0s6

    # count trailing 0s7# count trailing 0s8# count trailing 0s9

    Count of trailing 0s in 100 ! is 24
    8

    # in n !1____85_______2# in n !3

    Đầu ra

    Count of trailing 0s in 100 ! is 24

    Độ phức tạp về thời gian. Trên]

    Độ phức tạp của không gian. Trên]


    Ghi chú cá nhân của tôi arrow_drop_up

    Tiết kiệm

    Vui lòng Đăng nhập để nhận xét.

    Chủ Đề