1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
| //---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
#include "Unit2.h"
#include <math.h>
#include <algorithm>
#include <iterator>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <iostream>
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
using namespace std;
typedef vector<int> set;
//Ïðîâåðêà ÿâëÿåòñÿ ëè ÷èñëî ïðîñòûì
int is_simple (int n)
{
int i;
if (n == 2)
return 1;
if (n == 0 || n == 1 || n % 2 == 0)
return 0;
for(i = 3; i*i <= n && n % i; i += 2)
;
return i*i > n;
}
/* Âû÷èñëåíèå ðàçíîñòè ñóìì ïðîñòûõ ÷èñåë
è íå ïðîñòûõ èç ìàññèâà set
*/
int card(int* set, int sz){
int nat = 0;
int simpl = 0;
//Ñóììèðóåì ÷èñëà
for(int i=0;i<sz;i++){
if (is_simple(set[i])){
simpl+=set[i];
}else{
nat+=set[i];
}
}
//Âîçâðàùàåì ðàçíîñòü ñóìì
return nat - simpl;
}
//Ïðîâåðÿåì äâà ìíîæåñòâà íà ïåðåñå÷åíèå
bool is_intersect(int* a, int sza, int* b, int szb){
for(int i= 0;i < sza;i++){
for(int j= 0;j < szb;j++){
//Åñëè ó ìíîæåñòâî åñòü îäèíàêîâûå ýëåìåíòû, òî îíè ïåðåñåêàþòñÿ
if ( a[i] == b[j]) return true;
}
}
return false;
}
//Ñîðòèðîâêà âûáîðîì
void selection_sort(int* array, int size) {
int sw;
for (int idx_i = 0; idx_i < size - 1; idx_i++) {
size_t min_idx = idx_i;
for (size_t idx_j = idx_i + 1; idx_j < size; idx_j++) {
if (array[idx_j] < array[min_idx]) {
min_idx = idx_j;
}
}
if (min_idx != idx_i) {
sw = array[idx_i];
array[idx_i] = array[min_idx];
array[min_idx] = sw;
}
}
}
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
//Êíîïêà äîáàâèòü ÷èñëî
void __fastcall TForm1::Button1Click(TObject *Sender)
{
//Ïðîâåðÿåì, ÷òî ââåäåííîå ÷èñëî êîððåêòíî
try{
if ( StrToInt(Edit1->Text) !=0 ){
ListBox1->Items->Add(Edit1->Text);
}
}catch(Exception *ex){
ShowMessage(ex->Message);
}
//Ïûòàåìñÿ ðàçäåëèòü ïîëó÷åííîå ìíîæåñòâî íà 2
Calculate();
}
//---------------------------------------------------------------------------
#include <sstream>
using namespace std;
//Ôóíêöèÿ ðàçäÿëÿåò èñõîäíîå ìíîæåñòâî íà äâà ïî óñëîâèþ çàäà÷è
TForm1::Calculate()
{
int setall_sz;
//Ñ÷èòàåì èñõîäíîå ìíîæåñòâî â ìàññèâ
int* setall = GetSet(ListBox1, setall_sz);
//Ïîñ÷èòàåì ðàçíîñòü ñóìì è âûâåäåì íà ôîðìó
int setall_cardio = card(setall, setall_sz );
TVarRec vr[] = {setall_cardio};
Label2->Caption = Format("Ðàçíîñòü ñóìì ïðîñòûõ è íå ïðîñòûõ ÷èñåë: %d ", vr ,1);
Label3->Caption = "Ðàçäåëåíèå íå óäàëîñü";
//Î÷èñòèì âûõîäíûå ñïèñêè ñî ìíîæåñòâàìè
ListBox2->Items->Clear();
ListBox3->Items->Clear();
//Åñëè èñõîäíîå ìíîæåñòâî íå÷åòíîå, òî ìû íå ìîæåì ïîëó÷èòü èç íåãî äâà ìíîæåñòâà
//ñ îäèíàêîâîé ìîùíîñòüþ
if ( setall_sz % 2 != 0 ){
Label3->Caption = "Äëÿ ðàçäåëåíèÿ ìíîæåñòâ èñõîäíîå äîëæíî áûòü ÷åòíûì";
return 0;
}
int half = setall_sz/2;
int* a= new int[ half ];
int* b= new int[ half ];
int* ra= new int[ half ];
int* rb= new int[ half ];
int m_cardio_a, m_cardio_b;
//Î÷èñòèì
memset(ra,0,half*sizeof(int));
memset(rb,0,half*sizeof(int));
//Ìèíèìóì
int minimum = 10000;
int iter = 0;
set v;
//Çàïîíèì âåêòîð çíà÷åíèÿìè ìíîæåñòâà
for (int i=0; i!=setall_sz; ++i) v.push_back(setall[i]);
//Ïîëó÷èì âñå êîìáèíàöèè çíà÷åíèé â ýòîì ìíîæåñòâå
while (next_permutation(v.begin(), v.end() ) )
{
iter++;
if ( v.size() == half*2){
//ðàçîáúåì ïîëó÷åííóþ êîìáèíàöèþ íà äâà ìíîæåñòâà
for(int i = 0 ; i < half ; i++){
a[i] = v[i];
b[i] = v[i+half];
}
//ïîñ÷èòàåì äëÿ íèõ ñîîòíîøåíèå ÷èñåë
int cardio_a = card(b, half );
int cardio_b = card(a, half );
//åñëè ìíîæåñòâà íå ïåðåñåêàþòñÿ è ðàçíîñòü ìåæäó ïîñ÷èòàííûìè ÷èñëàìè ìèíèìàëüíà
//çàïîìíèì ýòó êîìáèíàöèþ
if ( minimum > abs( cardio_a - cardio_b) && !is_intersect(a,half,b,half)){
minimum = abs( cardio_a - cardio_b);
m_cardio_a = cardio_b;
m_cardio_b = cardio_a;
memcpy(ra,a,half*sizeof(int));
memcpy(rb,b,half*sizeof(int));
}
}
}
//åñëè ìû íàøëè êàêîé òî ìèíèìóì
//âûâåäåì ïîëó÷åííûå ìíîæåñòâà â ñïèñêè
if ( minimum != 10000 ){
//îòñîðòèðóåì ìíîæåñòâà
selection_sort(ra,half);
selection_sort(rb,half);
//âûâåäåì ìíîæåñòâà íà ýêðàí
SetSet(ListBox2,ra,half);
SetSet(ListBox3,rb,half);
//âûâåäåì äîï. èíôîðìàöèþ íà ýêðàí
TVarRec vr1[] = {m_cardio_b};
Label4->Caption = Format("Card A:%d ", vr1,1 );
TVarRec vr2[] = {m_cardio_a};
Label5->Caption = Format("Card B:%d ", vr2,1 );
TVarRec vrm[] = {minimum, iter};
Label6->Caption = Format("Ìèíèìóì ñóìì:%d", vrm,2 );
Label3->Caption = "Ðàçäåëåíèå óñïåøíî";
}
delete a;
delete b;
delete ra;
delete rb;
delete setall;
}
//ôóíêöèÿ ñîçäàåò ìàññèâ ýëåìåíòîâ èç ëèñòáîêñà
int* TForm1::GetSet(TListBox* lst, int& sz)
{
sz = lst->Items->Count;
int* set = new int[ sz ];
for (int i = 0 ; i < sz ; i++){
set[i] = StrToInt(Trim(lst->Items->Strings[i]));
}
return set;
}
//ôóíêöèÿ çàïîëíÿåò ëèñòáîêñ ýëåìåíòàìè ìàññèâà
TForm1::SetSet(TListBox* lst, int* set, int sz)
{
lst->Items->Clear();
for (int i = 0 ; i < sz ; i++){
lst->Items->Add(IntToStr(set[i]));
}
}
void __fastcall TForm1::N3Click(TObject *Sender)
{
Form2->generate = 0;
Form2->ShowModal();
if ( Form2->generate!=0){
randomize();
ListBox1->Items->Clear();
for (int i=0; i< Form2->CSpinEdit1->Value;i++)
ListBox1->Items->Add( IntToStr( Form2->CSpinEdit2->Value +
random(Form2->CSpinEdit3->Value - Form2->CSpinEdit2->Value) ));
Calculate();
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::N4Click(TObject *Sender)
{
int sz;
int* set = GetSet(ListBox1,sz);
selection_sort(set,sz);
SetSet(ListBox1,set,sz);
delete set;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::N7Click(TObject *Sender)
{
Close();
}
//--------------------------------------------------------------------------- |