Lấy 2 chữ số cuối của số nguyên Python
Bạn có thể sử dụng toán tử modulo (
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 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 Đ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ùngBạn có thể làm như sau
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 25 9 261 9 257 9 289_______4_______0 #include 629 264 // digits of a number 169 289 std; 7#include 67
9 25 #include 699 25 9 28 9 29
9 274 9 248 9 276 9 25 9 25 9 278 9 25 9 28 9 281_______4_______82 std; 7std; 8
9 285 9 261 9 257 9 234 // Find the first digit 29 290_______4_______91_______4_______57 #include 2std; 89 290_______4_______96
9 297 C#
9 201
9 25 9 25 // Find the first digit 9 25 namespace 3 9 20 9 21 9 20 9 23 9 25 9 24 9 22______137_______02 9 22_______4_______0 // digits of a number 059 20 using 219 25 9 22______137_______11 9 22_______137_______13 9 20 // digits of a number 829 20 using 309 25 9 22_______137_______19 9 22_______4_______8 9 29 9 25 // Program to find first and last 09 25 9 25 // Program to find first and last 19 25 namespace 3 9 20 // Program to find first and last 3_______4_______0 9 23 9 25 9 24 9 22_______161_______8 9 22_______4_______8 // digits of a number 19 25 // Program to find first and last 09 25 9 25 // digits of a number 39 25 using 5 namespace 3 9 227 9 259 9 25 9 24 9 22_______4_______0 // digits of a number 99 22_______4_______66
9 240 9 25 // Program to find first and last 0
PHP9 274
9 278 9 21 9 280 std; 89 24 9 25 // digits of a number 029 25 using 91 using 929 280 9 218
9 25 // digits of a number 119 25 9 280 using 999 280 namespace 01using 91namespace 03
9 25 // digits of a number 199 25 9 28 9 280 // Find the first digit 2
9 278 // Program to find first and last 39 280 std; 89 24 9 25 // Program to find first and last 89 25 9 28 9 289_______4_______80 // Program to find first and last 16
9 280 // Program to find first and last 20
9 21 9 280 namespace 31#include 2namespace 33
9 280 namespace 37// Program to find first and last 31// Find the first digit 2
Javascript
9 276
9 278 // Program to find first and last 429 24 9 25 9 25 // digits of a number 029 25 namespace 53
9 25 // digits of a number 119 25 namespace 57
9 25 // digits of a number 199 25 9 28 9 29
9 278 namespace 669 25 9 25 // Program to find first and last 89 25 9 28 namespace 72
Đầu ra 9 2 Thời gian phức tạp. O(log(log10(n)) 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(). |