Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Đọc
Examples:
Input: N = 1247 Output: 8 Explanation: First digit is 1 and Last digit is 7. So, addition of these two [1 + 7] is equal to 8.
Input: N = 73 Output: 10
Bàn luận String implementation
- Cho một số nguyên dương N [ít nhất là chứa hai chữ số]. Nhiệm vụ là viết một chương trình Python để thêm chữ số đầu tiên và cuối cùng của số đã cho N.
- Phương pháp 1: Thực hiện chuỗi
- Lấy đầu vào ở dạng chuỗi hoặc typecast đã cho đầu vào trong chuỗi.
- Bây giờ chọn chỉ số 0 của chuỗi và typecast nó vào số nguyên và lưu trữ nó trong một biến.
- Điều tương tự với chỉ số -1 và cũng lưu trữ trong một biến khác.
Bây giờ thêm hai biến này vàWe can access the first element of String using string[0] and the last element of String using string[-1].
In chúng dưới dạng đầu ra.
Python3
Lưu ý: & nbsp; chúng ta có thể truy cập phần tử đầu tiên của chuỗi bằng chuỗi [0] và phần tử cuối cùng của chuỗi bằng chuỗi [-1].
Đại diện chuỗi
number
=
1247
number
=
str
Input: N = 73 Output: 100
Input: N = 73 Output: 101
=
Input: N = 73 Output: 103
Input: N = 73 Output: 104
Input: N = 73 Output: 105
Input: N = 73 Output: 106
Addition of first and last digit of the number is 89
Input : 12345 Output : First digit: 1 last digit : 5 Input : 98562 Output : First digit: 9 last digit : 20
Input : 12345 Output : First digit: 1 last digit : 5 Input : 98562 Output : First digit: 9 last digit : 21
Input : 12345 Output : First digit: 1 last digit : 5 Input : 98562 Output : First digit: 9 last digit : 22
Input : 12345 Output : First digit: 1 last digit : 5 Input : 98562 Output : First digit: 9 last digit : 23
Input : 12345 Output : First digit: 1 last digit : 5 Input : 98562 Output : First digit: 9 last digit : 24
Output:
Input: N = 73 Output: 107=
Input: N = 73 Output: 103Input: N = 73 Output: 104Addition of first and last digit of the number is 81Addition of first and last digit of the number is 822216
Addition of first and last digit of the number is 84=
Input: N = 73 Output: 101Addition of first and last digit of the number is 87Addition of first and last digit of the number is 88: O[1] since performing constant operationsBổ sung chữ số đầu tiên và cuối cùng của số là 8 O[1] since using constant space for variables
Độ phức tạp về thời gian: O [1] kể từ khi thực hiện các hoạt động không đổiSolve it using an integer
- Không gian phụ trợ: O [1] Vì sử dụng không gian không đổi cho các biến
- Phương pháp 2: Giải quyết bằng cách sử dụng số nguyênresult variable.
- Chúng tôi đã cho một số nguyên tích cực.
- Sau khi chia cho 10, lưu trữ phần còn lại trong một biến kết quả.
- Tiếp tục vòng lặp cho đến khi số trở nên nhỏ hơn 9.
- Mỗi lần trong vòng lặp, chia số cho 10 [phân chia số nguyên].result variable.
- Sau khi kết thúc vòng lặp.
Thêm số trong biến kết quả.Whenever we divide any number with 10, we get the last digit as the remainder. If we divide any number with 100, we get the last two-digit as the remainder.
Python3
Lưu ý: & nbsp; chúng ta có thể truy cập phần tử đầu tiên của chuỗi bằng chuỗi [0] và phần tử cuối cùng của chuỗi bằng chuỗi [-1].
Đại diện chuỗi
number
=
1247
number
=
str
Input: N = 73 Output: 100
Input: N = 73 Output: 101
=
Input: N = 73 Output: 103
Input: N = 73 Output: 104
Input: N = 73 Output: 105
Input: N = 73 Output: 106
Input: N = 73 Output: 107
=
Input: N = 73 Output: 103
Input: N = 73 Output: 104
Addition of first and last digit of the number is 81
Addition of first and last digit of the number is 822216
Output:
Addition of first and last digit of the number is 8
Addition of first and last digit of the number is 84=
Input: N = 73 Output: 101Addition of first and last digit of the number is 87Addition of first and last digit of the number is 88O[n], where n is how many digits are there in the given number.Bổ sung chữ số đầu tiên và cuối cùng của số là 8: O[1] since using constant space for variables
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Đọc
Examples:
Input : 12345 Output : First digit: 1 last digit : 5 Input : 98562 Output : First digit: 9 last digit : 2
Bàn luậnmodulo operator %. When modulo divided by 10 returns its last digit.
Suppose if n = 1234
then last Digit = n % 10 => 4
To find first digit of a number is little expensive than last digit. To find first digit of a number we divide the given number by 10 until number is greater than 10. At the end we are left with the first digit.
Cho một số để tìm chữ số đầu tiên và cuối cùng của một số.examples: & nbsp; & nbsp;
C++
Để tìm chữ số cuối cùng của một số, chúng tôi sử dụng toán tử modulo %. Khi modulo chia cho 10 trả về chữ số cuối cùng của nó. Để tìm chữ số đầu tiên của một số, chúng tôi chia số đã cho 10 cho đến khi số lớn hơn 10. Ở cuối, chúng tôi còn lại với chữ số đầu tiên. & NBSP;
Cách tiếp cận 1 [với vòng lặp]:
1247
2
number
0
1247
3 1247
4 1247
5
number
4number
5
Input: N = 73 Output: 103
1247
7Input: N = 73 Output: 103
1247
9number
9
number
7number
3 number
3
number
0
number
7number
7 number
8
number
9
Input: N = 73 Output: 103
=
1Input: N = 73 Output: 103
1247
9number
0
number
7number
7 =
7
number
7str
6str
7
number
4str
9
Input: N = 73 Output: 103
str
0number
9
number
7Input: N = 73
Output: 10
3 str
4
number
7number
7
Input: N = 73 Output: 1002
Java
Input: N = 73 Output: 1004
Input: N = 73 Output: 1005
Input: N = 73 Output: 1004
Input: N = 73 Output: 1007
number
7number
0
Input: N = 73 Output: 1008
Input: N = 73 Output: 1009
Input: N = 73 Output: 1010
Input: N = 73 Output: 1025
Input: N = 73 Output: 1026
number
2Input: N = 73 Output: 1028
number
7
Input: N = 73 Output: 1008
Input: N = 73 Output: 1013
Input: N = 73 Output: 103
1247
7Input: N = 73 Output: 103
1247
9number
7number
9
number
4number
3
Input: N = 73 Output: 10222
number
2Input: N = 73 Output: 1024
number
7number
0
number
4number
7 number
8
number
7number
9
number
7
Input: N = 73 Output: 1008
Input: N = 73 Output: 1013
Input: N = 73 Output: 103
=
1Input: N = 73 Output: 103
1247
9number
7number
0
number
4number
7
Input: N = 73 Output: 1045
number
2Input: N = 73 Output: 1047
number
4
Input: N = 73 Output: 1063
str
7number
4
Input: N = 73 Output: 1066
number
7number
9
number
9
Python3
number
7
Input: N = 73 Output: 1008
Input: N = 73 Output: 1013
Input: N = 73 Output: 1053
Input: N = 73 Output: 1054
number
4
Input: N = 73 Output: 103
Input: N = 73 Output: 1059
Input: N = 73 Output: 1060
Input: N = 73 Output: 1028
Input: N = 73 Output: 1070
Input: N = 73 Output: 1071
number
7number
3
Input: N = 73 Output: 1074
=
number
2number
6number
4
Input: N = 73 Output: 1079
=
Input: N = 73 Output: 1079
=
1 number
2Input: N = 73 Output: 1028
number
7number
7
Input: N = 73 Output: 103
Input: N = 73 Output: 1088
Input: N = 73 Output: 1070
Input: N = 73 Output: 1090
number
7number
7
Input: N = 73 Output: 1093
number
1 number
2Input: N = 73 Output: 1024
Addition of first and last digit of the number is 89
Addition of first and last digit of the number is 807
C#
Input: N = 73 Output: 1079
=
Input: N = 73 Output: 1060
Input: N = 73 Output: 1028
Input: N = 73 Output: 1004
Input: N = 73 Output: 1005
Input: N = 73 Output: 1004
Input: N = 73 Output: 1007
number
7number
0
Input: N = 73 Output: 1008
Input: N = 73 Output: 1009
Input: N = 73 Output: 1010
Input: N = 73 Output: 1025
number
5number
7
Input: N = 73 Output: 1008
Input: N = 73 Output: 1013
Input: N = 73 Output: 103
1247
7Input: N = 73 Output: 103
1247
9number
7number
9
number
4number
3
Input: N = 73 Output: 10222
number
2Input: N = 73 Output: 1024
number
7number
0
number
4number
7 number
8
number
7number
9
number
7
Input: N = 73 Output: 1008
Input: N = 73 Output: 1013
Input: N = 73 Output: 103
=
1Input: N = 73 Output: 103
1247
9number
7number
0
number
4number
7
Input: N = 73 Output: 1045
number
2Input: N = 73 Output: 1047
number
4
Addition of first and last digit of the number is 857
str
7number
4
Input: N = 73 Output: 1066
number
7number
9
number
9
number
7Input: N = 73
Output: 10
08 Input: N = 73
Output: 10
13 Input: N = 73
Output: 10
53 Input: N = 73
Output: 10
54
Addition of first and last digit of the number is 864
number
4
Input: N = 73 Output: 103
Input: N = 73 Output: 1059
Input: N = 73 Output: 1060
Input: N = 73 Output: 1028
number
0
Input: N = 73 Output: 1070
Input: N = 73 Output: 1071
number
7number
3
Input: N = 73 Output: 1074
=
number
2number
6number
4
Input: N = 73 Output: 1079
=
Input: N = 73 Output: 1079
=
1 number
2Input: N = 73 Output: 1028
number
9
number
7number
7
Input: N = 73 Output: 103
Input: N = 73 Output: 1088
number
0
Input: N = 73 Output: 1070
Input: N = 73 Output: 1090
number
9
number
7number
7
Input: N = 73 Output: 1093
number
1 number
2Input: N = 73 Output: 1024
Input: N = 73 Output: 1079
=
Input: N = 73 Output: 1060
Input: N = 73 Output: 1028
Input : 12345 Output : First digit: 1 last digit : 5 Input : 98562 Output : First digit: 9 last digit : 203
=
1Addition of first and last digit of the number is 867
Input : 12345 Output : First digit: 1 last digit : 5 Input : 98562 Output : First digit: 9 last digit : 200
Input : 12345 Output : First digit: 1 last digit : 5 Input : 98562 Output : First digit: 9 last digit : 207
Input: N = 73 Output: 1028
Addition of first and last digit of the number is 8
9Addition of first and last digit of the number is 8
02=
str
7Input: N = 73
Output: 10
24
Input : 12345 Output : First digit: 1 last digit : 5 Input : 98562 Output : First digit: 9 last digit : 209
1247
3
Addition of first and last digit of the number is 809
number
7number
0
Input: N = 73 Output: 1008
Input: N = 73 Output: 1009
Input: N = 73 Output: 1010
number
7
Input: N = 73 Output: 1008
Input: N = 73 Output: 1013
Input: N = 73 Output: 103
1247
7Input: N = 73 Output: 103
1247
9number
4number
3
Input: N = 73 Output: 10222
number
2Input: N = 73 Output: 1024
number
7number
9
number
4number
7 number
8
number
7number
0
number
7
Input: N = 73 Output: 1008
Input: N = 73 Output: 1013
Input: N = 73 Output: 103
=
1Input: N = 73 Output: 103
1247
9number
7number
9
Input : 12345 Output : First digit: 1 last digit : 5 Input : 98562 Output : First digit: 9 last digit : 235
Input : 12345 Output : First digit: 1 last digit : 5 Input : 98562 Output : First digit: 9 last digit : 236
number
4
Input : 12345 Output : First digit: 1 last digit : 5 Input : 98562 Output : First digit: 9 last digit : 238
str
7number
4
Input: N = 73 Output: 1066
Input : 12345 Output : First digit: 1 last digit : 5 Input : 98562 Output : First digit: 9 last digit : 242
number
4number
7 Input: N = 73
Output: 10
45number
2Input: N = 73
Output: 10
47 O[log10n]
Auxiliary Space: O[1]
number
7Input: N = 73
Output: 10
08 Input: N = 73
Output: 10
13 Input: N = 73
Output: 10
53 Input: N = 73
Output: 10
54
C++
1247
2
Cách tiếp cận 1 [với vòng lặp]:
1247
2
number
0
1247
3 1247
4 1247
5
number
7
Input : 12345 Output : First digit: 1 last digit : 5 Input : 98562 Output : First digit: 9 last digit : 260
Input: N = 73 Output: 103
Input : 12345 Output : First digit: 1 last digit : 5 Input : 98562 Output : First digit: 9 last digit : 262
Input : 12345 Output : First digit: 1 last digit : 5 Input : 98562 Output : First digit: 9 last digit : 263
Input : 12345 Output : First digit: 1 last digit : 5 Input : 98562 Output : First digit: 9 last digit : 264
Input: N = 73 Output: 103
1247
7Input: N = 73 Output: 103
1247
9number
9
number
7number
3 number
3
number
0
number
7number
7 number
8
number
9
Input: N = 73 Output: 103
=
1Input: N = 73 Output: 103
1247
9number
0
number
7number
7 =
7
number
7str
6str
7
Input : 12345 Output : First digit: 1 last digit : 5 Input : 98562 Output : First digit: 9 last digit : 235
str
9Input: N = 73 Output: 103
str
0number
9
number
7Input: N = 73
Output: 10
3 str
4
number
7number
7
Input: N = 73 Output: 1002
Java
Input: N = 73 Output: 1004
Input: N = 73 Output: 1005
number
7number
0
Input: N = 73 Output: 1004
Input: N = 73 Output: 1007
number
4
Input : 12345 Output : First digit: 1 last digit : 5 Input : 98562 Output : First digit: 9 last digit : 260
Input: N = 73 Output: 103
number
13Input: N = 73 Output: 103
number
15number
2number
17number
4number
7 number
8
number
7number
9
number
7
Input: N = 73 Output: 1013
Input: N = 73 Output: 103
=
1Input: N = 73 Output: 103
1247
9number
7number
0
number
4number
7
Input: N = 73 Output: 1045
number
2Input: N = 73 Output: 1047
number
7number
9
number
7
Input: N = 73 Output: 1008
Input: N = 73 Output: 1013
Input: N = 73 Output: 1053
number
42number
7number
0
number
4
Input: N = 73 Output: 103
Input: N = 73 Output: 1059
Input: N = 73 Output: 1060
Input: N = 73 Output: 1028
number
4number
51
number
52str
7
Input: N = 73 Output: 1066
number
7number
9
number
9
Python3
Input: N = 73 Output: 1004
number
59Input: N = 73 Output: 1070
Input: N = 73 Output: 1071
number
7number
63=
Input : 12345 Output : First digit: 1 last digit : 5 Input : 98562 Output : First digit: 9 last digit : 20
Input: N = 73 Output: 103
number
67Các
number
7number
7 number
8
Input: N = 73 Output: 1070
Input: N = 73 Output: 1090
number
7number
7
Input: N = 73 Output: 1093
number
1 number
2Input: N = 73 Output: 1024
Input: N = 73 Output: 1079
=
Input: N = 73 Output: 1060
Input: N = 73 Output: 1028
Addition of first and last digit of the number is 89
Addition of first and last digit of the number is 802
=
str
7Input: N = 73 Output: 1024
Addition of first and last digit of the number is 89
Addition of first and last digit of the number is 807
C#
1247
3
Addition of first and last digit of the number is 809
Input: N = 73 Output: 1009
Input : 12345 Output : First digit: 1 last digit : 5 Input : 98562 Output : First digit: 9 last digit : 296
number
7
Input: N = 73 Output: 1013
Input: N = 73 Output: 103
1247
7Input: N = 73 Output: 103
1247
9number
7number
0
number
4
Input: N = 73 Output: 103
Input : 12345 Output : First digit: 1 last digit : 5 Input : 98562 Output : First digit: 9 last digit : 254
Input: N = 73 Output: 103
=
17number
4
Input : 12345 Output : First digit: 1 last digit : 5 Input : 98562 Output : First digit: 9 last digit : 260
Input: N = 73 Output: 103
number
13Input: N = 73 Output: 103
=
23number
4number
7 number
8
number
7number
9
number
7
Input: N = 73 Output: 1013
Input: N = 73 Output: 103
=
1Input: N = 73 Output: 103
1247
9number
7number
0
number
4number
7
Input: N = 73 Output: 1045
number
2Input: N = 73 Output: 1047
number
7number
9
number
7
Input: N = 73 Output: 1008
Input: N = 73 Output: 1013
Input: N = 73 Output: 1053
number
42number
7number
0
number
4
Input: N = 73 Output: 103
Input: N = 73 Output: 1059
Input: N = 73 Output: 1060
Input: N = 73 Output: 1028
number
4=
53
number
52str
7
Input: N = 73 Output: 1066
number
7number
9
number
9
Input: N = 73
Output: 10
04 number
59
Addition of first and last digit of the number is 864
Input: N = 73 Output: 1070
Input: N = 73 Output: 1071
number
0
number
7number
63=
Input : 12345 Output : First digit: 1 last digit : 5 Input : 98562 Output : First digit: 9 last digit : 20
Input: N = 73 Output: 103
number
67Các
number
7number
7 number
8
number
9
Input: N = 73 Output: 1070
Input: N = 73 Output: 1090
number
0
number
7number
7
Input: N = 73 Output: 1093
number
1 number
2Input: N = 73 Output: 1024
number
9
Input: N = 73 Output: 1079
=
Input: N = 73 Output: 1060
Input: N = 73 Output: 1028
Addition of first and last digit of the number is 89
Addition of first and last digit of the number is 802
=
str
7Input: N = 73 Output: 1024
Input : 12345 Output : First digit: 1 last digit : 5 Input : 98562 Output : First digit: 9 last digit : 203
=
1Addition of first and last digit of the number is 867
1247
05Input : 12345 Output : First digit: 1 last digit : 5 Input : 98562 Output : First digit: 9 last digit : 207
Input: N = 73 Output: 1028
1247
08
1247
3 Addition of first and last digit of the number is 8
09
Input : 12345 Output : First digit: 1 last digit : 5 Input : 98562 Output : First digit: 9 last digit : 209
Input: N = 73 Output: 1009
Input : 12345 Output : First digit: 1 last digit : 5 Input : 98562 Output : First digit: 9 last digit : 296
number
0
number
71247
14
number
71247
16
number
7number
7 number
8
number
9
Input: N = 73 Output: 1070
Input: N = 73 Output: 1090
number
7number
7
Input: N = 73 Output: 1093
number
1 number
2Input: N = 73 Output: 1024
number
9
Input : 12345 Output : First digit: 1 last digit : 5 Input : 98562 Output : First digit: 9 last digit : 236
1247
28str
7
Input: N = 73 Output: 1024
1247
311247
32
Input: N = 73 Output: 1024
Input : 12345 Output : First digit: 1 last digit : 5 Input : 98562 Output : First digit: 9 last digit : 242
Input: N = 73
Output: 10
79=
Input: N = 73
Output: 10
60Input: N = 73
Output: 10
28O[1]
Auxiliary Space: O[1]
Addition of first and last digit of the number is 8
9Addition of first and last digit of the number is 8
02=
str
7Input: N = 73
Output: 10
24log10[] is a mathematical function present in math.h header
file. It returns log base 10 value of the passed parameter to log10[] function.