Подразделы

Другие разделы

Дата и время

25/04/2024 18:06:27

Авторизация

Имя:
Пароль:
Зарегистрироваться
Восстановить пароль
 

printКак некоторые "хакеры" пишут программы

hacker

2. a sloppy or inefficient computer programmer
Dictionary American Slang and Colloquial Expressions by Richard A. Spears.


Из 39 человек только трое решили по одной задаче. Довольно плачевные результаты, которые отражают:
1. неумение выбрать задачу себе по силам
Большинство решало самую сложную 2-ю задачу (на построение эйлерова пути в графе), но которая имела самую простую формулировку. Самая простой из всех была 4-я задача (вычисление площади фигуры с помощью формулы для площади трапеции), затем 1-я (на сообразительность) и 3-я (поиска пути в лабиринте – волновой алгоритм).
2. неумение отлаживать (проверять) программу
Некоторые переданные на тестирование программы дают неверный результат даже на том тесте, который указан в задаче.
3. незнание элементарных возможностей языка программирования
В большинстве задач требовалось вводить целые числа, пары чисел или набор чисел. Посмотрим, что получилось при этом.

Ввод одного числа:
var s:string;
  n:integer;
...
  readln(f,s);
  val(s,n,code);
вместо простого
  readln(f,n);

Ввод пары чисел:
type poin=record
      x,y:integer;
     end;
var
    ssk,ss1              :string;
    as                   :array[1..2,1..50] of poin;
    i,h,q1,w,q2          :integer;
...
 for i:=1 to q1+1 do
  begin
   readln(inp,ssk);
   ss1:=copy(ssk,1,pos(' ',ssk)-1);
   val(ss1,w,r);
   as[1,i].x:=w;
   delete(ssk,1,pos(' ',ssk));
   val(ssk,w,r);
   as[1,i].y:=w;
  end;
вместо простого
  for i:=1 to q1+1 do
    readln(inp,as[1,i].x,as[1,i].y);

Тоже ввод пары целых чисел, еще более сложная хакерская реализация показанных выше двух операторов:
Var xverh:array[1..51] of word;
    yverh:array[1..51] of longint;
    str,stro:string;
    flag1:boolean;
    schet,ymax,xmax,ymin,k,numbv,numbn,i,h:longint;
...
function stepen(nn:byte):longint;
var st:longint;
    p:byte;
 begin
  st:=1;
  for p:=1 to nn do st:=st*10;
  stepen:=st;
 end;
Function perevod(strok:string):longint;
var flag,j:byte;
chis:longint;
begin
 chis:=0;
 flag:=0;
 if strok[1]='-' then flag:=1;
 for j:=1+flag to length(strok) do
    chis:=chis+((ord(strok[j])-48)*stepen(length(strok)-j));
 if flag=1 then chis:=(-1)*chis;
 perevod:=chis;
end;
...
 For i:=1 to numbv+1 do
  Begin
   readLn(finp,str);
   stro:='';
   stro:=stro+str[1];
   schet:=1;flag1:=true;
   Repeat                     
   schet:=schet+1;
   if str[schet]<>' ' then stro:=stro+str[schet] else flag1:=false;
   Until flag1=false;
   xverh[i]:=perevod(stro);
   stro:='';if str[schet+1]<>'-' then begin
                                       stro:=stro+str[schet+1];
                                       if length(str)>schet+1 then
                                          for k:=schet+2 to length(str) do
                                             stro:=stro+str[k];
                                      end;
             if str[schet+1]='-' then begin
                                       stro:=stro+'-'+str[schet+2];
                                       if length(str)>schet+2 then
                                          for k:=schet+3 to length(str) do
                                             stro:=stro+str[k];
                                      end;
   yverh[i]:=perevod(stro);
   ...
  End;
Этот хакер не знает о существовании функций POS и STR.
Желающие могут найти ошибку.

Странное:
program oli_1;
uses crt,dos;
type
  Intt  = array[1..20]     of Integer;
  CharData = array['A'..'Z']   of Byte;
  Mat   = array[1..2, 1..8] of integer;
  outt = array[1..2] of integer;
var F: Text;
  a:intt;
  c:mat;
  i,j:integer;
  s1,s2:integer;
  min,max:integer;
  b:outt;
procedure readf(var f:text;var a:intt);
var i:integer;
begin
  { Get file to read from command line }
  Assign(F, 'input.txt');
  Reset(F);i:=1;
  while not Eof(F) do
  begin
    Read(F, a[i]);
    i:=i+1;
  end;
end;
procedure wrf(b:outt;var f:text);
begin
  Assign(F, 'output.txt');  { Standard output }
  Rewrite(F);
  Write(F,b[1],' ');  Writeln(F,b[2] );
  Close(F);
end;
begin
  readf(f,a);
  s1:=0;s2:=0;
  for j:=1 to a[1] do
  begin
    c[1,j]:=a[j+1];
    s1:=s1+c[1,j]
  end;
  for j:=1 to a[1] do
  begin
    c[2,j]:=a[j+1+a[1]];
    s2:=s2+c[2,j]
  end;
  min:=0;
  if s1>=s2 then min:=s1 else min:=s2;
  max:=0;
  for i:=1 to a[1] do
  for j:=1 to a[1] do
    if c[1,i]>=c[2,j] then max:=max+c[2,j] else max:=max+c[1,i];
  b[1]:=min;b[2]:=max;
  wrf(b,f)
end.
Зачем-то вводится весь набор данных в один массив, а затем данные заносятся в нужные переменные и массивы.

Но всех переплюнул хакер, решавший 1-ю задачу:
Program SKM;
label 1;
var a,b,c,d,e,f,g,i,o,p,k,r,s,t,q,v,x,y,z:real;
begin
x:=0;
readln(z);
if z=1 then
 begin
   readln(a);
   readln(o);
   if a>=o then x:=x+a
   else x:=x+o;
 end;
 if z=2 then
 begin
    readln(a,b);
    readln(o,p);
    if o>=a then x:=x+a
    else x:=x+o;
    if o>=b then x:=x+b
    else x:=x+o;
    if p>=b then x:=x+b
    else x:=x+p;
    if p>=a then x:=x+a
    else x:=x+p;
    if a+b>o+p then y:=a+b
    else y:=o+p;
  end;
if z=3 then
 begin
   readln(a,b,c);
   readln(o,p,k);
   if o>=a then x:=x+a
   else x:=x+o;
   if o>=b then x:=x+b
   else x:=x+o;
   if o>=c then x:=x+c
   else x:=x+o;
   if p>=b then x:=x+b
   else x:=x+p;
   if p>=a then x:=x+a
   else x:=x+p;
   if p>=c then x:=x+c
   else x:=x+p;
   if k>=a then x:=x+a
   else x:=x+k;
   if k>=b then x:=x+b
   else x:=x+k;
   if k>=c then x:=x+c
   else x:=x+k;
   if a+b+c>o+p+k then y:=a+b+c
   else y:=o+p+k;
 end;
if z=4 then
 begin
   readln(a,b,c,d);
   readln(o,p,k,r);
   if o>=a then x:=x+a
   else x:=x+o;
   if o>=b then x:=x+b
   else x:=x+o;
   if o>=c then x:=x+c
   else x:=x+o;
   if o>=d then x:=x+d
   else x:=x+o;
   if p>=b then x:=x+b
   else x:=x+p;
   if p>=a then x:=x+a
   else x:=x+p;
   if p>=c then x:=x+c
   else x:=x+p;
   if p>=d then x:=x+d
   else x:=x+p;
   if k>=a then x:=x+a
   else x:=x+k;
   if k>=b then x:=x+b
   else x:=x+k;
   if k>=c then x:=x+c
   else x:=x+k;
   if k>=d then x:=x+d
   else x:=x+k;
   if r>=a then x:=x+a
   else x:=x+r;
   if r>=b then x:=x+b
   else x:=x+r;
   if r>=c then x:=x+c
   else x:=x+r;
   if r>=d then x:=x+d
   else x:=x+r;
   if a+b+c+d>o+p+k+r then y:=a+b+c+d
   else y:=o+p+k+r;
 end;
if z=5 then
 begin
   readln(a,b,c,d,e);
   readln(o,p,k,r,s);
   if o>=a then x:=x+a
   else x:=x+o;
   if o>=b then x:=x+b
   else x:=x+o;
   if o>=c then x:=x+c
   else x:=x+o;
   if o>=d then x:=x+d
   else x:=x+o;
   if o>=e then x:=x+e
   else x:=x+o;
   if p>=b then x:=x+b
   else x:=x+p;
   if p>=a then x:=x+a
   else x:=x+p;
   if p>=c then x:=x+c
   else x:=x+p;
   if p>=d then x:=x+d
   else x:=x+p;
   if p>=e then x:=x+e
   else x:=x+p;
   if k>=a then x:=x+a
   else x:=x+k;
   if k>=b then x:=x+b
   else x:=x+k;
   if k>=c then x:=x+c
   else x:=x+k;
   if k>=d then x:=x+d
   else x:=x+k;
   if k>=e then x:=x+e
   else x:=x+k;
   if r>=a then x:=x+a
   else x:=x+r;
   if r>=b then x:=x+b
   else x:=x+r;
   if r>=c then x:=x+c
   else x:=x+r;
   if r>=d then x:=x+d
   else x:=x+r;
   if r>=e then x:=x+e
   else x:=x+r;
   if s>=a then x:=x+a
   else x:=x+s;
   if s>=b then x:=x+b
   else x:=x+s;
   if s>=c then x:=x+c
   else x:=x+s;
   if s>=d then x:=x+d
   else x:=x+s;
   if s>=e then x:=x+e
   else x:=x+s;
   if a+b+c+d+e>o+p+k+r+s then y:=a+b+c+d+e
   else y:=o+p+k+r+s;
 end;
if z=6 then
 begin
   readln(a,b,c,d,e,f);
   readln(o,p,k,r,s,t);
   if o>=a then x:=x+a
   else x:=x+o;
   if o>=b then x:=x+b
   else x:=x+o;
   if o>=c then x:=x+c
   else x:=x+o;
   if o>=d then x:=x+d
   else x:=x+o;
   if o>=e then x:=x+e
   else x:=x+o;
   if o>=f then x:=x+f
   else x:=x+o;
   if p>=b then x:=x+b
   else x:=x+p;
   if p>=a then x:=x+a
   else x:=x+p;
   if p>=c then x:=x+c
   else x:=x+p;
   if p>=d then x:=x+d
   else x:=x+p;
   if p>=e then x:=x+e
   else x:=x+p;
   if p>=f then x:=x+f
   else x:=x+p;
   if k>=a then x:=x+a
   else x:=x+k;
   if k>=b then x:=x+b
   else x:=x+k;
   if k>=c then x:=x+c
   else x:=x+k;
   if k>=d then x:=x+d
   else x:=x+k;
   if k>=e then x:=x+e
   else x:=x+k;
   if k>=f then x:=x+f
   else x:=x+k;
   if r>=a then x:=x+a
   else x:=x+r;
   if r>=b then x:=x+b
   else x:=x+r;
   if r>=c then x:=x+c
   else x:=x+r;
   if r>=d then x:=x+d
   else x:=x+r;
   if r>=e then x:=x+e
   else x:=x+r;
   if r>=f then x:=x+f
   else x:=x+r;
   if s>=a then x:=x+a
   else x:=x+s;
   if s>=b then x:=x+b
   else x:=x+s;
   if s>=c then x:=x+c
   else x:=x+s;
   if s>=d then x:=x+d
   else x:=x+s;
   if s>=e then x:=x+e
   else x:=x+s;
   if s>=f then x:=x+f
   else x:=x+s;
   if t>=a then x:=x+a
   else x:=x+t;
   if t>=b then x:=x+b
   else x:=x+t;
   if t>=c then x:=x+c
   else x:=x+t;
   if t>=d then x:=x+d
   else x:=x+t;
   if t>=e then x:=x+e
   else x:=x+t;
   if t>=f then x:=x+f
   else x:=x+t;
   if a+b+c+d+e+f>o+p+k+r+s+t then y:=a+b+c+d+e+f
   else y:=o+p+k+r+s+t;
 end;
if z=7 then
 begin
   readln(a,b,c,d,e,f,g);
   readln(o,p,k,r,s,t,q);
   i:=0;
   v:=0;
   goto 1;
 end;
if z=8 then
 begin
    readln(a,b,c,d,e,f,g);
    readln(o,p,k,r,s,t,q);
    if o>=i then x:=x+i
    else x:=x+o;
    if p>=i then x:=x+i
    else x:=x+p;
    if k>=i then x:=x+i
    else x:=x+k;
    if r>=i then x:=x+i
    else x:=x+r;
    if s>=i then x:=x+i
    else x:=x+s;
    if t>=i then x:=x+i
    else x:=x+t;
    if q>=i then x:=x+i
    else x:=x+q;
    if v>=a then x:=x+a
    else x:=x+v;
    if v>=b then x:=x+b
    else x:=x+v;
    if v>=c then x:=x+c
    else x:=x+v;
    if v>=d then x:=x+d
    else x:=x+v;
    if v>=e then x:=x+e
    else x:=x+v;
    if v>=f then x:=x+f
    else x:=x+v;
    if v>=g then x:=x+g
    else x:=x+v;
    if v>=i then x:=x+i
    else x:=x+v;
1:  if o>=a then x:=x+a
    else x:=x+o;
    if o>=b then x:=x+b
    else x:=x+o;
    if o>=c then x:=x+c
    else x:=x+o;
    if o>=d then x:=x+d
    else x:=x+o;
    if o>=e then x:=x+e
    else x:=x+o;
    if o>=f then x:=x+f
    else x:=x+o;
    if o>=g then x:=x+g
    else x:=x+o;
    if p>=b then x:=x+b
    else x:=x+p;
    if p>=a then x:=x+a
    else x:=x+p;
    if p>=c then x:=x+c
    else x:=x+p;
    if p>=d then x:=x+d
    else x:=x+p;
    if p>=e then x:=x+e
    else x:=x+p;
    if p>=f then x:=x+f
    else x:=x+p;
    if p>=g then x:=x+g
    else x:=x+p;
    if k>=a then x:=x+a
    else x:=x+k;
    if k>=b then x:=x+b
    else x:=x+k;
    if k>=c then x:=x+c
    else x:=x+k;
    if k>=d then x:=x+d
    else x:=x+k;
    if k>=e then x:=x+e
    else x:=x+k;
    if k>=f then x:=x+f
    else x:=x+k;
    if k>=g then x:=x+g
    else x:=x+k;
    if r>=a then x:=x+a
    else x:=x+r;
    if r>=b then x:=x+b
    else x:=x+r;
    if r>=c then x:=x+c
    else x:=x+r;
    if r>=d then x:=x+d
    else x:=x+r;
    if r>=e then x:=x+e
    else x:=x+r;
    if r>=f then x:=x+f
    else x:=x+r;
    if r>=g then x:=x+g
    else x:=x+r;
    if s>=a then x:=x+a
    else x:=x+s;
    if s>=b then x:=x+b
    else x:=x+s;
    if s>=c then x:=x+c
    else x:=x+s;
    if s>=d then x:=x+d
    else x:=x+s;
    if s>=e then x:=x+e
    else x:=x+s;
    if s>=f then x:=x+f
    else x:=x+s;
    if s>=g then x:=x+g
    else x:=x+s;
    if t>=a then x:=x+a
    else x:=x+t;
    if t>=b then x:=x+b
    else x:=x+t;
    if t>=c then x:=x+c
    else x:=x+t;
    if t>=d then x:=x+d
    else x:=x+t;
    if t>=e then x:=x+e
    else x:=x+t;
    if t>=f then x:=x+f
    else x:=x+t;
    if t>=g then x:=x+g
    else x:=x+t;
    if q>=a then x:=x+a
    else x:=x+q;
    if q>=b then x:=x+b
    else x:=x+q;
    if q>=c then x:=x+c
    else x:=x+q;
    if q>=d then x:=x+d
    else x:=x+q;
    if q>=e then x:=x+e
    else x:=x+q;
    if q>=f then x:=x+f
    else x:=x+q;
    if q>=g then x:=x+g
    else x:=x+q;
    if a+b+c+d+e+f+g+i>o+p+k+r+s+t+q+v then y:=a+b+c+d+e+f+g+i
    else y:=o+p+k+r+s+t+q+v;
 end;
 writeln(y, x);
end.
loading