Unbelievable

Write here if you have problems with your C source code

Moderator: Board moderators

Post Reply
sacra
New poster
Posts: 8
Joined: Sun Oct 27, 2002 9:36 pm
Location: Coimbra, Portugal
Contact:

Unbelievable

Post by sacra »

I have two almost perfectly equivalent (on my mind they're equivalent) programs. One in Java, the other one in C. One gets Accepted by the judge, the other doesn't. Obviously, it's the Java one that doesn't work :)
Originally, I was using different ways to solve the problem. For example, I was using Java Vectors instead of simple arrays, had a different way of controlling the flow, etc..
But now, I was so curious, that I changed the Java program to be an exact match of the C one.

Check it out:

(In C)
#include <stdio.h>

#define MAXN 1000

int code[MAXN];
char match[MAXN];
int guess[MAXN];
int hint[2];
int n;

void generate_hint()
{
int i, j;

for(i=0;i<n;i++)
if (guess == code) match = 'S';
else match = ' ';

for(i=0;i<n;i++) {
if (match == 'S') continue;

for (j=0;j<n;j++) {
if (match[j] != ' ') continue;
if (guess != code[j]) continue;
match[j] = 'W';
break;
}
}

hint[0] = hint[1] = 0;
for (i=0;i<n;i++) switch(match) {
case 'S': hint[0]++; break;
case 'W': hint[1]++; break;
}
}

main()
{
int i, gameno = 0;

for(;;) {
scanf("%d",&n);

if (n == 0) exit(0);
printf("Game %d:\n", ++gameno);
for(i=0;i<n;i++) scanf("%d",code+i);

for(;;) {
for(i=0;i<n;i++) scanf("%d",guess+i);
if (guess[0] == 0) break;

generate_hint();
printf(" (%d,%d)\n", hint[0], hint[1]);
}
}
}

(In Java)

import java.io.*;
import java.util.*;

class Main
{
final int MAX = 1000;

static String ReadLn (int maxLg)
{
byte lin[] = new byte [maxLg];
int lg = 0, car = -1;
String line = "";

try
{
while (lg < maxLg)
{
car = System.in.read();
if ((car < 0) || (car == '\n')) break;
lin [lg++] += car;
}
}
catch (IOException e)
{
return (null);
}

if ((car < 0) && (lg == 0)) return (null); // eof
return (new String (lin, 0, lg));
}

public static void main(String args[])
{
Main m = new Main();
m.start();
}

private void start()
{
String s;
StringTokenizer st;
int n;
int[] code = new int[MAX];
int[] attempt = new int[MAX];
char[] matches = new char[MAX];

int numStrong=0, numWeak=0, counter = 0;

while (true)
{
s = ReadLn(255).trim();

n = Integer.parseInt(s);

if(n==0)
System.exit(0);

System.out.println("Game "+(++counter)+":");

s = ReadLn(255).trim();

st = new StringTokenizer(s);

for(int i=0;i<n;i++)
code = Integer.parseInt(st.nextToken());

while (true)
{
s = ReadLn(255).trim();
st = new StringTokenizer(s);

for(int i=0;i<n;i++)
attempt = Integer.parseInt(st.nextToken());

if(attempt[0]==0)
break;

for(int i=0;i<n;i++)
{
if (attempt==code[i]) matches[i]='S';
else
matches[i]=' ';
}

for(int i=0;i<n;i++)
{
if(matches[i]=='S')
continue;

for(int j=0;j<n;j++)
{
if (matches[j]!=' ')
continue;

if (attempt[i]!=code[j])
continue;

matches[j]='W';
break;
}
}

numStrong=0;
numWeak=0;

for(int i=0;i<n;i++)
{
switch (matches[i])
{
case('S'):numStrong++;
break;
case('W'):numWeak++;
break;
}
}

System.out.println(" ("+numStrong+","+numWeak+")");
}
}
}
}

Unbelievable!
ithamar
Learning poster
Posts: 56
Joined: Mon May 13, 2002 11:58 pm
Location: Venezuela

Post by ithamar »

I havent seen your code.

But this happen when you use the readline provided in the instruction page. This readline doesnt handle right the last line and when there are more than one line between the data.
Those Who Don't Know History are doomed to repeat it
Post Reply

Return to “C”