Làm thế nào để bạn tăng một phần tử mảng trong python?

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

Làm thế nào để bạn tăng một phần tử mảng trong python?

Đầ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

  1. Bắt đầu
  2. khai báo mảng
  3. Khởi tạo mảng
  4. Khai báo một biến sẽ lưu trữ kích thước của mảng
  5. Hiển thị mảng gốc trước
  6. Sử dụng vòng lặp for để duyệt qua tất cả các phần tử
  7. Bây giờ, tăng từng giá trị một và lưu trữ nó trong mảng
  8. 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
  9. 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 4

    Khuyế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 approach0 // C++ implementation of the approach1_______20 // C++ implementation of the approach3

    // C++ implementation of the approach4

    // C++ implementation of the approach5// C++ implementation of the approach6 // C++ implementation of the approach7_______449 using50using49 using52using43#include 3using49using56

    Cho 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à. 2

    Khuyế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

    1. 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[
    2. 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
    3. 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]
    4. 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ệt

    Phươ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

    1. 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
    2. 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
    3. 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
    4. Đối với các phần tử còn lại, Cập nhật A[i] theo (A[i-1] + D[i])
    5. 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 approach7

     

    #include

    using namespace std;

     

    #include 2

    #include 3

    ________ 34 ________ 35 ________ 270

    // C++ implementation of the approach71#include 4#include 5// C++ implementation of the approach74

    // C++ implementation of the approach75

    // C++ implementation of the approach71#include 5 // C++ implementation of the approach78

     

    // C++ implementation of the approach71#include 0

    // C++ implementation of the approach71#include 4#include 5#include 4

     

    // C++ implementation of the approach71#include 6

     

    // C++ implementation of the approach71#include 8

    // C++ implementation of the approach71using0 using1#include 5 using3

    using4using5

     

    // C++ implementation of the approach71using7

    // C++ implementation of the approach71using9 namespace0

    namespace1

     

    namespace2

    namespace3

    namespace4 namespace5_______35______57#include 5 namespace9

    std;0#include 5 std;2#include 5 std;4

    // C++ implementation of the approach75

    // C++ implementation of the approach71std;7

    // C++ implementation of the approach71std;9

    // C++ implementation of the approach71#include 21

    namespace1

     

    #include 23

    #include 24

    namespace4 #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 approach75

    // C++ implementation of the approach71#include 41

    // C++ implementation of the approach71#include 43

    // C++ implementation of the approach71#include 45

    #include 46#include 47

    namespace1

     

    #include 49

    namespace4 #include 51____35#include 28

    #include 54#include 5 #include 56#include 5 #include 58

    // C++ implementation of the approach75

    // C++ implementation of the approach71// C++ implementation of the approach701

    // C++ implementation of the approach71using0 using1#include 5 // C++ implementation of the approach706

     

    using4// C++ implementation of the approach708

    _______44____2710

    // C++ implementation of the approach711// C++ implementation of the approach712

    // C++ implementation of the approach711// C++ implementation of the approach714

    // C++ implementation of the approach71namespace1

    namespace1

     

    // C++ implementation of the approach718

    // C++ implementation of the approach719

    namespace4 // C++ implementation of the approach721____35____2723

    // C++ implementation of the approach724#include 4#include 5// C++ implementation of the approach727

    // C++ implementation of the approach75

    // C++ implementation of the approach71____2730

    // C++ implementation of the approach71using0 using1#include 5 // C++ implementation of the approach735

     

    using4// C++ implementation of the approach737

    using4// C++ implementation of the approach739 // C++ implementation of the approach740

    std;0// C++ implementation of the approach742

    using4namespace1

     

    _______44____2746

    using4____2748

    using4// C++ implementation of the approach750 // C++ implementation of the approach75

    std;0// C++ implementation of the approach753

    using4namespace1

    // C++ implementation of the approach71namespace1

    namespace1

     

    // C++ implementation of the approach759

    ________ 54 ________ 2761 ________ 35 ________ 274

    // C++ implementation of the approach75

    // C++ implementation of the approach71// C++ implementation of the approach766

    // C++ implementation of the approach71using0 using1#include 5 // C++ implementation of the approach735

    using4// C++ implementation of the approach773______2774// C++ implementation of the approach775

    // C++ implementation of the approach71namespace1

     

    // C++ implementation of the approach71____49____2775

    namespace1

     

    // C++ implementation of the approach782

    // C++ implementation of the approach783

    ________ 54 ________ 2785 ________ 35 ________ 2723

    // C++ implementation of the approach788#include 5 #include 56______35 #include 58

    // C++ implementation of the approach75

    // C++ implementation of the approach71____2795

    // C++ implementation of the approach71____2797

    // C++ implementation of the approach71#include 4#include 5#include 01

    using4#include 03

     

    // C++ implementation of the approach71#include 05

     

    // C++ implementation of the approach71#include 07

    // C++ implementation of the approach71#include 09

    // C++ implementation of the approach71#include 11

     

    // C++ implementation of the approach71____313

    // C++ implementation of the approach71#include 15

    // C++ implementation of the approach71#include 17

    namespace1

     

    #include 19

    #include 5 #include 21

    // C++ implementation of the approach75

    // C++ implementation of the approach71#include 24

    // C++ implementation of the approach71#include 5 #include 27

     

    // C++ implementation of the approach71#include 29

    // C++ implementation of the approach71#include 4#include 5#include 33

     

    // C++ implementation of the approach71____335

    // C++ implementation of the approach71#include 5 #include 38

    #include 39#include 40

    #include 39#include 42

     

    // C++ implementation of the approach71#include 44

    // C++ implementation of the approach71#include 46

     

    // C++ implementation of the approach71using9 #include 49

    namespace1

    Java




    #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 approach775

    #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 60 namespace4 #include 89

    // C++ implementation of the approach75

    #include 57#include 92____393#include 94#include 93#include 82

    _______357____398____393// C++ implementation of the approach775

     

    #include 57#include 8

    #include 57using0 using1#include 5 using07#include 81using09

    _______271____411____381#include 82

    namespace1

     

    using15

    using16

    #include 60 namespace4 using19#include 5 namespace9

    #include 39#include 5 std;2______35 std;4

    // C++ implementation of the approach75

    #include 57____429

    #include 57using31

    #include 57std;9

    _______357____435____381using37

    namespace1

     

    #include 23

    #include 24

    #include 60 namespace4 using43#include 5 #include 31#include 5 #include 33

    using48______35 #include 36______35 #include 38

    // C++ implementation of the approach75

    #include 57#include 41

    #include 57#include 43

    _______357____459____381using61

    using62____463____381using65

    namespace1

     

    #include 49

    #include 60 namespace4 using70#include 5 using72

    // C++ implementation of the approach75

    #include 57// C++ implementation of the approach701

    #include 57using0 using1#include 5 using07#include 93using82

    #include 57// C++ implementation of the approach75

    // C++ implementation of the approach71____2708

    _______271____488____393____490#include 81using92

    using93____494____495using90#include 63using98

    #include 57namespace1

    namespace1

     

    // C++ implementation of the approach718

    // C++ implementation of the approach719

    #include 60 namespace4 namespace06

    // C++ implementation of the approach75

    #include 57// C++ implementation of the approach730

    #include 57using0 using1#include 5 using07#include 93using09

    #include 57// C++ implementation of the approach75

    // C++ implementation of the approach71____2737

    // C++ implementation of the approach71// C++ implementation of the approach739 namespace23____393namespace25

    // C++ implementation of the approach71// C++ implementation of the approach75

    _______528____2742

    // C++ implementation of the approach71namespace1

     

    // C++ implementation of the approach71____533

    // C++ implementation of the approach71____535

    // C++ implementation of the approach71____537

    // C++ implementation of the approach71____2750

    // C++ implementation of the approach71// C++ implementation of the approach75

    namespace28namespace43____381#include 82

    // C++ implementation of the approach71namespace1

    #include 57namespace1

    namespace1

     

    namespace51

    namespace52

    #include 60 namespace4 namespace55

    // C++ implementation of the approach75

    #include 57// C++ implementation of the approach766

    #include 57using0 using1#include 5 using07#include 93using09

    #include 57// C++ implementation of the approach75

    // C++ implementation of the approach71____569

    #include 29#include 81 namespace72// C++ implementation of the approach774namespace74

    #include 57namespace1

    #include 57____49____2775

    namespace1

     

    // C++ implementation of the approach782

    // C++ implementation of the approach783

    #include 60 namespace4 namespace85______35 namespace87

    namespace88#include 5 namespace90#include 5 #include 58

    // C++ implementation of the approach75

    #include 57// C++ implementation of the approach795

    #include 57// C++ implementation of the approach797

    #include 57namespace99

     

    #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;15

    namespace1

     

    #include 19

    ________ 618 ________ 360 ________ 54 ________ 621

    // C++ implementation of the approach75

    #include 57#include 29

    #include 57#include 5 std;27_______381____461using95____461#include 63std;33

     

    #include 57#include 35

    #include 57#include 5 std;38#include 81______461using95using61#include 81using61std;45std;46

    std;47// C++ implementation of the approach75#include 81using61#include 63using61______495using61#include 63std;46

    std;47// C++ implementation of the approach75using95using61#include 63using61std;45using61std;65std;66

     

    #include 57#include 44

    #include 57#include 46

    namespace1

    namespace1

     

    std;73

    Python3




    std;74

     

    std;75

    std;76

    std;77 std;78

    // C++ implementation of the approach71

    // C++ implementation of the approach71std;81std;82 std;83std;84

     

    // C++ implementation of the approach71std;86

    // C++ implementation of the approach71std;88std;82 std;90#include 93std;92std;93 std;94namespace72 #include 81namespace25

     

    // C++ implementation of the approach71#include 92______393std;92std;82 #include 203#include 93std;92

    // C++ implementation of the approach71#include 207std;82 #include 93

     

    // C++ implementation of the approach71____3211

    // C++ implementation of the approach71using0 #include 214#include 215 #include 216using1#include 81#include 219

    using4#include 221std;82 #include 223#include 224 #include 225#include 224 #include 81std;92

     

    // C++ implementation of the approach71____3230

    // C++ implementation of the approach71using9 std;88

     

    #include 234

    #include 235

    std;77 #include 237

    // C++ implementation of the approach71

    // C++ implementation of the approach71____3240

    // C++ implementation of the approach71____3242____572std;82 #include 245

    // C++ implementation of the approach71#include 247namespace72 #include 81____692#include 224std;82 #include 245

     

    #include 254

    #include 255

    std;77 #include 257

    using93____3259

    // C++ implementation of the approach711

    // C++ implementation of the approach71____3262

    // C++ implementation of the approach71____3264

    // C++ implementation of the approach71#include 266#include 224 #include 81using61

    #include 46______3271____3224 #include 81#include 274std;93 #include 38

     

    #include 277

    std;77 #include 279

    // C++ implementation of the approach71

    // C++ implementation of the approach71____3282

    // C++ implementation of the approach71using0 #include 214#include 215 #include 216#include 288

     

    using4____3290

    _______44____3292____393using92

    #include 295using94#include 81using90using95using92

    _______3295____494____363#include 304

     

    #include 305

    #include 306

    std;77 #include 308

    // C++ implementation of the approach71

    // C++ implementation of the approach71____3311

    // C++ implementation of the approach71using0 #include 214#include 215 #include 216using1std;83#include 319

     

    _______44____3321

    using4// C++ implementation of the approach739 #include 324std;82std;82 #include 93#include 328

    std;0#include 223____682 #include 221

     

    _______44____3334

    using4#include 336

    _______44____2750____3339

    std;0#include 223std;82 #include 221namespace72 #include 225#include 224 #include 81std;92

     

    #include 349

    std;77 #include 351

    // C++ implementation of the approach71

    // C++ implementation of the approach71____3354

    // C++ implementation of the approach71using0 #include 214#include 215 #include 216using1std;83#include 319

    using4#include 364____3365std;82 // C++ implementation of the approach774namespace25

    using4

    // C++ implementation of the approach71using9

     

    #include 372

    #include 373

    std;77 #include 375

    // C++ implementation of the approach71

    // C++ implementation of the approach71____3378

    // C++ implementation of the approach71____3380

    // C++ implementation of the approach71#include 382std;82 #include 384

     

    // C++ implementation of the approach71____3386

     

    // C++ implementation of the approach71____3388

    // C++ implementation of the approach71____3390

    // C++ implementation of the approach71____3392

     

    // C++ implementation of the approach71____3394

    // C++ implementation of the approach71____3396

    // C++ implementation of the approach71____3398

     

    #include 399

    // C++ implementation of the approach739 #include 401std;82std;82 #include 404#include 339

    // C++ implementation of the approach71

    // C++ implementation of the approach71____3408

    ________ 271 ________ 681 ________ 682 ________ 363

    // C++ implementation of the approach71#include 414std;82 #include 63

     

    // C++ implementation of the approach71____3418

    // C++ implementation of the approach71#include 420std;82 std;90#include 81using61using95using61#include 63 std;92

     

    // C++ implementation of the approach71____3430

    // C++ implementation of the approach71#include 432std;82 #include 434#include 81using61using95using61#include 81using61std;45 using92

    #include 443std;90using95using61#include 63using61std;45using61std;65 #include 462

    Có 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ó.