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
- Chuyển số về dạng tuyệt đối;
- Lấy chữ số cuối cùng bằng phép tính
number % 10
; - 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
- Chuyển đổi số nguyên thành một chuỗi;
- Lấy ký tự cuối cùng của chuỗi;
- Chuyển đổi chuỗi trở lại số nguyên;
- 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 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#
#include
98
#include
99
using
9 201
using
6 // digits of a number
57
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
using
72______136_______2
9 240
9 25
// Program to find first and last
0// Program to find first and last
0
using
78
PHP
9 274
// Program to find first and last
// digits of a number
// Find the first digit
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// Program to find first and last
0
// Program to find first and last
1
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// Program to find first and last
0
// Program to find first and last
18
9 280
// Program to find first and last
20// Program to find first and last
21
9 21
9 280
namespace
31#include
2namespace
33// Program to find first and last
27_______161_______3
9 280
namespace
37// Program to find first and last
31// Find the first digit
2
namespace
40
namespace
41
Javascript
// Program to find first and last
35
namespace
43
9 276
// Find the first digit
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
// Program to find first and last
0
// Program to find first and last
1
9 278
namespace
669 25
9 25
// Program to find first and last
89 25
9 28
namespace
72// Program to find first and last
0
// Program to find first and last
18
// Program to find first and last
79
namespace
76#include
2std;
8
namespace
79namespace
80std;
8
namespace
82
// Program to find first and last
88
Đầ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[].