Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Whats Wrong!?!

JP161470JP161470 Member Posts: 26
[color=Blue]Given T (1 <= T <= 100) integers in the range (-1000,1000), representing percentages, calculate which letter grade each of them correspond to.
The mark table is as follows:
A = 80..100
B = 70.. 79
C = 60.. 69
D = 50.. 59
F = 0.. 49
X everything else

Input Format
Line 1: one integer T
Lines 2..T+1: one integer N

Output Format:
Lines 1..T: one character denoting the letter grade received

Input:
3
10
99
101

Output:
F
A
X
[/color]

[code]var
a,b:integer;
d: array [1..1000] of integer;


begin
readln(a);
for b:= 1 to a do

readln(d[b]);

for a:= 1 to a do
begin
if (d[b]>=80) and (d[b]<=100) then
writeln('A')

else if (d[b]>=70) and (d[b]<=79) then
writeln('B')
else if (d[b]>=60) and (d[b]<=69) then
writeln('C')
else if (d[b]>=50) and (d[b]<=59) then
writeln('D')
else if (d[b] >= 49) then
writeln('F')
else
writeln('X');


end;
readln;
end.

«1

Comments

  • ActorActor Member Posts: 447 ✭✭
    [code]
    var
    i,j:integer;
    d: array [1..1000] of integer;


    begin
    readln(j);
    for i := 1 to j do
    readln(d[i]);

    for [red]i[/red] := 1 to a j begin
    if (d[i]>=80) and (d[i]<=100) then
    writeln('A')
    else if (d[i]>=70) and (d[i]<=79) then
    writeln('B')
    else if (d[i]>=60) and (d[i]<=69) then
    writeln('C')
    else if (d[i]>=50) and (d[i]<=59) then
    writeln('D')
    else if (d[i] [red]<=[/red] 49) [red]or (d[i] > 100)[/red] then
    writeln('F')
    else
    writeln('X');
    end;
    readln;
    end.
    [/code]

    Even better

    [code]
    var
    i,j : integer;
    d : array [1..1000] of integer;


    begin
    readln(j);
    for i := 1 to i do
    readln(d[i]);

    for i := 1 to j do
    if d[i] > 100 then
    writeln('X')
    else if d[i] >= 80 then [red]{ no need to test d[i] <= 100 since d[i] > 100 }[/red]
    writeln('A') [red]{ has been eliminated by previous test }[/red]
    else if d[i] >= 70 then
    writeln('B')
    else if d[i] >= 60 then
    writeln('C')
    else if d[i] >= 50 then
    writeln('D')
    else if d[i] >= 0 then
    writeln('F')
    else
    writeln('X');
    readln;
    end.
    [/code]
    ?
  • JP161470JP161470 Member Posts: 26
    [color=Red]Given an integer N (0 <= N <= 200000) representing the mass of a letter in grams, print out the cost in cents to mail the letter.

    The first line of input will be the number of test cases T (1 <= T <= 100). The following T lines will contain N. Output the cost in cents to mail the letter.

    The pricing is as follows:
    0 <= N <= 30 costs 38 cents
    30 < N <= 50 costs 55 cents
    50 < N <= 100 costs 73 cents
    If N > 100 then the base cost is 73 cents, plus 24 cents for every additional 50 grams or part thereof.

    Input:
    2
    5
    101

    Output:
    38
    97[/color]


    [code]uses crt;
    var
    total,cost1,tt:integer;
    a:array [1..100000] of integer;


    begin
    clrscr;
    readln(tt);
    for total:= 1 to tt do

    readln(a[total]);

    for total:= 1 to tt do
    begin
    if total<=30 then
    writeln('38')

    else if total<=50 then
    writeln('55')

    else if total<=100 then
    writeln('73')

    else if total>100 then
    begin
    cost1:= total-50;
    writeln(73+24*(cost1 div 50));
    end;
    end;

    readln;
    end.[/code]



  • ActorActor Member Posts: 447 ✭✭
    : [code]: uses crt;
    : var
    : total,cost1,tt:integer;
    : a:array [1..100000] of integer;
    :
    :
    : begin
    : clrscr;
    : readln(tt);
    : for total:= 1 to tt do
    :
    : readln(a[total]);
    :
    : for total:= 1 to tt do
    : begin
    : if total<=30 then [red]{ think about it -- if tt <= 30 then }[/red]
    : writeln('38') [red]{ '38' gets printed every time } [/red]
    :
    : else if total<=50 then
    : writeln('55')
    :
    : else if total<=100 then
    : writeln('73')
    :
    : else if total>100 then
    : begin
    : cost1:= total-50;
    : writeln(73+24*(cost1 div 50));
    : end;
    : end;
    :
    : readln;
    : end.[/code]:
    :
    :
    :
    :
    [code]
    uses crt;
    var
    i, t : 1 .. 100 ; [red]{ subrange of integer or byte }[/red]
    n : 0 .. 200000 ; [red]{ subrange of longint - integer can only
    go as high as 32767 }[/red]
    cost : array [1 .. 100] of 0 .. 200000 ;

    begin
    clrscr;
    readln(t);

    for i := 1 to t do begin
    readln(n);
    if n <= 30 then
    cost[i] := 38
    else if n <= 50 then
    cost[i] := 55
    else if n <= 100 then
    cost[i] := 73
    else [red]{ n > 100 is only remaining possibility }[/red]
    cost[i] := 73 + 24*(((n - 100) div 50) + 1)
    end ;

    for i := 1 to t do
    writeln (cost[i]) ;

    readln;
    end.
    [/code]
  • JP161470JP161470 Member Posts: 26
    [color=Blue]Given two integers N and M (N <= M), output all the prime numbers between N and M inclusive, one per line.

    N and M will be positive integers less than or equal to 1,000,000,000.
    The difference between N and M will be less than or equal to 5,000,000.

    Sample Input

    5 20

    Sample Output

    5
    7
    11
    13
    17
    19[/color]
    [color=Red]
    and BTW thankx a lot for all your help!!! [/color]

    [code]uses crt;
    CONST
    N = 100;

    Var
    P : Array [1 .. N] of boolean ;
    i,q,j,m : longint;


    begin
    clrscr;
    readln(i);
    readln(q);

    for i := 1 TO N do
    P[i] := TRUE;

    m := trunc(sqrt(N)) ;
    for i := 2 to m do

    if P[i] then
    for j := 2 to N DIV i do
    P[i * j] := FALSE ;

    for i := 1 to q do
    if P[i] then
    if i>=r then
    begin
    writeln(i) ;
    end;
    readln
    end.

    [/code]
  • ActorActor Member Posts: 447 ✭✭
    [code]
    uses crt;
    CONST
    N = 100;

    Var
    P : Array [1 .. N] of boolean ;
    i,j,m,q,[red]r[/red] : longint; [red]{ declare r - didn't compiler catch this? }[/red]


    begin
    clrscr;
    readln([red]r[/red]); [red]{ readln(r), not readln(i) }[/red]
    readln(q);

    for i := 1 TO N do
    P[i] := TRUE;
    [red]{
    Sieve of Aristosthenes - good approach, but putting the
    sieve in an array won't work for large N because memory
    won't hold it. For large N you'll have to use a file.
    }[/red]
    m := trunc(sqrt(N)) ;
    for i := 2 to m do
    if P[i] then
    for j := 2 to N DIV i do
    P[i * j] := FALSE ;

    for i := 1 to q do
    if P[i] then
    if i >= r then
    begin
    writeln(i) ;
    end;
    readln
    end.
    [/code]


  • AtexAtex Member Posts: 268
    Because the possible large interval we looking at any algorithm involving a look-up array won't work ( by using the sieve of Erathostenes, the biggest interval would be 'maxavail div 4', assuming that we are using longint's and real mode). The following method would work with some speed trade off ( before doing some optimizations, it covered the 5 million interval in about 3-4 minutes on my computer ):
    [code][color=Blue]
    program primes;

    const max_val{ue}=1000000000;
    max_dif{ference}=5000000;
    mv:array[1..4] of byte=(2,3,5,7);

    function is_prime(l:longint):boolean;
    var l2:comp; {very large integer, to support square of max_value}
    i:longint;
    m:byte;
    begin
    for m:=1 to 4 do
    if (l mod mv[m]=0) then begin
    is_prime:=(l=mv[m]);
    exit;
    end;
    l2:=mv[4];
    while sqr(l2)min_v) and (max_v-min_v<=max_dif));
    writeln;
    n:=0;
    for t:=min_v to max_v do
    if is_prime(t) then begin
    writeln(t);
    inc(n);
    end;
    writeln(n,' number of primes found');
    end.
    [/color][/code]
  • JP161470JP161470 Member Posts: 26
    TIME LIMIT OF 2 SECONDS!! ANY IDEA..?
  • AtexAtex Member Posts: 268
    : TIME LIMIT OF 2 SECONDS!! ANY IDEA..?
    :
    Wait a few years, then run the code on an entry level PC...:-)
  • Phat NatPhat Nat Member Posts: 757
    For one, this checks all the numbers right up to it, however we know that anything over half the number is not going to divide into it, so
    [code]
    while i<l2 do begin
    inc(i,2);
    [/code]

    can be changed to:
    [code]
    while i < (l2 DIV 2) do begin
    inc(i,2);
    [/code]

    however this is slow, so we use a shift instead:
    [code]
    while i < (l2 SHR 1) do begin
    inc(i,2);
    [/code]


    Also, I would suggest that your main procedcure increments the prime checks by two and therefore only needs to do half the amount of function calls (slow).
  • AtexAtex Member Posts: 268
    Alas, TP is not renowned for the the fast EXE's, to really speed things up ASM is the way to go. Parallel programming to take advantage of the todays multicore cpu's, 087 code + heavy optimization...but we don't want to switch from the good old TP.....
«1
Sign In or Register to comment.