Trong hướng dẫn này, chúng ta sẽ tìm hiểu cách tăng từng phần tử của một mảng và sau đó in mảng đã tăng. Nhưng trước khi tiếp tục, nếu bạn chưa quen với các khái niệm về mảng, thì hãy xem bài viết Mảng trong Java
Đầu vào. Nhập các phần tử mảng. 34 52 67 87 98 12 79 54 89
đầu ra. 35 53 68 88 99 13 80 55 90
chương trình 1. Tăng từng phần tử của mảng lên một và sau đó in mảng tăng
Trong trường hợp này, chúng ta sẽ xem cách tăng từng phần tử một và sau đó in phần tử được cập nhật khi các giá trị được xác định trước. Được xác định trước có nghĩa là các giá trị đã được xác định trong chương trình
thuật toán
- Bắt đầu
- khai báo mảng
- Khởi tạo mảng
- Khai báo một biến sẽ lưu trữ kích thước của mảng
- Hiển thị mảng gốc trước
- Sử dụng vòng lặp for để duyệt qua tất cả các phần tử
- Bây giờ, tăng từng giá trị một và lưu trữ nó trong mảng
- Một lần nữa, bằng cách sử dụng vòng lặp for hiển thị tất cả các phần tử của mảng được cập nhật
- Dừng lại
Dưới đây là mã cho cùng
Chương trình dưới đây trình bày cách tăng từng phần tử một và sau đó in phần tử được cập nhật khi các giá trị do người dùng xác định
Khi được yêu cầu tăng phần tử cuối cùng lên 1 khi nhập giá trị thập phân, một phương thức có tên 'increment_num' được định nghĩa để kiểm tra xem phần tử cuối cùng trong danh sách có nhỏ hơn 9 không. Tùy thuộc vào điều này, các hoạt động được thực hiện trên các phần tử của danh sách
Thí dụ
Dưới đây là một cuộc biểu tình của cùng một
def increment_num[my_list, n]: i = n if[my_list[i] < 9]: my_list[i] = my_list[i] + 1 return my_list[i] = 0 i -= 1 increment_num[my_list, i] n = 4 my_list = [0, 1, 4, 7, 9] print["The list is :"] print[my_list] increment_num[my_list, n] if[my_list[0] > 0]: print[my_list[0], ", "] print["After incrementing, the list is :"] print[my_list]
đầu ra
The list is : [0, 1, 4, 7, 9] After incrementing, the list is : [0, 1, 4, 8, 0]
Giải trình
Một phương thức có tên 'increment_num' được xác định lấy danh sách và một số làm tham số
Phần tử cuối cùng trong danh sách được kiểm tra nhỏ hơn 9
Nếu có, phần tử cuối cùng được tăng thêm 1 và được trả về dưới dạng đầu ra
Mặt khác, nếu phần tử cuối cùng là 0, chỉ mục sẽ giảm đi 1 và phương thức 'increment_num' được gọi lại
Cho một mảng arr[], nhiệm vụ là tăng tất cả các phần tử ở vị trí lẻ lên 1 và giảm tất cả các phần tử ở vị trí chẵn đi 1
ví dụ.
Đầu vào. mảng[] = {3, 6, 8}
Đầu ra. 4 5 9Đầu vào. mảng[] = {9, 7, 3}
Đầu ra. 10 6 4Khuyến khích. Vui lòng thử cách tiếp cận của bạn trên {IDE} trước, trước khi chuyển sang giải pháp
Cách tiếp cận. Duyệt qua phần tử mảng theo từng phần tử và nếu vị trí của phần tử hiện tại là số lẻ thì tăng nó lên 1, nếu không thì giảm nó đi 1. In nội dung của mảng được cập nhật cuối cùng
Dưới đây là việc thực hiện các phương pháp trên.
C++
// C++ implementation of the approach
#include
using
namespace
std;
// Utility function to print
________số 8
void
The list is : [0, 1, 4, 7, 9] After incrementing, the list is : [0, 1, 4, 8, 0]
0// C++ implementation of the approach
0// C++ implementation of the approach
1_______20// C++ implementation of the approach
3// C++ implementation of the approach
4// C++ implementation of the approach
5// C++ implementation of the approach
6// C++ implementation of the approach
7_______449using
50using
49using
52using
43#include
3using
49using
56Cho một mảng, arr[] gồm N số nguyên dương và M truy vấn có dạng {a, b, val, f}. Nhiệm vụ là in mảng sau khi thực hiện mỗi truy vấn để tăng các phần tử mảng trong khoảng [a, b] lên một giá trị val f số lần
ví dụ
Đầu vào. mảng[] = {1, 2, 3}, M=3, Q[][] = {{1, 2, 1, 4}, {1, 3, 2, 3}, {2, 3, 4, . 11 32 29
Output: 11 32 29
Giải thích.
Sau khi áp dụng Truy vấn đầu tiên 4 lần,
Mảng sẽ là. 5 6 3
Sau khi áp dụng Truy vấn thứ 2 3 lần,
Mảng sẽ là. 11 12 9
Sau khi áp dụng Truy vấn thứ 3 5 lần,
Mảng sẽ là. 11 32 29
Do đó, mảng cuối cùng sẽ là {11, 32, 29}.Đầu vào. mảng[] = {1}, M = 1, Q[][] = {{1, 1, 1, 1}}
Đầu ra. 2
Giải thích.
Sau khi áp dụng truy vấn đầu tiên và duy nhất 1 lần duy nhất.
Mảng sẽ là. 2Khuyến khích. Vui lòng thử cách tiếp cận của bạn trên {IDE} trước, trước khi chuyển sang giải pháp
Cách tiếp cận ngây thơ. Cách tiếp cận đơn giản nhất là thực hiện từng truy vấn trên mảng đã cho i. e. , với mỗi truy vấn {a, b, val, f} duyệt qua mảng trong phạm vi [a, b] và tăng từng phần tử theo giá trị val lên f số lần. In mảng sau khi thực hiện mỗi truy vấn
Độ phức tạp về thời gian. O[N * M * max[Tần số]]
Không gian phụ. Ô[1]Cách tiếp cận tốt hơn. Ý tưởng dựa trên mảng khác biệt có thể được sử dụng trong các hoạt động Cập nhật phạm vi. Dưới đây là các bước
- Tìm mảng khác biệt D[] của một mảng đã cho A[] được định nghĩa là D[i] = [A[i] – A[i – 1]] [0 < i < N] và D[0] = A[
- Thêm val vào D[a – 1] và trừ nó khỏi D[[b – 1] + 1], i. e. , D[a – 1] += val, D[[b – 1] + 1] -= val. Thực hiện thao tác này Tần suất số lần
- Bây giờ hãy cập nhật mảng đã cho bằng cách sử dụng mảng khác biệt. Cập nhật A[0] thành D[0] và in ra. Đối với các phần tử còn lại, hãy thực hiện A[i] = A[i-1] + D[i]
- In mảng kết quả sau các bước trên
Độ phức tạp về thời gian. O[N + M * max[Tần số]]
Không gian phụ. O[N] Không gian bổ sung cho Mảng khác biệtPhương pháp tiếp cận hiệu quả. Cách tiếp cận này tương tự như cách tiếp cận trước nhưng là phiên bản mở rộng của ứng dụng mảng khác biệt. Trước đây, nhiệm vụ là cập nhật các giá trị từ chỉ số a đến b theo số lần val, f. Ở đây thay vì gọi hàm cập nhật phạm vi f nhiều lần, chỉ gọi nó một lần cho mỗi truy vấn
- Cập nhật giá trị từ chỉ số a đến b theo val*f, chỉ 1 lần cho mỗi truy vấn
- Cộng val*f vào D[a – 1] và trừ nó khỏi D[[b – 1] + 1], i. e. , tăng D[a – 1] lên val*f, và giảm D[b] xuống val*f
- Bây giờ hãy cập nhật mảng chính bằng cách sử dụng mảng khác biệt. Cập nhật A[0] thành D[0] và in ra
- Đối với các phần tử còn lại, Cập nhật A[i] theo [A[i-1] + D[i]]
- In mảng kết quả sau các bước trên
Dưới đây là việc thực hiện các phương pháp trên.
C++
// C++ implementation of the approach
7#include
using
namespace
std;
#include
2#include
3________ 34 ________ 35 ________ 270
// C++ implementation of the approach
71#include
4#include
5// C++ implementation of the approach
74// C++ implementation of the approach
75// C++ implementation of the approach
71#include
5// C++ implementation of the approach
78// C++ implementation of the approach
71#include
0// C++ implementation of the approach
71#include
4#include
5#include
4// C++ implementation of the approach
71#include
6// C++ implementation of the approach
71#include
8// C++ implementation of the approach
71using
0using
1#include
5using
3using
4using
5// C++ implementation of the approach
71using
7// C++ implementation of the approach
71using
9namespace
0namespace
1namespace
2namespace
3namespace
4namespace
5_______35______57#include
5namespace
9std;
0#include
5std;
2#include
5std;
4// C++ implementation of the approach
75// C++ implementation of the approach
71std;
7// C++ implementation of the approach
71std;
9// C++ implementation of the approach
71#include
21namespace
1#include
23#include
24namespace
4#include
26____35____328#include
29#include
5#include
31______35#include
33#include
29#include
5#include
36______35#include
38// C++ implementation of the approach
75// C++ implementation of the approach
71#include
41// C++ implementation of the approach
71#include
43// C++ implementation of the approach
71#include
45#include
46#include
47namespace
1#include
49namespace
4#include
51____35#include
28#include
54#include
5#include
56#include
5#include
58// C++ implementation of the approach
75// C++ implementation of the approach
71// C++ implementation of the approach
701// C++ implementation of the approach
71using
0using
1#include
5// C++ implementation of the approach
706using
4// C++ implementation of the approach
708_______44____2710
// C++ implementation of the approach
711// C++ implementation of the approach
712// C++ implementation of the approach
711// C++ implementation of the approach
714// C++ implementation of the approach
71namespace
1namespace
1// C++ implementation of the approach
718// C++ implementation of the approach
719namespace
4// C++ implementation of the approach
721____35____2723// C++ implementation of the approach
724#include
4#include
5// C++ implementation of the approach
727// C++ implementation of the approach
75// C++ implementation of the approach
71____2730// C++ implementation of the approach
71using
0using
1#include
5// C++ implementation of the approach
735using
4// C++ implementation of the approach
737using
4// C++ implementation of the approach
739// C++ implementation of the approach
740std;
0// C++ implementation of the approach
742using
4namespace
1_______44____2746
using
4____2748using
4// C++ implementation of the approach
750// C++ implementation of the approach
75std;
0// C++ implementation of the approach
753using
4namespace
1// C++ implementation of the approach
71namespace
1namespace
1// C++ implementation of the approach
759________ 54 ________ 2761 ________ 35 ________ 274
// C++ implementation of the approach
75// C++ implementation of the approach
71// C++ implementation of the approach
766// C++ implementation of the approach
71using
0using
1#include
5// C++ implementation of the approach
735using
4// C++ implementation of the approach
773______2774// C++ implementation of the approach
775// C++ implementation of the approach
71namespace
1// C++ implementation of the approach
71____49____2775namespace
1// C++ implementation of the approach
782// C++ implementation of the approach
783________ 54 ________ 2785 ________ 35 ________ 2723
// C++ implementation of the approach
788#include
5#include
56______35#include
58// C++ implementation of the approach
75// C++ implementation of the approach
71____2795// C++ implementation of the approach
71____2797// C++ implementation of the approach
71#include
4#include
5#include
01using
4#include
03// C++ implementation of the approach
71#include
05// C++ implementation of the approach
71#include
07// C++ implementation of the approach
71#include
09// C++ implementation of the approach
71#include
11// C++ implementation of the approach
71____313// C++ implementation of the approach
71#include
15// C++ implementation of the approach
71#include
17namespace
1#include
19#include
5#include
21// C++ implementation of the approach
75// C++ implementation of the approach
71#include
24// C++ implementation of the approach
71#include
5#include
27// C++ implementation of the approach
71#include
29// C++ implementation of the approach
71#include
4#include
5#include
33// C++ implementation of the approach
71____335// C++ implementation of the approach
71#include
5#include
38#include
39#include
40#include
39#include
42// C++ implementation of the approach
71#include
44// C++ implementation of the approach
71#include
46// C++ implementation of the approach
71using
9#include
49namespace
1Java
#include
51#include
52#include
53#include
54#include
55#include
56#include
57#include
58#include
59#include
60#include
5#include
62#include
63#include
64#include
63// C++ implementation of the approach
775#include
57#include
60#include
5#include
70#include
71#include
5#include
73#include
74#include
60#include
5#include
77#include
71#include
5#include
80#include
81#include
82#include
57#include
84#include
85#include
86#include
60namespace
4#include
89// C++ implementation of the approach
75#include
57#include
92____393#include
94#include
93#include
82_______357____398____393
// C++ implementation of the approach
775#include
57#include
8#include
57using
0using
1#include
5using
07#include
81using
09_______271____411____381
#include
82namespace
1using
15using
16#include
60namespace
4using
19#include
5namespace
9#include
39#include
5std;
2______35std;
4// C++ implementation of the approach
75#include
57____429#include
57using
31#include
57std;
9_______357____435____381
using
37namespace
1#include
23#include
24#include
60namespace
4using
43#include
5#include
31#include
5#include
33using
48______35#include
36______35#include
38// C++ implementation of the approach
75#include
57#include
41#include
57#include
43_______357____459____381
using
61using
62____463____381using
65namespace
1#include
49#include
60namespace
4using
70#include
5using
72// C++ implementation of the approach
75#include
57// C++ implementation of the approach
701#include
57using
0using
1#include
5using
07#include
93using
82#include
57// C++ implementation of the approach
75// C++ implementation of the approach
71____2708_______271____488____393____490
#include
81using
92using
93____494____495using
90#include
63using
98#include
57namespace
1namespace
1// C++ implementation of the approach
718// C++ implementation of the approach
719#include
60namespace
4namespace
06// C++ implementation of the approach
75#include
57// C++ implementation of the approach
730#include
57using
0using
1#include
5using
07#include
93using
09#include
57// C++ implementation of the approach
75// C++ implementation of the approach
71____2737// C++ implementation of the approach
71// C++ implementation of the approach
739namespace
23____393namespace
25// C++ implementation of the approach
71// C++ implementation of the approach
75_______528____2742
// C++ implementation of the approach
71namespace
1// C++ implementation of the approach
71____533// C++ implementation of the approach
71____535// C++ implementation of the approach
71____537// C++ implementation of the approach
71____2750// C++ implementation of the approach
71// C++ implementation of the approach
75namespace
28namespace
43____381#include
82// C++ implementation of the approach
71namespace
1#include
57namespace
1namespace
1namespace
51namespace
52#include
60namespace
4namespace
55// C++ implementation of the approach
75#include
57// C++ implementation of the approach
766#include
57using
0using
1#include
5using
07#include
93using
09#include
57// C++ implementation of the approach
75// C++ implementation of the approach
71____569#include
29#include
81namespace
72// C++ implementation of the approach
774namespace
74#include
57namespace
1#include
57____49____2775namespace
1// C++ implementation of the approach
782// C++ implementation of the approach
783#include
60namespace
4namespace
85______35namespace
87namespace
88#include
5namespace
90#include
5#include
58// C++ implementation of the approach
75#include
57// C++ implementation of the approach
795#include
57// C++ implementation of the approach
797#include
57namespace
99#include
57std;
01#include
57std;
03#include
57std;
05#include
57std;
07#include
57std;
09#include
57#include
13#include
57#include
15#include
57std;
15namespace
1#include
19________ 618 ________ 360 ________ 54 ________ 621
// C++ implementation of the approach
75#include
57#include
29#include
57#include
5std;
27_______381____461using
95____461#include
63std;
33#include
57#include
35#include
57#include
5std;
38#include
81______461using
95using
61#include
81using
61std;
45std;
46std;
47// C++ implementation of the approach
75#include
81using
61#include
63using
61______495using
61#include
63std;
46std;
47// C++ implementation of the approach
75using
95using
61#include
63using
61std;
45using
61std;
65std;
66#include
57#include
44#include
57#include
46namespace
1namespace
1std;
73Python3
std;
74std;
75std;
76std;
77std;
78// C++ implementation of the approach
71// C++ implementation of the approach
71std;
81std;
82std;
83std;
84// C++ implementation of the approach
71std;
86// C++ implementation of the approach
71std;
88std;
82std;
90#include
93std;
92std;
93std;
94namespace
72#include
81namespace
25// C++ implementation of the approach
71#include
92______393std;
92std;
82#include
203#include
93std;
92// C++ implementation of the approach
71#include
207std;
82#include
93// C++ implementation of the approach
71____3211// C++ implementation of the approach
71using
0#include
214#include
215#include
216using
1#include
81#include
219using
4#include
221std;
82#include
223#include
224#include
225#include
224#include
81std;
92// C++ implementation of the approach
71____3230// C++ implementation of the approach
71using
9std;
88#include
234#include
235std;
77#include
237// C++ implementation of the approach
71// C++ implementation of the approach
71____3240// C++ implementation of the approach
71____3242____572std;
82#include
245// C++ implementation of the approach
71#include
247namespace
72#include
81____692#include
224std;
82#include
245#include
254#include
255std;
77#include
257using
93____3259// C++ implementation of the approach
711// C++ implementation of the approach
71____3262// C++ implementation of the approach
71____3264// C++ implementation of the approach
71#include
266#include
224#include
81using
61#include
46______3271____3224#include
81#include
274std;
93#include
38#include
277std;
77#include
279// C++ implementation of the approach
71// C++ implementation of the approach
71____3282// C++ implementation of the approach
71using
0#include
214#include
215#include
216#include
288using
4____3290_______44____3292____393
using
92#include
295using
94#include
81using
90using
95using
92_______3295____494____363
#include
304#include
305#include
306std;
77#include
308// C++ implementation of the approach
71// C++ implementation of the approach
71____3311// C++ implementation of the approach
71using
0#include
214#include
215#include
216using
1std;
83#include
319_______44____3321
using
4// C++ implementation of the approach
739#include
324std;
82std;
82#include
93#include
328std;
0#include
223____682#include
221_______44____3334
using
4#include
336_______44____2750____3339
std;
0#include
223std;
82#include
221namespace
72#include
225#include
224#include
81std;
92#include
349std;
77#include
351// C++ implementation of the approach
71// C++ implementation of the approach
71____3354// C++ implementation of the approach
71using
0#include
214#include
215#include
216using
1std;
83#include
319using
4#include
364____3365std;
82// C++ implementation of the approach
774namespace
25using
4// C++ implementation of the approach
71using
9#include
372#include
373std;
77#include
375// C++ implementation of the approach
71// C++ implementation of the approach
71____3378// C++ implementation of the approach
71____3380// C++ implementation of the approach
71#include
382std;
82#include
384// C++ implementation of the approach
71____3386// C++ implementation of the approach
71____3388// C++ implementation of the approach
71____3390// C++ implementation of the approach
71____3392// C++ implementation of the approach
71____3394// C++ implementation of the approach
71____3396// C++ implementation of the approach
71____3398#include
399// C++ implementation of the approach
739#include
401std;
82std;
82#include
404#include
339// C++ implementation of the approach
71// C++ implementation of the approach
71____3408________ 271 ________ 681 ________ 682 ________ 363
// C++ implementation of the approach
71#include
414std;
82#include
63// C++ implementation of the approach
71____3418// C++ implementation of the approach
71#include
420std;
82std;
90#include
81using
61using
95using
61#include
63std;
92// C++ implementation of the approach
71____3430// C++ implementation of the approach
71#include
432std;
82#include
434#include
81using
61using
95using
61#include
81using
61std;
45using
92#include
443std;
90using
95using
61#include
63using
61std;
45using
61std;
65#include
462Có một I++ trong Python không?
Có toán tử ++ trong Python không? . Đây là một quyết định thiết kế rõ ràng của các nhà phát triển ngôn ngữ Python. No, there is no ++ operator in Python. This was a clear design decision by the developers of the Python language.Có toán tử gia tăng trong Python không?
Python không có toán tử tăng trước và sau . Cái nào sẽ gán lại b thành b+1. Đó không phải là toán tử tăng, bởi vì nó không tăng b , nó gán lại nó.