Const n=8; var a:array[1..n,1..n] of integer; i,j,k,s,s1,si,dmin,smin:integer; begin Randomize; writeln('Исходный массив:'); for i:=1 to n do begin for j:=1 to n do begin a[i,j]:=random(50); write(a[i,j]:4); end; writeln; end; write('k = '); readln(k); s:=0; for j:=1 to n do s:=s+a[k,j]; writeln('s = ',s); dmin:=999999; smin:=999999; for i:=1 to n do if i<>k then begin s1:=0; for j:=1 to n do s1:=s1+a[i,j]; writeln('s',i,' = ',s1); if abs(s1-s)<dmin then begin dmin:=abs(s1-s); smin:=s1; si:=i; end; end; writeln('Номер строки = ',si,', smin = ',smin); end.
var
a:array[1..n,1..n] of integer;
i,j,k,s,s1,si,dmin,smin:integer;
begin
Randomize;
writeln('Исходный массив:');
for i:=1 to n do
begin
for j:=1 to n do
begin
a[i,j]:=random(50);
write(a[i,j]:4);
end;
writeln;
end;
write('k = '); readln(k);
s:=0;
for j:=1 to n do s:=s+a[k,j];
writeln('s = ',s);
dmin:=999999; smin:=999999;
for i:=1 to n do
if i<>k then
begin
s1:=0;
for j:=1 to n do s1:=s1+a[i,j];
writeln('s',i,' = ',s1);
if abs(s1-s)<dmin then begin dmin:=abs(s1-s); smin:=s1; si:=i; end;
end;
writeln('Номер строки = ',si,', smin = ',smin);
end.
Пример:
Исходный массив:
9 0 22 40 20 35 2 25
23 30 22 35 41 0 9 40
1 15 6 18 43 47 34 33
26 5 2 45 13 46 40 2
26 39 7 31 3 43 20 8
25 15 24 6 10 16 3 25
47 0 27 35 14 15 36 11
16 38 14 14 33 7 11 26
k = 5
s = 177
s1 = 153
s2 = 200
s3 = 197
s4 = 179
s6 = 124
s7 = 185
s8 = 159
Номер строки = 4, smin = 179
Вот, нашел на одном из сайтов: #include <iostream>
#include <conio.h>
#include <cstdlib>
#include <ctime>
using namespace std;
int main (){
srand(time(0));
int n, one, two, co=0, ol, k=0, var, sum1=0, sum2=0;
bool kto=true;
cout << "Privet davaii sigraem v kosti.\nPobedit tot u kogo suma 3 broskov budet bolshe\n\n";
while (k<3)
{
cout << "\n1 - Brositj kubik\n2 - Vijti\nViberi variant: ";
cin >> var;
if (var==1)
{
for (int j=0; j<=1; j++)
{
one=1+(rand()%6);
two=1+(rand()%6);
cout << "\n";
if (kto) cout << "\tTvoi kubiki\n";
if (!kto) cout << "\tMoi kubiki\n";
cout << "\n";
for (int i=1; i<=5; i++)
{
if (i==1) n=1;
else if (i==2 && one==1) n=2;
else if (i==2 && one==3 || i==2 && one==2) n=3;
else if (i==2 && one==4 || i==2 && one==5 || i==2 && one==6) n=4;
else if (i==3 && one==3 || i==3 && one==1 || i==3 && one==5) n=5;
else if (i==3 && one==2 || i==3 && one==4) n=2;
else if (i==3 && one==6) n=4;
else if (i==4 && one==1) n=2;
else if (i==4 && one==3 || i==4 && one==2) n=6;
else if (i==4 && one==4 || i==4 && one==5 || i==4 && one==6) n=4;
else if (i==5) n=7;
switch (n){
case 1:
cout << (char)201 << (char)205 << (char)205 << (char)205 << (char)205 << (char)205 << (char)205 << (char)205 << (char)187;
break;
case 2:
cout << (char)186 << " " << (char)186;
break;
case 3:
cout << (char)186 << " o " << (char)186;
break;
case 4:
cout << (char)186 << " o o " << (char)186;
break;
case 5:
cout << (char)186 << " o " << (char)186;
break;
case 6:
cout << (char)186 << " o " << (char)186;
break;
case 7:
cout << (char)200 << (char)205 << (char)205 << (char)205 << (char)205 << (char)205 << (char)205 << (char)205 << (char)188;
break;
}
if (co%2==0) {
cout << "\t";
i--;
ol=one;
one=two;}
else {
cout << endl;
one=ol; }
co++;
}
if (kto){
sum1=sum1+one+two;
kto=false;
continue;
}
if(!kto){
sum2=sum2+one+two;
kto=true;
}
}
}
else if (var==2) return 0;
else {
cout << "\n!!! Viberi variant 1 ili 2 !!!\n";
continue;}
k++;
}
cout << "\nTvoj rezultat = " << sum1 << endl << "Moj rezultat = " << sum2 << endl;
if (sum1>sum2)
cout << "\n\tTi pobedil ;)";
else cout << "\n\tTi proigral :(";
getch ();
}