Tổng mảng PHP với điều kiện

Nói chung, mệnh đề GROUP BY và hàm SUM() được sử dụng trong cơ sở dữ liệu để sắp xếp các bản ghi. Nhóm câu lệnh GROUP BY ghi lại các giá trị giống nhau và trả về các bản ghi đã lọc. SUM() là một hàm tổng hợp được sử dụng bởi câu lệnh GROUP BY. Các hoạt động GROUP BY và SUM có thể được tích hợp vào danh sách dữ liệu ở cấp mã trong PHP

Sử dụng hàm PHP array_reduce() để GROUP BY và SUM giá trị của một mảng trong PHP. Trong ví dụ này, chúng tôi sẽ chỉ cho bạn cách nhóm mảng theo khóa và giá trị tổng bằng cách sử dụng PHP. Nó giúp tính tổng các giá trị của một mảng có cùng khóa trong PHP

Trong đoạn mã sau, chúng ta sẽ nhóm mảng theo giá trị category_id và tính tổng các giá trị score bằng PHP

Bạn được cho 2 số nguyên N và M (M ≤ N), nhiệm vụ là tạo một mảng có độ dài N theo điều kiện cho bên dưới

  • Ái. = i, Chính thức, A[i] sẽ là -i hoặc i
  • Chính xác M mảng tiền tố phải ở đó, sao cho tổng của mỗi mảng tiền tố phải lớn hơn 0.  

Ghi chú. Nếu có nhiều cách sắp xếp thì xuất bất kỳ cách sắp xếp hợp lệ nào

ví dụ

Đầu vào. N = 3, M = 3
Đầu ra. 1 2 3
Giải thích. Có chính xác 3 mảng tiền tố có tổng lớn hơn 0, đó là.
Mảng tiền tố đầu tiên[]. {1}, Sum=1 
Mảng tiền tố thứ hai[] 1. {1, 2}, Sum=1+2=3
Mảng tiền tố thứ ba[] = {1, 2, 3}, Sum=1+2+3=6 
It can be verified that the output arrangement follows all the constraints provided in the problem statement.

Đầu vào. N = 4, M = 2 
Đầu ra. 1 -2 3 -4 
Giải thích. Có đúng 2 mảng tiền tố có tổng lớn hơn 0, đó là {1} và {1, -2, 3} có tổng lần lượt là 1 và 2.

Cách tiếp cận. Vấn đề có thể được giải quyết dựa trên quan sát sau đây

Cố gắng giữ các phần tử ở vị trí lẻ là dương và các phần tử ở vị trí chẵn là âm và duy trì số tiền tố dương (Nếu số lượng tiền tố tổng dương được yêu cầu nhiều nhất là N/2). Mặt khác, đặt các phần tử dương cho đến khi số tiền tố dương được yêu cầu trở nên nhỏ hơn kích thước mảng còn lại.  

Thực hiện theo các bước sau để giải quyết vấn đề

  • Tạo biến đếm kiểu StringBuilder ans và int để sắp xếp
  • Chạy một vòng lặp từ i = 1 đến N và làm theo các bước được đề cập bên dưới trong phạm vi của vòng lặp
    • Nếu M tối đa là N/2
      • Nếu tôi là số lẻ và đếm < M, hãy thêm i vào ans và tăng bộ đếm khác, hãy thêm -i
  • Nếu không, hãy làm như sau
    • Nếu tôi là số lẻ và đếm < N-M nối thêm -i trong ans và bộ đếm tăng khác nối thêm i
  • In và

Dưới đây là việc thực hiện các phương pháp trên

C++




// C++ code to implement the approach

#include

using namespace std;

 

string solve(int l,int // C++ code to implement the approach0

// C++ code to implement the approach1

// C++ code to implement the approach2____23

// C++ code to implement the approach2int // C++ code to implement the approach6

 

// C++ code to implement the approach2____28

// C++ code to implement the approach2______30#include 1____32

 

// C++ code to implement the approach2____34

// C++ code to implement the approach2#include 6 #include 7int #include 9

using0using1 using2

using3using1 using5

using6using7____48#include 2

using6namespace1

using3namespace3

using3namespace5

using6namespace7using8#include 2

using0namespace3

using0namespace5 // C++ code to implement the approach1

using3using1 std;7

using6namespace7using8#include 2

using6namespace1

using3namespace3

using3namespace5

using6using7____48#include 2

using0namespace3

// C++ code to implement the approach2____53

 

// C++ code to implement the approach2int7 int8

namespace3

 

l,0

int l,2

// C++ code to implement the approach1

// C++ code to implement the approach2l,5

// C++ code to implement the approach2int l,8

// C++ code to implement the approach2int int1

 

// C++ code to implement the approach2int3

 

// C++ code to implement the approach2int7 int6

namespace3

 

int8

Java




int9

 

// C++ code to implement the approach00 // C++ code to implement the approach01

// C++ code to implement the approach00 // C++ code to implement the approach03

// C++ code to implement the approach00 // C++ code to implement the approach05

 

________ 206 ________ 207

 

// C++ code to implement the approach2______209 // C++ code to implement the approach10 // C++ code to implement the approach11int l,int // C++ code to implement the approach0

// C++ code to implement the approach2// C++ code to implement the approach1

using0// C++ code to implement the approach3

using0int // C++ code to implement the approach22// C++ code to implement the approach23#include 2

 

using0// C++ code to implement the approach26

using0// C++ code to implement the approach28// C++ code to implement the approach29 // C++ code to implement the approach30

 

using0#include 4

using0#include 6 #include 7int // C++ code to implement the approach37// C++ code to implement the approach38// C++ code to implement the approach39

using3using1 // C++ code to implement the approach42// C++ code to implement the approach43// C++ code to implement the approach44

using6using1 // C++ code to implement the approach47// C++ code to implement the approach43 // C++ code to implement the approach49// C++ code to implement the approach38 // C++ code to implement the approach51

// C++ code to implement the approach52// C++ code to implement the approach53____48// C++ code to implement the approach55

// C++ code to implement the approach52namespace1

using6namespace3

using6namespace5

// C++ code to implement the approach52// C++ code to implement the approach63____238 // C++ code to implement the approach65using8// C++ code to implement the approach55

using3namespace3

using3namespace5 // C++ code to implement the approach1

using6using1 // C++ code to implement the approach47// C++ code to implement the approach43 // C++ code to implement the approach49// C++ code to implement the approach38 // C++ code to implement the approach79

// C++ code to implement the approach52// C++ code to implement the approach63____238 // C++ code to implement the approach65using8// C++ code to implement the approach55

// C++ code to implement the approach52namespace1

using6namespace3

using6namespace5

// C++ code to implement the approach52// C++ code to implement the approach53____48// C++ code to implement the approach55

using3namespace3

using0namespace3

 

using0int7 #include 02

// C++ code to implement the approach2____53

 

// C++ code to implement the approach2l,0

// C++ code to implement the approach2______209 // C++ code to implement the approach10 #include 10 #include 11

// C++ code to implement the approach2// C++ code to implement the approach1

using0l,5

using0int #include 18#include 19#include 2

using0int #include 23// C++ code to implement the approach43#include 2

 

using0#include 27

// C++ code to implement the approach2____53

namespace3

Python3




#include 31

 

 

#include 32 #include 33

 

// C++ code to implement the approach2____335

// C++ code to implement the approach2______337#include 38 // C++ code to implement the approach23

 

// C++ code to implement the approach2____341

// C++ code to implement the approach2______343#include 38 #include 1

 

// C++ code to implement the approach2____347

// C++ code to implement the approach2______36 #include 50#include 51 #include 52#include 7// C++ code to implement the approach38#include 55#include 56 // C++ code to implement the approach38#include 58

using0using1 #include 61#include 38 #include 63#include 64#include 64 // C++ code to implement the approach43#include 58

using3using1 #include 70_______371 // C++ code to implement the approach43 #include 38#include 38 // C++ code to implement the approach38 #include 76 #include 77

using6#include 43#include 56#include 38 #include 82#include 7____400// C++ code to implement the approach38 using02 using03#include 56 using8

Làm cách nào để tính tổng các phần tử của một mảng trong PHP?

Hàm array_sum() trả về tổng của tất cả các giá trị trong mảng.

Làm cách nào để tính tổng các giá trị của một mảng có cùng khóa trong PHP?

Để tính tổng các giá trị của một khóa cụ thể (chẳng hạn như qty ) trong mảng, bạn có thể sử dụng bất kỳ cách nào sau đây. .
vòng lặp foreach;
mảng_tổng() và mảng_cột() ;
array_sum() và array_map() ;
mảng_reduce()

Làm cách nào để thêm một số vào một mảng trong PHP?

Hàm array_push() chèn một hoặc nhiều phần tử vào cuối mảng . Mẹo. Bạn có thể thêm một giá trị hoặc bao nhiêu tùy thích. Ghi chú. Ngay cả khi mảng của bạn có các phím chuỗi, các phần tử được thêm vào của bạn sẽ luôn có các phím số (Xem ví dụ bên dưới).

Làm cách nào để tính tổng mảng nhiều chiều trong PHP?

Để tổng hợp tất cả các giá trị trong một mảng nhiều chiều nằm dưới một khóa cụ thể, chúng tôi sử dụng hai hàm mảng. Đầu tiên, chúng ta gọi hàm array_column() , hàm này trả về cho chúng ta một mảng gồm tất cả các giá trị được tìm thấy cho một khóa cụ thể. Thứ hai, chúng ta sử dụng phương thức array_sum() để tính tổng tất cả các giá trị đó .