## 10205 - Stack 'em Up

All about problems in Volume 102. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

ante
New poster
Posts: 8
Joined: Wed Mar 20, 2002 2:00 am

### 10205 - Stack 'em Up

I'm wondering if I 'm handleing input correctly, this is my code and problem has multiple test cases

Code: Select all

``````var
Tests, TestNO: integer;
br: byte;
Niz: string

Procedure ReadShuffle(br: byte);
var
n, i: byte;
begin
for n := 1 to br do
For i := 1 to 52 do
if i=52 then
readln(shuffle[n, i])
else
read(shuffle[n, i]);
end;

begin
readln(Tests);
readln;
for TestNO := 1 to Tests do
begin
GenerateDeck;
readln(br);
ReadShuffle(br);
repeat
readln(niz);
niz := trim(niz);
if niz<>'' then ShuffleCards(Niz);
until (niz='') or eof(input);
if TestNO<>1 then writeln;
GenerateOutput;
end;
end.
``````
If anyone has AC Pascal or C++ code for handleing this input would you post it, please
Shahid
Learning poster
Posts: 68
Joined: Fri Oct 26, 2001 2:00 am
Location: Dhaka, Bangladesh
Contact:

### where is the error

i solved this problem with the modified problem description. it all works good on the sample inout - output and seems ok, but the judge replies WA. can anyone help me to find otu teh glitch. here is my code. it may be seem lenghty. but if u cut out the pritnting portion then it's not so lengthy. thanx

[c]

/*@BEGIN_OF_SOURCE_CODE*/

#include<stdio.h>
#include<string.h>

void main()
{
int suf, n, k, deck, dupdeck, i, j, d, m, l, nm, Nn, p = 0, total;
char suite[] = {"Clubs", "Diamonds", "Hearts", "Spades"}, temp;

scanf("%d", &Nn);

for(p = 0; p < Nn; p++)
{
if(p)
printf("\n");

scanf("%d", &n);

memset(suf, 0, sizeof(suf));
memset(k, 0, sizeof(k));
memset(deck, 0, sizeof(deck));
memset(dupdeck, 0, sizeof(dupdeck));

for(i = 0; i < n; i++)
for(j = 0; j < 52; j++)
scanf("%d", &suf[j]);

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

getchar();
i = 0;
while(gets(temp) && sscanf(temp,"%d", &k) == 1)
i++;

total = i;
i = 0;

if(k)
{
j = k - 1;

for(d = 0; d < 52; d++)
deck[d] = suf[j][d];
}

i++;
memcpy(dupdeck, deck, sizeof(deck));

j = 0;
while(j < total-1)
{
nm = k - 1;

for(d = 0; d < 52; d++)
{
l = suf[nm][d];
m = dupdeck[d];
deck[l-1] = m;
}
i++;
j++;
memcpy(dupdeck, deck, sizeof(deck));
}

// printing portion(i don't inlcude this line in the time of submission)

for(i = 0; i < 52; i++)
{
m = deck;

if(m > 0 && m < 14)
{
l = 0;
if(m < 10)
printf("%d of %s\n", m+1, suite[l]);
else
{
if(m == 10)
printf("Jack of %s\n", suite[l]);
else if(m == 11)
printf("Queen of %s\n", suite[l]);
else if(m == 12)
printf("King of %s\n", suite[l]);
else if(m == 13)
printf("Ace of %s\n", suite[l]);
}
}

else if(m > 13 && m < 27)
{
l = 1;
m = m - 13;
if(m < 10)
printf("%d of %s\n", m+1, suite[l]);
else
{
if(m == 10)
printf("Jack of %s\n", suite[l]);
else if(m == 11)
printf("Queen of %s\n", suite[l]);
else if(m == 12)
printf("King of %s\n", suite[l]);
else if(m == 13)
printf("Ace of %s\n", suite[l]);
}
}

else if(m > 26 && m < 40)
{
l = 2;
m = m - 26;

if(m < 10)
printf("%d of %s\n", m+1, suite[l]);
else
{
if(m == 10)
printf("Jack of %s\n", suite[l]);
else if(m == 11)
printf("Queen of %s\n", suite[l]);
else if(m == 12)
printf("King of %s\n", suite[l]);
else if(m == 13)
printf("Ace of %s\n", suite[l]);
}
}

else if(m > 39 && m < 53)
{
l = 3;

m = m - 39;
if(m < 10)
printf("%d of %s\n", m+1, suite[l]);
else
{
if(m == 10)
printf("Jack of %s\n", suite[l]);
else if(m == 11)
printf("Queen of %s\n", suite[l]);
else if(m == 12)
printf("King of %s\n", suite[l]);
else if(m == 13)
printf("Ace of %s\n", suite[l]);
}
}
}
}
}

/*@END_OF_SOURCE_CODE*/

[/c]
htl
Experienced poster
Posts: 185
Joined: Fri Jun 28, 2002 12:05 pm
Location: Taipei, Taiwan

### 10205

I think it's a really simple problem. But it's too easy to solve it(??). Can someone execute my program and check what's wrong with it??

[c]
#include<stdio.h>
#define CLUB 53
#define DIAMOND 59
#define HEART 61
#define SPADE 67
void main(void)
{
int card,count,x,shuffle,n,y,z,k,temp,num;
char s;
for(x=0;x<12;x++)
num[x]=x+2;
num=1;
scanf("%d",&count);
for(x=0;x<count;x++)
{
if(x)
printf("\n");
scanf("%d",&n);
for(y=0;y<n;y++)
for(z=0;z<52;z++)
scanf("%d",&shuffle[y][z]);
for(z=0;z<52;z++)
{
if((z+1)%13==0)
card[z]=1;
else
card[z]=num[(z+1)%13-1];
if(z<=12)
card[z]*=CLUB;
else if(z>=13 && z<=25)
card[z]*=DIAMOND;
else if(z>=26 && z<=38)
card[z]*=HEART;
else
card[z]*=SPADE;
}
gets(s);
while(gets(s)!=NULL)
{
if(!s)
break;
for(y=0,k=0;s[y]!='\0';y++)
if(s[y]>='0' && s[y]<='9')
k=k*10+s[y]-'0';
for(z=0;z<52;z++)
temp[shuffle[k-1][z]-1]=card[z];
for(z=0;z<52;z++)
card[z]=temp[z];
}
for(z=0;z<52;z++)
if(temp[z]%CLUB==0)
{
temp[z]/=CLUB;
if(temp[z]==1)
printf("Ace of Clubs\n");
else if(temp[z]==11)
printf("Jack of Clubs\n");
else if(temp[z]==12)
printf("Queen of Clubs\n");
else if(temp[z]==13)
printf("King of Clubs\n");
else
printf("%d of Clubs\n",temp[z]);
}
else if(temp[z]%DIAMOND==0)
{
temp[z]/=DIAMOND;
if(temp[z]==1)
printf("Ace of Diamonds\n");
else if(temp[z]==11)
printf("Jack of Diamonds\n");
else if(temp[z]==12)
printf("Queen of Diamonds\n");
else if(temp[z]==13)
printf("King of Diamonds\n");
else
printf("%d of Diamonds\n",temp[z]);
}
else if(temp[z]%HEART==0)
{
temp[z]/=HEART;
if(temp[z]==1)
printf("Ace of Hearts\n");
else if(temp[z]==11)
printf("Jack of Hearts\n");
else if(temp[z]==12)
printf("Queen of Hearts\n");
else if(temp[z]==13)
printf("King of Hearts\n");
else
printf("%d of Hearts\n",temp[z]);
}
else
{
temp[z]/=SPADE;
if(temp[z]==1)
printf("Ace of Spades\n");
else if(temp[z]==11)
printf("Jack of Spades\n");
else if(temp[z]==12)
printf("Queen of Spades\n");
else if(temp[z]==13)
printf("King of Spades\n");
else
printf("%d of Spades\n",temp[z]);
}
}
}
[/c]
Sebasti
New poster
Posts: 10
Joined: Sun Apr 13, 2003 11:41 pm
Contact:
Hi htl,

I has the same error because the problem description that is not good.
Change the line:
[cpp]temp[shuffle[k-1][z]-1]=card[z];[/cpp]
by
[cpp]temp[z]=card[shuffle[k-1][z]-1];[/cpp]

Best regards,

Sebasti
PJYelton
New poster
Posts: 20
Joined: Fri May 30, 2003 6:56 pm

### 10205 WA but seems right to me

Hi, I am trying to do Stack 'em Up and it seems real easy but I keep getting wrong answer from the judge even though when I do my tests I always seem to get the correct answer. If I had to guess, it is becuase my program isn't recognizing the end of file at the end of the input. Now I am very new to C++ and to be honest I'm not sure how to account for that. I have no problem with the blank line between inputs, but I'm not sure if it is processing the last case given by the judge. Could someone tell me how to account for this (using C++ and not C hopefully!) Here is my code, if someone sees another possible error, please let me know!
[cpp]
/* @JUDGE_ID: 32250TW 10205 C++ "Simple Iteration" */
/* @BEGIN_OF_SOURCE_CODE */
#include <iostream>
#include <vector>
#include<string>
using namespace std;
struct card
{
string suit,name;
};
int numReturn(string s)
{
int i=0;
for (int x=s.length()-1,y=1; x>=0; x--,y*=10)
i+=((s[x]-'0')*y);
return i;
}

int main()
{
int cases, nShuff,x,y,z;
string buff;
cin>>cases;
cin.ignore(1000,'\n');
getline(cin,buff);
for (x=0; x<cases; x++)
{
vector<card> deck;
vector<card> pdeck;
card c;
deck.push_back(c);
for (y=0; y<52; y++)
{
c.name="";
c.suit="";
if (y/13==0)
c.suit="Clubs";
if (y/13==1)
c.suit="Diamonds";
if (y/13==2)
c.suit="Hearts";
if (y/13==3)
c.suit="Spades";
if (y%13==12)
c.name="Ace";
else if (y%13==11)
c.name="King";
else if (y%13==10)
c.name="Queen";
else if (y%13==9)
c.name="Jack";
else if (y%13==8)
c.name="Ten";
else
c.name+=((y%13)+'2');
deck.push_back(c);
}

cin>>nShuff;
vector<int> shuffle;
for (y=1; y<=nShuff; y++)
{
shuffle[y].push_back(0);
for (z=0; z<52; z++)
{
int i;
cin>>i;
shuffle[y].push_back(i);
}
}

pdeck=deck;
cin.ignore(1000,'\n');
while (!cin.eof())
{
getline(cin,buff);
if (buff=="")
break;
int i=numReturn(buff);
for (y=1; y<=52; y++)
{
deck[y]=pdeck[shuffle[y]];
}
pdeck=deck;
}
for (y=1; y<=52; y++)
cout<<deck[y].name<<" of "<<deck[y].suit<<endl;
if (x!=cases-1)
cout<<endl;
}
return 0;
}
/* @END_OF_SOURCE_CODE */ [/cpp]
PJYelton
New poster
Posts: 20
Joined: Fri May 30, 2003 6:56 pm
I got it figured out. Someone pointed out that I was writing "Ten" instead of "10", so now it works.
czar
New poster
Posts: 15
Joined: Tue Jun 10, 2003 7:30 pm

### 10205

Could someone please tell me why this won't compile?
note: It compiles fine under the version of gcc that comes with latest stable version mingw.

[cpp]
#include<iostream>
#include<string>
#include<algorithm>
#include<vector>

using namespace std;

const int NUMCARDS = 52;

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

vector<string> decode(const vector<int> &curr);
void applyShuff(vector<int> &curr, vector<int> &last, vector<int> &shuff);
void readShuff(vector< vector<int> > &shuffles);
void printCards(string card);
void init(vector<int> &curr, vector<int> &last);

int main(void) {

int numCases;
cin>>numCases;

for(int i = 0; i < numCases; i++) {
int numShuff;
int shuff;
vector<int> curr, last;
vector< vector<int> > shuffles;
init(curr, last);

cin>>numShuff;

for(int i = 0; i < numShuff; i++)
readShuff(shuffles);

for(int i = 0; i < numShuff; i++) {
cin>>shuff;
applyShuff(curr, last, shuffles[shuff-1]);
}

vector<string> finalcards = decode(curr);
for_each(finalcards.begin(), finalcards.end(), printCards);
}
return 0;
}

vector<string> decode(const vector<int> &encoded){
vector<string> ret;
int len = encoded.size();
for(int i = 0; i < len; i++)
ret.push_back(string(cards[(encoded-1)%13]) + " of " + string(suits[(encoded-1)/13]));

return ret;
}

void applyShuff(vector<int> &curr, vector<int> &last, vector<int> &shuff){

int len = shuff.size();
for(int i = 0; i < len; i++)
curr = last[shuff-1];
last = curr;
}
void readShuff(vector< vector<int> > &shuffles) {
int card;
vector<int> shuff;
for(int i = 0; i < NUMCARDS; i++) {
cin>>card; getchar();
shuff.push_back(card);
}
shuffles.push_back(shuff);
}

void printCards(string card) {
cout<<card<<endl;
}

void init(vector<int> &curr, vector<int> &last) {

for(int i = 1; i <= 52; i++) {

curr.push_back(i);
last.push_back(i);
}
}
[/cpp]
danielrocha
New poster
Posts: 44
Joined: Sun Apr 27, 2003 3:17 am
Location: Rio Grande do Norte - Brazil
Contact:

### Include stdio

The only problem in you code is that you forgot to include the "stdio" C library. All you've got to do is add this at the beginning of your code:

[cpp]#include <cstdio>
[/cpp]

But it appears you have a presentation error.. Don't forget to include a blank line between two different outputs and you're ok, I guess!
Hope I've helped,

------
Daniel "Naz
czar
New poster
Posts: 15
Joined: Tue Jun 10, 2003 7:30 pm
Thanks Daniel.
zacharyleung
New poster
Posts: 14
Joined: Tue Feb 03, 2004 3:43 am

### Java input issues... (ref Problem 10205)

This is my first post! Anyway, I know this is a pretty simple problem, but I can't figure out how to read in the input correctly. I'm pretty confident that my algorithm works. Because I can't always read in the input correctly, I'm having problems with a few other problems too. I'd appreciate it if someone could help me out on this!

[java]

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

class Main {
// utility function to read from stdin
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)).trim();
} //end of readLn

// entry point from OS
public static void main (String args[]) {
Main myWork = new Main(); // create a dinamic instance
myWork.begin(); // the true entry point
} //end of main

public static final int CARD_SIZE = 53;
public static final int BIG_SIZE = 100000;

void begin() {
String input;
StringTokenizer idata;
int number_of_cases;
int number_of_shuffles;
int[][] shuffles;
int shuffle_num;
int[] cards;

/*
//Testing code for unrank()
for( int i = 1; i <= 52; i++ ) {
System.out.println( unrank( i ) );
}
*/

idata = new StringTokenizer( Main.readLn( 255 ) );
number_of_cases = Integer.parseInt( idata.nextToken() );
Main.readLn( 255 );

for( int case_num = 0; case_num < number_of_cases; case_num++ ) {
idata = new StringTokenizer( Main.readLn( 255 ) );
number_of_shuffles = Integer.parseInt( idata.nextToken() );
cards = new int[ CARD_SIZE ];
for( int i = 1; i <= 52; i++ )
cards[ i ] = i;

//NOTE: The first shuffle starts from index 1, and the first real data starts from index 1
shuffles = new int[ number_of_shuffles+1 ][ CARD_SIZE ];
idata = new StringTokenizer( Main.readLn( BIG_SIZE ) );
for( int i = 1; i <= number_of_shuffles; i++ ) {
for( int j = 1; j <= 52; j++ )
shuffles[ i ][ j ] = Integer.parseInt (idata.nextToken());
} //end of for i

while( true ) {
input = Main.readLn( 255 );
idata = new StringTokenizer( input );
if( !idata.hasMoreTokens() )
break;
shuffle_num = Integer.parseInt (idata.nextToken());

int[] new_cards = new int[ CARD_SIZE ];
for( int i = 1; i <= 52; i++ )
new_cards[ i ] = cards[ shuffles[ shuffle_num ][ i ] ];
cards = new_cards;
} //end of while

for( int i = 1; i <= 52; i++ )
System.out.println( unrank( cards[ i ] ) );

System.out.println();
} //end of for case
} //end of begin

/** the ranks go from 1 to 52 inclusive */
String unrank( int rank ) {
String result = "";

int number = (rank + 1) % 13;
switch( number ) {
case 2: case 3: case 4: case 5: case 6:
case 7: case 8: case 9: case 10:
result += number;
break;
case 11:
result = "Jack";
break;
case 12:
result = "Queen";
break;
case 0:
result = "King";
break;
case 1:
result = "Ace";
break;
default:
System.out.println( "error in unrank's first switch" );
break;
} //end of switch

result += " of ";

switch( (rank-1) / 13 ) {
case 0:
result += "Clubs";
break;
case 1:
result += "Diamonds";
break;
case 2:
result += "Hearts";
break;
case 3:
result += "Spades";
break;
default:
System.out.println( "error in unrank's second switch" );
break;
} //end of switch

return result;
} //end of unrank
} //end of Main

[/java]
grirus
New poster
Posts: 12
Joined: Sat Mar 13, 2004 6:15 pm
Location: Sampa - Brazil
Contact:

### 10205

Please, i
thanx
sohel
Guru
Posts: 856
Joined: Thu Jan 30, 2003 5:50 am
Location: New York

### confusing

Hello grirus,

I wondered how your code avoided RTE and got WA.
....so I sent your code and the reply was RTE.... and it should be.

BTW why do you have to use sscanf() for this program.....
... you only use it when you don't know the number of inputs.....
.. but for this program you do know it ( namely 52n )

make the modification and try you luck.
grirus
New poster
Posts: 12
Joined: Sat Mar 13, 2004 6:15 pm
Location: Sampa - Brazil
Contact:
Thanx... I
thanx
noren
New poster
Posts: 3
Joined: Tue Nov 18, 2003 1:07 pm
Location: Ume

### 10205 TLE

Got TLE on this one. Thought that you could simply simulate the shuffling of the deck. Anyone knows if I just ran into an infinte loop (due to bad programming), or if you actually must do something more clever than brute force? It feels like a brute force problem =)

/johan
Per
A great helper
Posts: 429
Joined: Fri Nov 29, 2002 11:27 pm
Location: Sweden
I have to go with the infinite loop choice. Make sure that you handle input files where there are several cases correctly (i.e. that you skip exactly the one blank line between test cases, etc).