## 10205 - Stack 'em Up

Moderator: Board moderators

Darko
Guru
Posts: 580
Joined: Fri Nov 11, 2005 9:34 am
Sorry, it only *looked* like it worked, I should've checked the input specification.

You probably want to use fgets(), I am not that familiar with C, maybe you should check C forum - I bet someone asked that question already. So, no, I don't have that much experience

I thought you were someone from U of C, no, I probably don't know you

Darko

chaturvedi
New poster
Posts: 8
Joined: Mon Jul 10, 2006 9:31 am

### 10205 Wrong Answer!! Help me!!

I am getting WA for my Code!! The problem seems to be quite easy one and my code is quite starght forward... Help me please!! Here's my code :

#include <stdio.h>
#include <string.h>
int a[150][60];
void print(void);
void change(int shuffle);
void copy(int shuffle);
void main()
{
int n,i,k,count,j,shuffle,t;

scanf("%d",&t);
printf("\n");
for(k=1;k<=t;k++)
{
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=52;j++)
scanf("%d",&a[j]);
}
count=1;
while(scanf("%d",&shuffle)==1)
{
if(count==1)
copy(shuffle);
else
change(shuffle);
count++;
}
print();
printf("\n\n");
}
}

void change(int shuffle)
{
int b[53],i;
for(i=1;i<=52;i++)
b=a[0];
for(i=1;i<=52;i++)
a[0]=b[a[shuffle]];
}

void copy(int shuffle)
{
int i;
for(i=1;i<=52;i++)
a[0]=a[shuffle];
}

void print(void)
{
int i,value,suit;
char suitname[10],value1[8];
for(i=1;i<=52;i++)
{
value=a[0]%13;
suit=(a[0]-1)/13;

switch(suit)
{
case 0:
strcpy(suitname,"Clubs");
break;
case 1:
strcpy(suitname,"Diamonds");
break;
case 2:
strcpy(suitname,"Hearts");
break;
case 3:
break;
}
if(value<10 && value!=0)
printf("\n%d of %s",value+1,suitname);
else
{
switch(value)
{
case 10:
strcpy(value1,"Jack");
break;
case 11:
strcpy(value1,"Queen");
break;
case 12:
strcpy(value1,"King");
break;
case 0:
strcpy(value1,"Ace");
break;
}
printf("\n%s of %s",value1,suitname);
}
}
}

missbus
New poster
Posts: 3
Joined: Tue Feb 27, 2007 3:44 pm

### 10205

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

public class Main{

public static void main(String[] args)throws Exception {
Scanner cin=new Scanner(System.in);

int choose=0;
int []original=new int[52];
int []position=new int[52];
int []result=new int[52];
String suit="",value="";
String [][]ans=new String[100][52];

for(int i=0;i<52;i++)
original=i+1;

for(int i=0;i<times;i++)
{ for(int j=0;j<26;j++)
{
position[j]= cin.nextInt();
}
for(int j=26;j<52;j++)
{
position[j]= cin.nextInt();
}
for(int k=0;k<52;k++)
{
result[k]=original[position[k]-1];
}
for(int l=0;l<52;l++)
{
switch((result[l]-1)/13)
{
case 0:
suit="Clubs";
break;
case 1:
suit="Diamonds";
break;
case 2:
suit="Hearts";
break;
default:
}

switch( result[l]%13)
{
case 0:
value="Ace";
break;
case 10:
value="Jack";
break;
case 11:
value="Queen";
break;
case 12:
value="King";
break;
default:
value=""+(1+result[l]);
}
ans[l]=value+" of "+suit;
// System.out.println(value+" of "+suit);
}
for(int s=0;s<52;s++)
{
original[s]=result[s];
}
}
for(;;)
{
System.out.println();
for(int i=0;i<52;i++)
{
System.out.println( ans[choose-1]);
}
}
}

}

I don't know what's wrong with my code,please tell me the reason.Thanks a lot.

Darko
Guru
Posts: 580
Joined: Fri Nov 11, 2005 9:34 am
You cannot use most of the java.io.* on UVa, including BufferedReader.

missbus
New poster
Posts: 3
Joined: Tue Feb 27, 2007 3:44 pm
How do I modify this problem,if I don't use java.io,I can't input anything....

Darko
Guru
Posts: 580
Joined: Fri Nov 11, 2005 9:34 am
There is stuff on this cluttered site, I guess it is hard to find for someone that is first time here.

I said "most", of course you have to use io, at least indirectly (System's in and out are io streams). I use this to read lines:

Code: Select all

``````private String readLine() {
StringBuffer sb = new StringBuffer();
int b = 0;
while (b != '\n' && b >= 0) {
try {
} catch (IOException e) {
return null;
}
if (b != '\r' && b != '\n' && b >= 0)
sb.append((char) b);
}
if (sb.length() == 0 && b < 0)
return null;
return sb.toString().trim(); // remove trim() if you need whitespace
}
``````

dag
New poster
Posts: 4
Joined: Fri Mar 02, 2007 2:50 am

### 10205

Hi, I am a Java user and have absolutely no idea why I get WA.

I would really appreciate it if somebody could tell me if there is any test case that makes this code work improperly.

Thank you

Code: Select all

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

class Main implements Runnable{
// Provided by Programming-challenges, edit for style only
byte line[] = new byte [maxLength];
int length = 0;
int input = -1;
try{
while (length < maxLength){//Read untill maxlength
if ((input < 0) || (input == '\n')) break; //or untill end of line ninput
line [length++] += input;
}

if ((input < 0) && (length == 0)) return null;  // eof
return new String(line, 0, length);
}catch (IOException e){
return null;
}
}

public static void main(String args[])  // entry point from OS
{
Main myWork = new Main();  // Construct the bootloader
myWork.run();            // execute
}

public void run() {
new myStuff().run();
}
}
class myStuff implements Runnable{
public void run(){

// read number of test cases
String input;
StringTokenizer iData;
iData = new StringTokenizer(input);

String userInput = iData.nextToken();
int cases = Integer.parseInt(userInput);

input = new String(" ");

// for test cases
for(int a =0; a< cases; a++){

iData = new StringTokenizer(input);
userInput  = iData.nextToken();

int shuffles = Integer.parseInt(userInput);

if (shuffles > 0){

int [][] shuffleArray = new int[shuffles][52];

// for shuffles
for(int b = 0; b < shuffles; b++){

if(iData.hasMoreTokens() == false){
iData = new StringTokenizer(input);
}

for(int c= 0; c < 52; c++){
if(iData.hasMoreTokens() == false){
iData = new StringTokenizer(input);
}
userInput = iData.nextToken();
shuffleArray [b][c] = Integer.parseInt(userInput);
}

}

Vector hand = new Vector();

iData = new StringTokenizer(input);

while(iData.hasMoreTokens()){
int temp = Integer.parseInt(iData.nextToken());
Integer number = new Integer(temp);

iData = new StringTokenizer(input);

}

myStuff.sortDeck(shuffleArray, hand);

if(a < (cases-1) )
System.out.println();

}

else{
int [] deck = new int[52];

for ( int i=0; i< 52; i++)
deck[i] = (i + 1);
myStuff.printDeck(deck);

if(a < (cases-1) )
System.out.println();

}

if(a < (cases-1) )
System.out.println();
}

}

StringBuffer sb = new StringBuffer();
int b = 0;
while (b != '\n' && b >= 0) {
try {
} catch (IOException e) {
return null;
}
if (b != '\r' && b != '\n' && b >= 0)
sb.append((char) b);
}
if (sb.length() == 0 && b < 0)
return null;
return sb.toString();  // .trim();
}

static String suit(int card){

if(card < 14)
return(suits[0]);
else if(card <27)
return(suits[1]);
else if(card <40)
return(suits[2]);
else  return(suits[3]);

}

static String value(int card){

int thiOut = (card-1) % 13;

return( values[thiOut]);
}

static String values [] = {"2","3","4","5","6","7","8","9","10","Jack","Queen","King","Ace"};
static String suits [] = {"Clubs","Diamonds","Hearts","Spades"};

static void sortDeck(int [][] shuffleArray, Vector hand){

int [] deck = new int[52];

for ( int i=0; i< 52; i++)
deck[i] = (i + 1);

while(hand.size() != 0){
int [] tempDeck = new int[52];
Object okay = hand.elementAt(0);
Integer nothing = (Integer) okay;
int temp = nothing.intValue();
hand.removeElementAt(0);

for(int y=0; y<52; y++){

int card = shuffleArray[temp -1][y];

tempDeck[y] = deck[card -1];

}

deck = tempDeck;
}

myStuff.printDeck(deck);
}

static void printDeck(int[] deck){

for( int g=0; g<52; g++){

String value = myStuff.value(deck[g]);

System.out.print(value);

System.out.print(" of ");

String suit = myStuff.suit(deck[g]);

if(g < 51)
System.out.println(suit);
else
System.out.print(suit);

}

}

static int[] reserve(int[] hand){

int size = (hand.length * 100);
int [] array = new int[size];

for(int u =0; u < hand.length; u++)
array[u] = hand[u];

return array;
}

}

``````

Darko
Guru
Posts: 580
Joined: Fri Nov 11, 2005 9:34 am
Interesting... that compiles? Why are you implementing Runnable if you are running a single thread? Hm, I have to check if I can use threads after all...

Anyway - I haven't checked your code, but WA with Java might mean anything on UVa, including Run Time Error. [EDIT] It crashes with more than one test case.

And - next time, use already existing threads, don't create new ones.

dag
New poster
Posts: 4
Joined: Fri Mar 02, 2007 2:50 am
Darko wrote:Interesting... that compiles? Why are you implementing Runnable if you are running a single thread? Hm, I have to check if I can use threads after all...

Anyway - I haven't checked your code, but WA with Java might mean anything on UVa, including Run Time Error. [EDIT] It crashes with more than one test case.

And - next time, use already existing threads, don't create new ones.
Sorry about that, but could you please elaborate on "crashes with more than one test case."

When I run it at home this is what I get when I use three cases

Code: Select all

``````3

2
2 1 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 52 51
52 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 1
1
2

2 of Clubs
4 of Clubs
5 of Clubs
6 of Clubs
7 of Clubs
8 of Clubs
9 of Clubs
10 of Clubs
Jack of Clubs
Queen of Clubs
King of Clubs
Ace of Clubs
2 of Diamonds
3 of Diamonds
4 of Diamonds
5 of Diamonds
6 of Diamonds
7 of Diamonds
8 of Diamonds
9 of Diamonds
10 of Diamonds
Jack of Diamonds
Queen of Diamonds
King of Diamonds
Ace of Diamonds
2 of Hearts
3 of Hearts
4 of Hearts
5 of Hearts
6 of Hearts
7 of Hearts
8 of Hearts
9 of Hearts
10 of Hearts
Jack of Hearts
Queen of Hearts
King of Hearts
Ace of Hearts
3 of Clubs

2
2 1 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
27 28 29 30 31 32 33 34 35 36 37 38 39 49 41 42 43 44 45 46 47 48 40 50 52 51
52 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 1
1
2

2 of Clubs
4 of Clubs
5 of Clubs
6 of Clubs
7 of Clubs
8 of Clubs
9 of Clubs
10 of Clubs
Jack of Clubs
Queen of Clubs
King of Clubs
Ace of Clubs
2 of Diamonds
3 of Diamonds
4 of Diamonds
5 of Diamonds
6 of Diamonds
7 of Diamonds
8 of Diamonds
9 of Diamonds
10 of Diamonds
Jack of Diamonds
Queen of Diamonds
King of Diamonds
Ace of Diamonds
2 of Hearts
3 of Hearts
4 of Hearts
5 of Hearts
6 of Hearts
7 of Hearts
8 of Hearts
9 of Hearts
10 of Hearts
Jack of Hearts
Queen of Hearts
King of Hearts
Ace of Hearts
3 of Clubs

2
2 1 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 52 51
52 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
27 28 29 30 31 50 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 32 51 1
1
2

2 of Clubs
4 of Clubs
5 of Clubs
6 of Clubs
7 of Clubs
8 of Clubs
9 of Clubs
10 of Clubs
Jack of Clubs
Queen of Clubs
King of Clubs
Ace of Clubs
2 of Diamonds
3 of Diamonds
4 of Diamonds
5 of Diamonds
6 of Diamonds
7 of Diamonds
8 of Diamonds
9 of Diamonds
10 of Diamonds
Jack of Diamonds
Queen of Diamonds
King of Diamonds
Ace of Diamonds
2 of Hearts
3 of Hearts
4 of Hearts
5 of Hearts
6 of Hearts
8 of Hearts
9 of Hearts
10 of Hearts
Jack of Hearts
Queen of Hearts
King of Hearts
Ace of Hearts
7 of Hearts
3 of Clubs``````
I do not see any crashing.

Darko
Guru
Posts: 580
Joined: Fri Nov 11, 2005 9:34 am
I am not sure what you posted there.

I copied your Main.java, I just made 2 cases out of that sample one, and it crashes. Maybe you misunderstood something, but this is what the input file would look like:

Code: Select all

``````2

2
2 1 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 52 51
52 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 1
1
2

2
2 1 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 52 51
52 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 1
1
2
``````

dag
New poster
Posts: 4
Joined: Fri Mar 02, 2007 2:50 am
Darko wrote:I am not sure what you posted there.

I copied your Main.java, I just made 2 cases out of that sample one, and it crashes. Maybe you misunderstood something, but this is what the input file would look like:

Code: Select all

``````2

2
2 1 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 52 51
52 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 1
1
2

2
2 1 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 52 51
52 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 1
1
2
``````
What compiler are you using?

I am using the latest jdk.

I just copied your thing and I get this for an answer. No crash

Code: Select all

``````2

2
2 1 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 52 51
52 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 1
1
2

2 of Clubs
4 of Clubs
5 of Clubs
6 of Clubs
7 of Clubs
8 of Clubs
9 of Clubs
10 of Clubs
Jack of Clubs
Queen of Clubs
King of Clubs
Ace of Clubs
2 of Diamonds
3 of Diamonds
4 of Diamonds
5 of Diamonds
6 of Diamonds
7 of Diamonds
8 of Diamonds
9 of Diamonds
10 of Diamonds
Jack of Diamonds
Queen of Diamonds
King of Diamonds
Ace of Diamonds
2 of Hearts
3 of Hearts
4 of Hearts
5 of Hearts
6 of Hearts
7 of Hearts
8 of Hearts
9 of Hearts
10 of Hearts
Jack of Hearts
Queen of Hearts
King of Hearts
Ace of Hearts
3 of Clubs

2
2 1 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 52 51
52 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 1
1
2

2 of Clubs
4 of Clubs
5 of Clubs
6 of Clubs
7 of Clubs
8 of Clubs
9 of Clubs
10 of Clubs
Jack of Clubs
Queen of Clubs
King of Clubs
Ace of Clubs
2 of Diamonds
3 of Diamonds
4 of Diamonds
5 of Diamonds
6 of Diamonds
7 of Diamonds
8 of Diamonds
9 of Diamonds
10 of Diamonds
Jack of Diamonds
Queen of Diamonds
King of Diamonds
Ace of Diamonds
2 of Hearts
3 of Hearts
4 of Hearts
5 of Hearts
6 of Hearts
7 of Hearts
8 of Hearts
9 of Hearts
10 of Hearts
Jack of Hearts
Queen of Hearts
King of Hearts
Ace of Hearts
3 of Clubs``````

Darko
Guru
Posts: 580
Joined: Fri Nov 11, 2005 9:34 am
Again - what is the thing you are posting? Is that the output of your program? Can you describe how you test your solution (in detail)?

And - having the latest JDK doesn't help here - I am compiling it in Eclipse to target 1.3. Java here is 1.1, but 1.3 is the lowest Eclipse can do.

Anyway - I put that input into the file called "in", compile Main.java and run it with "java Main < in". I am not sure why you don't want to accept the fact that it crashes?

I shouldn't be debugging this for you - but here it is: You assume that the file ends with a blank line. It does not. And it might not end with anything by EOF. And - I don't think it would get accepted anyway, seems like you are missing a newline at the end.

dag
New poster
Posts: 4
Joined: Fri Mar 02, 2007 2:50 am
Darko wrote:Again - what is the thing you are posting? Is that the output of your program? Can you describe how you test your solution (in detail)?

And - having the latest JDK doesn't help here - I am compiling it in Eclipse to target 1.3. Java here is 1.1, but 1.3 is the lowest Eclipse can do.

Anyway - I put that input into the file called "in", compile Main.java and run it with "java Main < in". I am not sure why you don't want to accept the fact that it crashes?

I shouldn't be debugging this for you - but here it is: You assume that the file ends with a blank line. It does not. And it might not end with anything by EOF. And - I don't think it would get accepted anyway, seems like you are missing a newline at the end.
Unbelievable. I never would have thought it possible. Well you were right, using an input file does crash the program.

You see I have only been using the cmd window and simply copied and pasted input into it. It was never a problem for me so far as I already solved a few problems using only the cmd window.

I had no idea I was supposed to be using an input file.

I would like to show my appreciation by saying thank you.

Carunty
New poster
Posts: 18
Joined: Sat Jul 08, 2006 2:40 am
Can paste some testcase here?
I get WA in this problem with reason unknow.

etkuo
New poster
Posts: 1
Joined: Tue Mar 18, 2008 3:20 am

### Re: Java WA 10205 stack em' up

Code: Select all

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

class Question5 {
static ArrayList<Integer> deck;
static ArrayList<String[]> shuffleList;
static String[] shuffleForm;
static String[] cardNum = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace"};

public static void main (String[] args) throws IOException {
//for every set of input
for (int i=0; i<n; i++) {
initDeck();
test();
toText();
System.out.println();
shuffleList.clear();
shuffleForm = null;
}
}

//initialize deck
public static void initDeck() {
deck = new ArrayList<Integer>(52);
deck.clear();
for (int i=1; i<=52; i++) {
}
}

public static void readShuffle() throws IOException {
shuffleList = new ArrayList<String[]>(numShuffle);
for (int i=0; i<numShuffle; i++) {
line += " " + br.readLine();
StringTokenizer st = new StringTokenizer(line, " ");
shuffleForm = new String[52];
for (int j=0; j<shuffleForm.length; j++) {
shuffleForm[j] = st.nextToken();
}
}
}

//shuffle process
public static void test() throws IOException {
int count = 0;
int shuffleNum = Integer.parseInt(input);
shuffle(shuffleNum);
count++;
input = null;
}
}

//shuffle the deck
public static void shuffle(int shuffleNum) {
String[] shuffleUsed = shuffleList.get(shuffleNum-1);
ArrayList<Integer> newDeck = new ArrayList<Integer>(52);
for (int i=0; i<shuffleUsed.length; i++) {
int pos = Integer.parseInt(shuffleUsed[i]);
}
deck = newDeck;
}

//convert card from number to text
public static void toText() {
for (int i=0; i<deck.size(); i++) {
String suit;
int card = deck.get(i);
if (card<13) {
suit = "Clubs";
}
else if (card<26) {
suit = "Diamonds";
}
else if (card<39) {
suit = "Hearts";
}
else {
}
while (card>13) {
card-=13;
}
System.out.println(cardNum[card-1] + " of " + suit);
}
}
}``````
Can someone help me too? I have tried multiple input and all the stuffs and it runs well. But when I submit it says Runtime error. Please someone help me...