## 10978 - Let's Play Magic!

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

Moderator: Board moderators

shihabrc
New poster
Posts: 49
Joined: Sun Sep 18, 2005 10:20 am
Location: CSE,BUET
Contact:

### 10978 - Let's Play Magic!

Though its a quite easy simulation prob, I'm getting WA 4 this. Can any1 hlp ma(I/O,suggestions, anything).

Code: Select all

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

using namespace std;

#define MAX 52

int pos[MAX+1];
string card[MAX+1];
string placed[MAX+1];
bool flag[MAX+1];

void main()
{
int N,i,j,k,current;
char input[50];

//	freopen("C:\\4.txt","r",stdin);

while(scanf("%d",&N)==1 && N)
{
getchar();

for(i=1;i<=N;i++)
{
gets(input);
card[i]=strtok(input," ");
pos[i]=strlen(strtok(NULL," "));
flag[i]=false;
}

current=0;

for(i=1;i<=N;i++)
{
j=current;k=0;

while(k<pos[i])
{
j++;
if(j>N) j%=N;
if(!flag[j]) k++;
else continue;
}

current=j;

placed[j]=card[i];
flag[j]=true;

}

for(i=1;i<=N;i++)
{
if(i==1) printf("%s",placed[i].c_str());
else printf(" %s",placed[i].c_str());
}

putchar('\n');
}
}
Shihab
CSE,BUET

DD
Experienced poster
Posts: 145
Joined: Thu Aug 14, 2003 8:42 am
Location: Mountain View, California
Contact:

### Re: 10978 - Let's Play Magic!

shihabrc wrote:Though its a quite easy simulation prob, I'm getting WA 4 this. Can any1 hlp ma(I/O,suggestions, anything).

Code: Select all

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

using namespace std;

#define MAX 52

int pos[MAX+1];
string card[MAX+1];
string placed[MAX+1];
bool flag[MAX+1];

void main()
{
int N,i,j,k,current;
char input[50];

//	freopen("C:\\4.txt","r",stdin);

while(scanf("%d",&N)==1 && N)
{
getchar();

for(i=1;i<=N;i++)
{
gets(input);
card[i]=strtok(input," ");
pos[i]=strlen(strtok(NULL," "));
flag[i]=false;
}

current=0;

for(i=1;i<=N;i++)
{
j=current;k=0;

while(k<pos[i])
{
j++;
if(j>N) j%=N;
if(!flag[j]) k++;
else continue;
}

current=j;

placed[j]=card[i];
flag[j]=true;

}

for(i=1;i<=N;i++)
{
if(i==1) printf("%s",placed[i].c_str());
else printf(" %s",placed[i].c_str());
}

putchar('\n');
}
}
I think there may be some problems between the transformation of char* and string. You may need to double-check that.
Have you ever...
• Wanted to work at best companies?
• Struggled with interview problems that could be solved in 15 minutes?
• Wished you could study real-world problems?
If so, you need to read Elements of Programming Interviews.

uDebug
A great helper
Posts: 475
Joined: Tue Jul 24, 2012 4:23 pm

### Re: 10978 - Let's Play Magic!

Here's some input / output I found useful during testing / debugging.

Input

Code: Select all

13
AS ACE
2S TWO
3S THREE
4C FOUR
5C FIVE
6C SIX
7D SEVEN
8D EIGHT
9D NINE
TH TEN
JH JACK
QH QUEEN
KH KING
13
AS ACE
2S TWO
3S THREE
4C FOUR
5C FIVE
6C SIX
7D SEVEN
8D EIGHT
9D NINE
TH TEN
JH JACK
QH QUEEN
KH KING
52
AS ACE
2S TWO
3S THREE
4S FOUR
5S FIVE
6S SIX
7S SEVEN
8S EIGHT
9S NINE
TS TEN
JS JACK
QS QUEEN
KS KING
AC ACE
2C TWO
3C THREE
4C FOUR
5C FIVE
6C SIX
7C SEVEN
8C EIGHT
9C NINE
TC TEN
JC JACK
QC QUEEN
KC KING
AH ACE
2H TWO
3H THREE
4H FOUR
5H FIVE
6H SIX
7H SEVEN
8H EIGHT
9H NINE
TH TEN
JH JACK
QH QUEEN
KH KING
AD ACE
2D TWO
3D THREE
4D FOUR
5D FIVE
6D SIX
7D SEVEN
8D EIGHT
9D NINE
TD TEN
JD JACK
QD QUEEN
KD KING
0
AC Output:

Code: Select all

QH 4C AS 8D KH 2S 7D 5C TH JH 3S 6C 9D
QH 4C AS 8D KH 2S 7D 5C TH JH 3S 6C 9D
TH 8D AS AC 5H 2S JC 2C KD 2D 3S 6H JH 3C 4S QC 7D 5D 5S 4C TD 6S KC 7H 5C JD 7S AH 6C QH 3D 8S 2H 6D 7C 9S 8H 9D TS KH 3H 8C JS QD 9H 4D 9C QS 4H AD TC KS
Check input and AC output for over 7,500 problems on uDebug!

Find us on Facebook. Follow us on Twitter.

uDebug
A great helper
Posts: 475
Joined: Tue Jul 24, 2012 4:23 pm

### Re: 10978 - Let's Play Magic!

shihabrc wrote:Though its a quite easy simulation prob, I'm getting WA 4 this. Can any1 hlp ma(I/O,suggestions, anything).
Try "int main()" and "return 0;" instead of "void main().
Check input and AC output for over 7,500 problems on uDebug!

Find us on Facebook. Follow us on Twitter.