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 M tối đa là N/2
- 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 approach
0
// C++ code to implement the approach
1
// C++ code to implement the approach
2____23
// C++ code to implement the approach
2int
// C++ code to implement the approach
6
// C++ code to implement the approach
2____28
// C++ code to implement the approach
2______30#include
1____32
// C++ code to implement the approach
2____34
// C++ code to implement the approach
2#include
6 #include
7int
#include
9
using
0using
1 using
2
using
3using
1 using
5
using
6using
7____48#include
2
using
6namespace
1
using
3namespace
3
using
3namespace
5
using
6namespace
7using
8#include
2
using
0namespace
3
using
0namespace
5 // C++ code to implement the approach
1
using
3using
1 std;
7
using
6namespace
7using
8#include
2
using
6namespace
1
using
3namespace
3
using
3namespace
5
using
6using
7____48#include
2
using
0namespace
3
// C++ code to implement the approach
2____53
// C++ code to implement the approach
2int
7 int
8
namespace
3
l,
0
int
l,
2
// C++ code to implement the approach
1
// C++ code to implement the approach
2l,
5
// C++ code to implement the approach
2int
l,
8
// C++ code to implement the approach
2int
int
1
// C++ code to implement the approach
2int
3
// C++ code to implement the approach
2int
7 int
6
namespace
3
int
8
Java
int
9
// C++ code to implement the approach
00 // C++ code to implement the approach
01
// C++ code to implement the approach
00 // C++ code to implement the approach
03
// C++ code to implement the approach
00 // C++ code to implement the approach
05
________ 206 ________ 207
// C++ code to implement the approach
2______209 // C++ code to implement the approach
10 // C++ code to implement the approach
11int
l,
int
// C++ code to implement the approach
0
// C++ code to implement the approach
2// C++ code to implement the approach
1
using
0// C++ code to implement the approach
3
using
0int
// C++ code to implement the approach
22// C++ code to implement the approach
23#include
2
using
0// C++ code to implement the approach
26
using
0// C++ code to implement the approach
28// C++ code to implement the approach
29 // C++ code to implement the approach
30
using
0#include
4
using
0#include
6 #include
7int
// C++ code to implement the approach
37// C++ code to implement the approach
38// C++ code to implement the approach
39
using
3using
1 // C++ code to implement the approach
42// C++ code to implement the approach
43// C++ code to implement the approach
44
using
6using
1 // C++ code to implement the approach
47// C++ code to implement the approach
43 // C++ code to implement the approach
49// C++ code to implement the approach
38 // C++ code to implement the approach
51
// C++ code to implement the approach
52// C++ code to implement the approach
53____48// C++ code to implement the approach
55
// C++ code to implement the approach
52namespace
1
using
6namespace
3
using
6namespace
5
// C++ code to implement the approach
52// C++ code to implement the approach
63____238 // C++ code to implement the approach
65using
8// C++ code to implement the approach
55
using
3namespace
3
using
3namespace
5 // C++ code to implement the approach
1
using
6using
1 // C++ code to implement the approach
47// C++ code to implement the approach
43 // C++ code to implement the approach
49// C++ code to implement the approach
38 // C++ code to implement the approach
79
// C++ code to implement the approach
52// C++ code to implement the approach
63____238 // C++ code to implement the approach
65using
8// C++ code to implement the approach
55
// C++ code to implement the approach
52namespace
1
using
6namespace
3
using
6namespace
5
// C++ code to implement the approach
52// C++ code to implement the approach
53____48// C++ code to implement the approach
55
using
3namespace
3
using
0namespace
3
using
0int
7 #include
02
// C++ code to implement the approach
2____53
// C++ code to implement the approach
2l,
0
// C++ code to implement the approach
2______209 // C++ code to implement the approach
10 #include
10 #include
11
// C++ code to implement the approach
2// C++ code to implement the approach
1
using
0l,
5
using
0int
#include
18#include
19#include
2
using
0int
#include
23// C++ code to implement the approach
43#include
2
using
0#include
27
// C++ code to implement the approach
2____53
namespace
3
Python3
#include
31
#include
32 #include
33
// C++ code to implement the approach
2____335
// C++ code to implement the approach
2______337#include
38 // C++ code to implement the approach
23
// C++ code to implement the approach
2____341
// C++ code to implement the approach
2______343#include
38 #include
1
// C++ code to implement the approach
2____347
// C++ code to implement the approach
2______36 #include
50#include
51 #include
52#include
7// C++ code to implement the approach
38#include
55#include
56 // C++ code to implement the approach
38#include
58
using
0using
1 #include
61#include
38 #include
63#include
64#include
64 // C++ code to implement the approach
43#include
58
using
3using
1 #include
70_______371 // C++ code to implement the approach
43 #include
38#include
38 // C++ code to implement the approach
38 #include
76 #include
77
using
6#include
43#include
56#include
38 #include
82#include
7____400// C++ code to implement the approach
38 using
02 using
03#include
56 using
8