Lấy 2 chữ số cuối của số nguyên Python

Bạn có thể sử dụng toán tử modulo (%) để lấy chữ số cuối cùng của một số nguyên theo cách sau

  1. Chuyển số về dạng tuyệt đối;
  2. Lấy chữ số cuối cùng bằng phép tính number % 10;
  3. Thêm dấu âm vào chữ số cuối cùng nếu số nguyên là số âm

Toán tử modulo trong Python sẽ không trả về chữ số cuối cùng chính xác cho số âm vì. Để có kết quả chính xác, trước tiên bạn phải chuyển đổi số sang dạng tuyệt đối (sử dụng abs()), sau đó thêm lại dấu

Ví dụ

def last_digit(num):
    last_digit_unsigned = abs(num) % 10
    return -last_digit_unsigned if (num < 0) else last_digit_unsigned

print(last_digit(0)) # 0
print(last_digit(1234)) # 4
print(last_digit(-1234)) # -4

Ngoài ra, bạn có thể sử dụng công thức toán học sau đây để tính toán phần còn lại một cách chính xác

def last_digit(num):
    return num - (10 * int(num / 10))

print(last_digit(0)) # 0
print(last_digit(1234)) # 4
print(last_digit(-1234)) # -4

Điều này thực hiện (tôi. e. sử dụng phương pháp int(), phần phân số bị loại bỏ/cắt bớt khỏi kết quả của phép chia)

Điều này hoạt động theo cách sau

# num = 1234

# last_digit = 1234 - (10 * int(1234 / 10))
# last_digit = 1234 - (10 * int(123.4))
# last_digit = 1234 - (10 * 123)
# last_digit = 1234 - 1230
# last_digit = 4

Chuyển đổi thành chuỗi và truy xuất ký tự cuối cùng

Bạn có thể làm như sau

  1. Chuyển đổi số nguyên thành một chuỗi;
  2. Lấy ký tự cuối cùng của chuỗi;
  3. Chuyển đổi chuỗi trở lại số nguyên;
  4. Thêm dấu âm vào chữ số cuối cùng nếu số nguyên là số âm
def last_digit(num):
    num_str = str(num)
    last_digit_unsigned = int(num_str[-1])
    return -last_digit_unsigned if (num < 0) else last_digit_unsigned

print(last_digit(0)) # 0
print(last_digit(1234)) # 4
print(last_digit(-1234)) # -4

Khi số nguyên được chuyển đổi thành chuỗi, dấu được coi là một ký tự trong chuỗi và không có ý nghĩa đặc biệt, đó là lý do tại sao nó không được thêm vào chữ số cuối cùng. Vì lý do này, dấu hiệu được thêm lại sau khi ký tự cuối cùng được chuyển đổi thành số nguyên


Hy vọng bạn tìm thấy bài viết này hữu ích. Nó được xuất bản vào ngày 05 tháng 12 năm 2022. Hãy thể hiện tình yêu và sự ủng hộ của bạn bằng cách

9 2
5
9 2
61
9 2
57
9 2
89_______4_______0#include 62
9 2
64 // digits of a number16
9 2
89std;7#include 67

 

9 2
5#include 69

9 2
5
9 2
8
9 2
9

 

9 2
74

9 2
48
9 2
76

9 2
5

9 2
5
9 2
78

9 2
5
9 2
8
9 2
81_______4_______82 std;7std;8

 

9 2
85

9 2
61
9 2
57
9 2
34// Find the first digit2

9 2
90_______4_______91_______4_______57 #include 2std;8

9 2
90_______4_______96

 

9 2
97

C#




#include 98

#include 99

using

9 2
01

 

using6 // digits of a number57

9 2
5

9 2
5// Find the first digit

9 2
5namespace3
9 2
0
9 2
1
9 2
0
9 2
3

9 2
5
9 2
4

9 2
2______137_______02

9 2
2_______4_______0 // digits of a number05
9 2
0using21

9 2
5

9 2
2______137_______11

9 2
2_______137_______13
9 2
0// digits of a number82
9 2
0using30

9 2
5

9 2
2_______137_______19

9 2
2_______4_______8
9 2
9

9 2
5// Program to find first and last0

9 2
5

9 2
5// Program to find first and last1

9 2
5namespace3
9 2
0 // Program to find first and last3_______4_______0
9 2
3

9 2
5
9 2
4

9 2
2_______161_______8

9 2
2_______4_______8 // digits of a number1

9 2
5// Program to find first and last0

9 2
5

9 2
5// digits of a number3

9 2
5using5 namespace3
9 2
27
9 2
59

9 2
5
9 2
4

9 2
2_______4_______0 // digits of a number9

9 2
2_______4_______66

using72______136_______2

9 2
40

9 2
5// Program to find first and last0

// Program to find first and last0

 

 

using78

PHP




9 2
74

// Program to find first and last

// digits of a number

 

// Find the first digit

9 2
78
9 2
1
9 2
80std;8

9 2
4

9 2
5// digits of a number02

9 2
5using91 using92
9 2
80
9 2
18

 

9 2
5// digits of a number11

9 2
5
9 2
80 using99
9 2
80 namespace01using91namespace03

 

9 2
5// digits of a number19

9 2
5
9 2
8
9 2
80// Find the first digit2

// Program to find first and last0

 

// Program to find first and last1

9 2
78 // Program to find first and last3
9 2
80std;8

9 2
4

9 2
5// Program to find first and last8

9 2
5
9 2
8
9 2
89_______4_______80 // Program to find first and last16

// Program to find first and last0

 

// Program to find first and last18

9 2
80 // Program to find first and last20

// Program to find first and last21

9 2
1
9 2
80namespace31#include 2namespace33

// Program to find first and last27_______161_______3

9 2
80namespace37// Program to find first and last31// Find the first digit2

 

namespace40

namespace41

Javascript




// Program to find first and last35

 

namespace43

9 2
76

 

// Find the first digit

9 2
78 // Program to find first and last42

9 2
4

9 2
5

9 2
5// digits of a number02

9 2
5namespace53

 

9 2
5// digits of a number11

9 2
5namespace57

 

9 2
5// digits of a number19

9 2
5
9 2
8
9 2
9

// Program to find first and last0

 

// Program to find first and last1

9 2
78 namespace66

9 2
5

9 2
5// Program to find first and last8

9 2
5
9 2
8 namespace72

// Program to find first and last0

 

// Program to find first and last18

// Program to find first and last79

namespace76#include 2std;8

namespace79namespace80std;8

 

namespace82

 

// Program to find first and last88

Đầu ra

9 2

Thời gian phức tạp. O(log(log10(n))
Không gian phụ trợ. Ô(1)

Lưu ý quan trọng. log10() là một hàm toán học có trong toán học. tập tin tiêu đề h. Nó trả về giá trị log cơ số 10 của tham số đã truyền cho hàm log10().