Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.

Neispravan USACO zadatak :(

[es] :: Pascal / Delphi / Kylix :: Neispravan USACO zadatak :(

[ Pregleda: 3204 | Odgovora: 11 ] > FB > Twit

Postavi temu Odgovori

Autor

Pretraga teme: Traži
Markiranje Štampanje RSS

peromalosutra
Ivan Rajkovic
Software engineer
Luxoft
Berlin

Član broj: 54774
Poruke: 871
*.dialup.blic.net.



+148 Profil

icon Neispravan USACO zadatak :(18.06.2005. u 21:06 - pre 228 meseci
Zapeo sam vec na drugom zadatku! Razumijem ga, ali ne razumijem zasto moj kod ne radi. Evo teksta:
Citat:

...your job is to write a program which takes the names of a group and a comet and then determines whether the group should go with the UFO behind that comet.

Both the name of the group and the name of the comet are converted into a number in the following manner: the final number is just the product of all the letters in the name, where "A" is 1 and "Z" is 26. For instance, the group "USACO" would be 21 * 19 * 1 * 3 * 15 = 17955. If the group's number mod 47 is the same as the comet's number mod 47, then you need to tell the group to get ready! (Remember that "a mod b" is the remainder left over after dividing a by b; 34 mod 10 is 4.)

Write a program which reads in the name of the comet and the name of the group and figures out whether according to the above scheme the names are a match, printing "GO" if they match and "STAY" if not. The names of the groups and the comets will be a string of capital letters with no spaces or punctuation, up to 6 characters long.


a evo i mog koda:
Code:

program ride;
uses wincrt;
var buffin,buffout:text;
    komet,grupa:string;
function desifruj (ime:string):longint;
var i,br:integer;
    znak:char;
    p:real;
begin
p:=1;
br:=0;
for i:=1 to length(ime) do
    for znak:='A' to 'Z' do
    begin
    inc(br,1);
    if ime[i]=znak then p:=p*br;
    if znak='Z' then br:=0;
    end;
desifruj:=round(p) mod 47;
end;

begin
assign (buffin,'c:/test.in');
reset (buffin);
readln (buffin, komet, grupa);
close (buffin);
assign (buffout,'c:/test.out');
rewrite (buffout);
if desifruj (komet)=desifruj (grupa)
   then write (buffout,'GO')
   else write (buffout,'STAY');
close (buffout);
end.

U cemu je greska?

 
Odgovor na temu

ivanho
slobodni umetnik
Beograd

Član broj: 28465
Poruke: 73
217.26.75.*



Profil

icon Re: Neispravan USACO zadatak :(18.06.2005. u 21:28 - pre 228 meseci
za funkciju desifruj ovo ti je mnogo bolje resenje:

Code:

function desifruj (ime:string):integer;
var i, p:integer;
begin
p:=1;
for i:=1 to length(ime) do
 begin
  if ime[i] in ['A'..'Z'] then
   p:=p*(Ord(ime[i])-64);
 end;
 Result:=p mod 47



 
Odgovor na temu

peromalosutra
Ivan Rajkovic
Software engineer
Luxoft
Berlin

Član broj: 54774
Poruke: 871
*.dialup.blic.net.



+148 Profil

icon Re: Neispravan USACO zadatak :(18.06.2005. u 21:34 - pre 228 meseci
Pokusao sam sa tvojom funkcijom:
Code:

program ride;
uses wincrt;
var buffin,buffout:text;
    komet,grupa:string;

function desifruj (ime:string):integer;
var i, p:integer;
begin
p:=1;
for i:=1 to length(ime) do
 begin
  if ime[i] in ['A'..'Z'] then
   p:=p*(Ord(ime[i])-64);
 end;
 desifruj:=p mod 47;
 end;
begin
assign (buffin,'c:/test.in');
reset (buffin);
readln (buffin, komet, grupa);
close (buffin);
assign (buffout,'c:/test.out');
rewrite (buffout);
if desifruj (komet)=desifruj (grupa)
   then write (buffout,'GO')
   else write (buffour,'STAY');
end.

ali i dalje ne radi. Test primjeri su:
Citat:


Input Output
-----------------
COMETQ
HVNGAT GO

ABSTAR
USACO STAY


 
Odgovor na temu

peromalosutra
Ivan Rajkovic
Software engineer
Luxoft
Berlin

Član broj: 54774
Poruke: 871
*.dialup.blic.net.



+148 Profil

icon Re: Neispravan USACO zadatak :(18.06.2005. u 21:35 - pre 228 meseci
Pokusao sam sa tvojom funkcijom:
Code:

program ride;
uses wincrt;
var buffin,buffout:text;
    komet,grupa:string;

function desifruj (ime:string):integer;
var i, p:integer;
begin
p:=1;
for i:=1 to length(ime) do
 begin
  if ime[i] in ['A'..'Z'] then
   p:=p*(Ord(ime[i])-64);
 end;
 desifruj:=p mod 47;
 end;
begin
assign (buffin,'c:/test.in');
reset (buffin);
readln (buffin, komet, grupa);
close (buffin);
assign (buffout,'c:/test.out');
rewrite (buffout);
if desifruj (komet)=desifruj (grupa)
   then write (buffout,'GO')
   else write (buffout,'STAY');
end.

ali i dalje ne radi. Test primjeri su:
Citat:


Input Output
-----------------
COMETQ
HVNGAT GO

ABSTAR
USACO STAY


 
Odgovor na temu

cassey
Andreja Ilic
Nis

Član broj: 57788
Poruke: 188
212.200.11.*



+1 Profil

icon Re: Neispravan USACO zadatak :(18.06.2005. u 21:56 - pre 228 meseci
Pazi, koliko vidim ti ne ucitavac stringove dobro. U input-u se po jedan string nalazi u zasebnom redu, tako da ti ne mozes da ih ucitavas sa ReadLn (jedna, drugi)... Ukoliko u inputu imas samo brojeve onda ih ucitavaj sa Read, bez obzira da li su oni u jednom ili vise reda. Ali kad imas string uvek moras da ucitavas sa ReadLn ali time ucitas ceo red...

Drugo, pazi da ti (u tvom kodu) ovo p ne prekoraci Real (pogledaj u helpu koja su ogranicenja za koje tipove) pa bi trebalo cesce (u ovom zadatku) da radis ... Ali opet (ovaj zadatak je prost pa nije bitno) pazi, jer funkcija mod radi dosta sporo tj. ona radi na principu oduzimanja dok se ne dobije manji broj od onog po kome se trai mod. Znaci ako ti trazis 743254843289 po modulu 2 on ce da oduzima 2 od ovog broja dok ne dobije 1 ili 0, sto je mnogo sporo....

Evo koda (nisam ga debagirao, ali probaj)...

Srecno

Code:

program ride;
const
  m = 47;
var
  s1, s2: String;
  Sol: Boolean;
  f, g: Text;

  procedure InPut;
  begin
    Assign (f, 'ride.in');
    Reset (f);
    ReadLn (f, s1);
    ReadLn (f, s2);
    Close (f);
  end;

  procedure Solve;
  var
    i, m1, m2: Longint;
  begin
    m1 := 1;
    for i := 1 to length (s1) do
      m1 := (m1 * (Ord (s1 [i]) - 64)) mod m;
    m2 := 1;
    for i := 1 to length (s2) do
      m2 := (m2 * (Ord (s2 [i]) - 64)) mod m;
    if (m1 = m2) then Sol := True
    else Sol := False;
  end;

  procedure OutPut;
  begin
    Assign (g, 'ride.out');
    ReWrite (g);
    if (Sol) then WriteLn (g, 'GO')
    else WriteLn (g, 'STAY');
    Close (g);
  end;

begin
  InPut;
  Solve;
  OutPut;
end.

Math is like love. A simple idea but it can get complicated.
 
Odgovor na temu

Srki_82
Srdjan Tot
Me @ My Home
Ljubljana

Član broj: 28226
Poruke: 1403
82.208.201.*

ICQ: 246436949


+10 Profil

icon Re: Neispravan USACO zadatak :(19.06.2005. u 06:06 - pre 228 meseci
Ovo je verovatno neki zadatak u kojem ne mozes da radis bas sve tako prosto kao sto izgleda
Za ime grupe koje se sastoji od 20 slova Z broj za koji treba da izracunas mod 47 je 518131871275444637960845131776... pitam se koliki bi bio broj da ima, recimo 50 slova Z... taj broj ti nece stati ni u jedan tip promenljivih. Dakle... treba da pomnozis dva broja i da odlucis koji je bitan deo za pamcenje, a ostatak da zaboravis. Da si recimo trazio mod 2 bilo bi ti dovoljno da pamtis samo zadnju cifru.
Razmisli i ovome
 
Odgovor na temu

peromalosutra
Ivan Rajkovic
Software engineer
Luxoft
Berlin

Član broj: 54774
Poruke: 871
*.dialup.blic.net.



+148 Profil

icon Re: Neispravan USACO zadatak :(19.06.2005. u 08:10 - pre 228 meseci
Citat:
Za ime grupe koje se sastoji od 20 slova Z broj za koji treba da izracunas mod 47 je 518131871275444637960845131776...


Ali u zadatku se kaze da ce najduze ime imati 6 znakova:

Code:
The names of the groups and the comets will be a string of capital letters with no spaces or punctuation, up to 6 characters long.


pa je najveci broj 26^6=308915776, a to je valjda dovoljno za longint.

 
Odgovor na temu

peromalosutra
Ivan Rajkovic
Software engineer
Luxoft
Berlin

Član broj: 54774
Poruke: 871
*.dialup.blic.net.



+148 Profil

icon Re: Neispravan USACO zadatak :(19.06.2005. u 11:00 - pre 228 meseci
Rjesio sam zadatak, kod mene radi dobro za sve test primjere, ali kad ga posaljem na USACO, njihov kompajler kaze da je sve u redu, ali mi kasnije prijavi error 2 (pozivanje nepostojeceg fajla):

Citat:
Execution error: Your program (`test') exited with exit status `2'
when presented with test case 1, shown below.



Evo kompletnog koda:
Code:


{
ID: ivanraj1
PROG: test
LANG: PASCAL
}

program ride;
var buffin,buffout:text;
    komet,grupa:string;

function desifruj (ime:string):longint;
var i,br:integer;
    znak:char;
    p:longint;
begin

p:=1;
for i:=1 to length(ime) do
    p:=p*(ord(ime[i])-64);
desifruj:=p mod 47;
end;

begin
assign (buffin,'ride.in');
reset (buffin);
readln (buffin, komet);
readln (buffin, grupa);
close (buffin);
assign (buffout,'ride.out');
rewrite (buffout);
if desifruj (komet)=desifruj (grupa)
   then write ('GO')
   else write ('STOP');
end.

Pogledao sam ponovo, ali lijepo pise da ulazna datoteka mora biti RIDE.IN, a izlazna RIDE.OUT, bas kako sam ja naveo u svom programu.

 
Odgovor na temu

Srki_82
Srdjan Tot
Me @ My Home
Ljubljana

Član broj: 28226
Poruke: 1403
82.208.201.*

ICQ: 246436949


+10 Profil

icon Re: Neispravan USACO zadatak :(19.06.2005. u 12:24 - pre 228 meseci
Pa ti ne pises u RIDE.OUT nego na CON: fajl. Write bez File parametra ispisuje podatak na default izlazu sto je ekran, a ne RIDE.OUT fajl. I jos nesto... otvaras RIDE.OUT fajl, a nigde ga ne zatvaras!!!
 
Odgovor na temu

peromalosutra
Ivan Rajkovic
Software engineer
Luxoft
Berlin

Član broj: 54774
Poruke: 871
*.dialup.blic.net.



+148 Profil

icon Re: Neispravan USACO zadatak :(19.06.2005. u 20:35 - pre 228 meseci
Nisam te bas shvatio, u zadatku se kaze da se podaci ucitavaju sa ride.in, a cuvaju u ride.out. Zar trebam napisati assign (buffout,'file ride.out'), ne shvatam ovo bas. Zbunjuje me ta lokacija fajla, da ga trebam smjestiti negdje na mom kompjuteru samo bih naveo putanju, a ovako mi nista nije jasno. Sto se tice zatvaranja fajla, njima (USACO-u) sam poslao ispravan kod, slucajno sam izbrisao tu liniju u postu.

 
Odgovor na temu

cassey
Andreja Ilic
Nis

Član broj: 57788
Poruke: 188
212.200.11.*



+1 Profil

icon Re: Neispravan USACO zadatak :(19.06.2005. u 20:40 - pre 228 meseci
Ti iz fila ride.in ucitavas polazne podatke, a rezulatat stampas u fail raie.out. Ti si u tvom kodu rezultat stampao na standardnom izlazu a ne u dadoteci. Pogledaj moj kod (postovan je u nekoj o prethodnih poruka)...
Math is like love. A simple idea but it can get complicated.
 
Odgovor na temu

cassey
Andreja Ilic
Nis

Član broj: 57788
Poruke: 188
212.200.11.*



+1 Profil

icon Re: Neispravan USACO zadatak :(19.06.2005. u 20:44 - pre 228 meseci
I da, ukoliliko ti napises time se podrazumeva da je navedeni fail u istom folderu kao i sam tvoj program (exe verzija), a ukoliko napises celekupnu putanju na komp (npr c:\aaa\xxx.in) tada taj fail mora tu da se nalazi. USACO (kao i svi drugi online testeri) podrazumevaju da se in i out failovi nalaze gde i sam program...
Math is like love. A simple idea but it can get complicated.
 
Odgovor na temu

[es] :: Pascal / Delphi / Kylix :: Neispravan USACO zadatak :(

[ Pregleda: 3204 | Odgovora: 11 ] > FB > Twit

Postavi temu Odgovori

Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.