340 - Master-Mind Hints
Moderator: Board moderators
340 - Master-Mind Hints
the problem is simple, but i got WA
is there any tricky case?
is there any tricky case?
deddy one it doesn't matter wether you print stuff before or after you read the input!!!
what matters is what you output... you could read all the input at once in a large buffer, or one job at a time, as you like.
this problem is not hard...think about special cases..
things like
123456789
111111111
what matters is what you output... you could read all the input at once in a large buffer, or one job at a time, as you like.
this problem is not hard...think about special cases..
things like
123456789
111111111
We never perform a computation ourselves, we just hitch a ride on the great Computation that is going on already. --Tomasso Toffoli
340--help!!
I keep get WA,so is there anyone can give me some test data??
PLEASE....><
PLEASE....><
340 why WA?
[java]import java.io.*;
import java.util.*;
class Main {
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);
return (new String (lin, 0, lg));
}
public static void main(String[] args) {
int games = 1;
String line = ReadLn(255);
while(line.charAt(0) != '0') {
System.out.println("Game " + games + ":");
StringTokenizer st = new StringTokenizer(line);
int len = Integer.parseInt(st.nextToken());
int[] secret = new int[len];
int[] guess = new int[len];
st = new StringTokenizer(ReadLn(255));
for (int i = 0;i < len;i++)
secret = Integer.parseInt(st.nextToken());
st = new StringTokenizer(ReadLn(255));
for (int i = 0;i < len;i++)
guess = Integer.parseInt(st.nextToken());
while(guess[0] != 0) {
int strong = 0;
int weak = 0;
boolean[] match1 = new boolean[len];
boolean[] match2 = new boolean[len];
for (int i = 0;i < len;i++) {
if (guess == secret) {
match1 = true;
match2 = true;
strong++;
}
}
for (int j = 0;j < len;j++) {
if (!match2[j]) {
for (int k = 0;k < len;k++) {
if (!match1[k] && (guess[j] == secret[k])) {
match1[k] = true;
match2[j] = true;
weak++;
break;
}
}
}
}
System.out.println(" (" + strong + "," + weak + ")");
st = new StringTokenizer(ReadLn(255));
for (int i = 0;i < len;i++)
guess = Integer.parseInt(st.nextToken());
}
games++;
line = ReadLn(255);
}
}
}[/java]
Why the above code gives WA? It works with the special cases mentioned in this forum, and also works with the sample input.
import java.util.*;
class Main {
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);
return (new String (lin, 0, lg));
}
public static void main(String[] args) {
int games = 1;
String line = ReadLn(255);
while(line.charAt(0) != '0') {
System.out.println("Game " + games + ":");
StringTokenizer st = new StringTokenizer(line);
int len = Integer.parseInt(st.nextToken());
int[] secret = new int[len];
int[] guess = new int[len];
st = new StringTokenizer(ReadLn(255));
for (int i = 0;i < len;i++)
secret = Integer.parseInt(st.nextToken());
st = new StringTokenizer(ReadLn(255));
for (int i = 0;i < len;i++)
guess = Integer.parseInt(st.nextToken());
while(guess[0] != 0) {
int strong = 0;
int weak = 0;
boolean[] match1 = new boolean[len];
boolean[] match2 = new boolean[len];
for (int i = 0;i < len;i++) {
if (guess == secret) {
match1 = true;
match2 = true;
strong++;
}
}
for (int j = 0;j < len;j++) {
if (!match2[j]) {
for (int k = 0;k < len;k++) {
if (!match1[k] && (guess[j] == secret[k])) {
match1[k] = true;
match2[j] = true;
weak++;
break;
}
}
}
}
System.out.println(" (" + strong + "," + weak + ")");
st = new StringTokenizer(ReadLn(255));
for (int i = 0;i < len;i++)
guess = Integer.parseInt(st.nextToken());
}
games++;
line = ReadLn(255);
}
}
}[/java]
Why the above code gives WA? It works with the special cases mentioned in this forum, and also works with the sample input.
Re: 340 why WA?
I know nothing about Java...
But why "ReadLn(255);"??
Note that "The maximum value for N will be 1000"!!!
So the line may contain more than 255 characters!!
But why "ReadLn(255);"??
Note that "The maximum value for N will be 1000"!!!
So the line may contain more than 255 characters!!
Last edited by Observer on Mon Jul 07, 2003 10:57 am, edited 1 time in total.
7th Contest of Newbies
Date: December 31st, 2011 (Saturday)
Time: 12:00 - 16:00 (UTC)
URL: http://uva.onlinejudge.org
Date: December 31st, 2011 (Saturday)
Time: 12:00 - 16:00 (UTC)
URL: http://uva.onlinejudge.org
Re: 340 why WA?
Observer wrote:I know nothing about Java...
But why "ReadLn(255);"??
Note that "The maximum value for N will be 1000"!!!chunyi81 wrote:[java]import java.io.*;
import java.util.*;
class Main {
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);
return (new String (lin, 0, lg));
}
public static void main(String[] args) {
int games = 1;
String line = ReadLn(255);
while(line.charAt(0) != '0') {
System.out.println("Game " + games + ":");
StringTokenizer st = new StringTokenizer(line);
int len = Integer.parseInt(st.nextToken());
int[] secret = new int[len];
int[] guess = new int[len];
st = new StringTokenizer(ReadLn(255));
for (int i = 0;i < len;i++)
secret = Integer.parseInt(st.nextToken());
st = new StringTokenizer(ReadLn(255));
for (int i = 0;i < len;i++)
guess = Integer.parseInt(st.nextToken());
while(guess[0] != 0) {
int strong = 0;
int weak = 0;
boolean[] match1 = new boolean[len];
boolean[] match2 = new boolean[len];
for (int i = 0;i < len;i++) {
if (guess == secret) {
match1 = true;
match2 = true;
strong++;
}
}
for (int j = 0;j < len;j++) {
if (!match2[j]) {
for (int k = 0;k < len;k++) {
if (!match1[k] && (guess[j] == secret[k])) {
match1[k] = true;
match2[j] = true;
weak++;
break;
}
}
}
}
System.out.println(" (" + strong + "," + weak + ")");
st = new StringTokenizer(ReadLn(255));
for (int i = 0;i < len;i++)
guess = Integer.parseInt(st.nextToken());
}
games++;
line = ReadLn(255);
}
}
}[/java]
Why the above code gives WA? It works with the special cases mentioned in this forum, and also works with the sample input.
I know the max value of N is 1000. I used ReadLn(255) where ReadLn is a method defined to read from stdin(standard input) in Java. The number 255 means the max number of characters read per line.
Could u take a look at my code for problem 320 please? I could have misunderstood the problem 320. Thanks.
340 why WA
Never mind, i found my mistake and got AC already. Some of the ReadLn statements I had to increase the max length of the string read to 2100 because the input for the code can be up to 1000(numbers in a code of length 1000) + 999(spaces) = 1999 characters. Thanks.
340 - Master-Mind hints
Hi, I have been playing around with this problem for a while, and cannot find any example where my program is wrong, but still it is since the judge won't accept it ![;)](./images/smilies/icon_wink.gif)
[c]
#include <stdio.h>
int min(int a, int b){
if(a < b)
return a;
return b;
}
int main(){
int N,g=0,i,s,w;
char facit[1000];
char guess[1000];
int f_freq[10],g_freq[10];
while( scanf("%d",&N) == 1 && N > 0){
g++;
printf("Game %d:\n",g);
for(i = 0; i < N ; i++){
scanf("%d",&facit);
}
while(1){
for(i = 0; i < N ; i++){
scanf("%d",&guess);
}
if(guess[0] == 0) break;
s = w = 0;
[SNIP] Don't want to spoil all the fun
[/SNIP]
printf(" (%d,%d)\n",s,w);
}
}
}
[/c]
On an input file like
I get the following answers:
Which is the expected (?) my question is if someone could give me a counterexample?
Sorry for the long post, just felt like it would be fun to find the error...
/Hans
![;)](./images/smilies/icon_wink.gif)
[c]
#include <stdio.h>
int min(int a, int b){
if(a < b)
return a;
return b;
}
int main(){
int N,g=0,i,s,w;
char facit[1000];
char guess[1000];
int f_freq[10],g_freq[10];
while( scanf("%d",&N) == 1 && N > 0){
g++;
printf("Game %d:\n",g);
for(i = 0; i < N ; i++){
scanf("%d",&facit);
}
while(1){
for(i = 0; i < N ; i++){
scanf("%d",&guess);
}
if(guess[0] == 0) break;
s = w = 0;
[SNIP] Don't want to spoil all the fun
![;)](./images/smilies/icon_wink.gif)
printf(" (%d,%d)\n",s,w);
}
}
}
[/c]
On an input file like
Code: Select all
4
1 3 5 5
1 1 2 3
4 3 3 5
6 5 5 1
6 1 3 5
1 3 5 5
0 0 0 0
10
1 2 2 2 4 5 6 6 6 9
1 2 3 4 5 6 7 8 9 1
1 1 2 2 3 3 4 4 5 5
1 2 1 3 1 5 1 6 1 9
1 2 2 5 5 5 6 6 6 7
0 0 0 0 0 0 0 0 0 0
9
1 2 3 4 5 6 7 8 9
1 1 1 1 1 1 1 1 1
0 0 0 0 0 0 0 0 0
9
1 1 1 1 1 1 1 1 1
1 2 3 4 5 6 7 8 9
9 8 7 6 5 4 3 2 1
0 0 0 0 0 0 0 0 0
3
1 2 3
3 2 1
1 1 1
2 2 2
3 3 3
1 2 3
2 1 2
4 1 2
2 1 2
3 3 1
1 2 2
0 0 0
1
2
3
2
1
0
2
2 2
1 2
1 3
3 1
2 1
2 2
0 0
4
1 2 1 2
2 1 1 2
2 1 2 1
1 2 2 1
1 3 3 1
3 1 3 1
0 0 0 0
0
Code: Select all
Game 1:
(1,1)
(2,0)
(1,2)
(1,2)
(4,0)
Game 2:
(2,4)
(3,2)
(5,0)
(7,0)
Game 3:
(1,0)
Game 4:
(1,0)
(1,0)
Game 5:
(1,2)
(1,0)
(1,0)
(1,0)
(3,0)
(0,2)
(0,2)
(0,2)
(0,2)
(2,0)
Game 6:
(0,0)
(1,0)
(0,0)
Game 7:
(1,0)
(0,0)
(0,0)
(1,0)
(2,0)
Game 8:
(2,2)
(0,4)
(2,2)
(1,1)
(0,2)
Sorry for the long post, just felt like it would be fun to find the error...
/Hans
Last edited by dt96hasv on Wed Oct 20, 2004 1:20 pm, edited 1 time in total.
-
- Guru
- Posts: 1080
- Joined: Thu Dec 19, 2002 7:37 pm