555 - Bridge Hands

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

Moderator: Board moderators

Post Reply
Ryck
New poster
Posts: 2
Joined: Tue Apr 20, 2004 10:26 am

555

Post by Ryck »

In my linux computer can compile it and no problem to solve the question
but when i submit to online judge .it say complie error??
what's wrong of my code

#include <iostream>
#include <cstdlib>
using namespace std;

enum type{CLUBS=0,DIAMONDS,SPADES,HEARTS};
enum no{two=0,three,four,five,six,seven,eight,nine,ten,jack,queen,king,Ace};
struct card{
no nu;
char b;
char a;
type cardtype;
};

void swap(card &a,card &b);
void print(card A[],char dir);
void sort(card A[13]);
void save(card A[13],card B[],int i);
void sortname(int a,int b,card A[]);

int main(){
card total[52];
card North[13];
card South[13];
card East[13];
card West[13];

char c;
char who_start=getchar();
int i=0;
while(1){
c=getchar();
if(c=='\n'){}
else if(c=='#')break;
else{
total.a=c;
total.b=getchar();
i++;
}
}


for(int i=0;i<52;i++){
switch(total.a){
case 'C':total.cardtype=CLUBS;
break;
case 'D':total.cardtype=DIAMONDS;
break;
case 'S':total.cardtype=SPADES;
break;
case 'H':total.cardtype=HEARTS;
break;
}//end switch
switch(total.b){
case 'A' :total.nu=Ace;
break;
case '2': total.nu=two;
break;
case '3':total[i].nu=three;
break;
case '4':total[i].nu=four;
break;
case '5':total[i].nu=five;
break;
case '6':total[i].nu=six;
break;
case '7':total[i].nu=seven;
break;
case '8':total[i].nu=eight;
break;
case '9':total[i].nu=nine;
break;
case 'T':total[i].nu=ten;
break;
case 'J':total[i].nu=jack;
break;
case 'Q':total[i].nu=queen;
break;
case 'K':total[i].nu=king;
break;
}//end swithc


}//end for


switch(who_start){
case 'W':save(North,total,0);
save(East,total,1);
save(South,total,2);
save(West,total,3);
break;
case 'E':save(South,total,0);
save(West,total,1);
save(North,total,2);
save(East,total,3);
break;
case 'N':save(East,total,0);
save(South,total,1);
save(West,total,2);
save(North,total,3);
break;
case 'S':save(West,total,0);
save(North,total,1);
save(East,total,2);
save(South,total,23);
break;
}//end switch
sort(South);
sort(North);
sort(West);
sort(East);


print(South,'S');
cout<<endl;
print(West,'W');
cout<<endl;
print(North,'N');
cout<<endl;
print(East,'E');
}

void sort(card A[13]){
int l=0,k=0,r=0;
for(int i=0;i<13;i++){
if(A[i].cardtype==CLUBS){
l++;
}
else if(A[i].cardtype==DIAMONDS){
k++;
}
else if(A[i].cardtype==SPADES){
r++;
}
}

k=k+l;
r+=k;

for(int i=0;i<12;i++){
for(int j=i+1;j<13;j++){
if(A[i].cardtype>A[j].cardtype){
swap(A[i],A[j]);
}
}
}
sortname(0,l,A);
sortname(l,k,A);
sortname(k,r,A);
sortname(r,13,A);

}

void print(card A[],char dir){
cout<<dir<<": ";
for(int i=0;i<13;i++){
cout<<A[i].a<<A[i].b<<" ";
}
}

void sortname(int a,int b,card A[]){
for(int i=a;i<b;i++){
for(int j=i;j<b;j++){
if(A[i].nu>A[j].nu){
swap(A[i],A[j]);
}
}
}
}
void swap(card &a, card &b){
card temp=a;
a=b;
b=temp;
}
void save(card A[13],card B[],int i){
for(int j=0;j<13;j++){
A[j]=B[i];
i=i+4;
}
}
Dejarik
New poster
Posts: 32
Joined: Sun Mar 07, 2004 1:23 pm
Location: Barcelona, SPAIN
Contact:

Post by Dejarik »

The compiler used by the Online Judge does not accept "//" as a coment. You have to use "/* ........ */".

In addition, you receive an email for each code you submit. If you receive Compilation Error, in the mail is explained the reason.

Hope it helps, Joan
Ryck
New poster
Posts: 2
Joined: Tue Apr 20, 2004 10:26 am

Post by Ryck »

thx , i find online-judge need include <stdio.h> to use getchar();

but i still get error ,
you program die in signal 11
that's mean what wrong?
aeiou
New poster
Posts: 21
Joined: Wed May 07, 2008 11:32 am

555 - Bridge Hands

Post by aeiou »

Hi everybody,
I thought this problem is easy but I get WA...
Here's my code , anyone pls help me!!

Code: Select all

#include <iostream>
#include <vector>
#include <string>
#include <cstdio>
#include <algorithm>
using namespace std;

#define pb push_back
#define fir_big 0
#define same 1
#define sec_big 2

vector <char> nc , ec , wc , sc , nn , en , wn , sn;

string parse_str ( string s )
{
 string x = "";
 for ( int i = 0 ; i < s.size() ; i ++ )
 {
  if ( s[i] != ' ' )
   x += s[i];
 } 
 return x;
}

int get_big ( char c , char d )
{
 int a = 4 , b = 3;
 if ( c == d ) 
  return same;
 else if ( c == 'C' && ( d == 'D' || d == 'S' || d == 'H' ) )
  return fir_big;
 else if ( c == 'D' && ( d == 'S' || d == 'H' ) )
  return fir_big;
 else if ( c == 'S' && d == 'H' )
  return fir_big;
 return sec_big;
}

void sort_pile ( vector <char> ty , vector <char> num , char s )
{
 vector <int> val (num.size());
 for ( int i = 0 ; i < num.size() ; i ++ )
 {
  if ( num[i] == 'T' ) val[i] = 10;
  else if ( num[i] == 'J' ) val[i] = 11;
  else if ( num[i] == 'Q' ) val[i] = 12;
  else if ( num[i] == 'A' ) val[i] = 13;
  else val[i] = num[i] - '0';
 }

 for ( int i = 0 ; i < ty.size() ; i ++ )
 {
  for ( int j = i + 1 ; j < ty.size() ; j ++ )
  {
   char c = ty[i] , d = ty[j];
   if ( get_big ( c , d ) == same )
   {
    if ( val[i] > val[j] )
    {
     swap ( val[i] , val[j] );
     swap ( num[i] , num[j] );
    }
   }
   else if ( get_big ( c , d ) == sec_big )
   {
    swap ( ty[i] , ty[j] );
    swap ( val[i] , val[j] );
    swap ( num[i] , num[j] );
   }
  } 
 }
 if ( s == 'N' )
 {
  nc = ty;
  nn = num;
 }
 else if ( s == 'S' )
 {
  sc = ty;
  sn = num;
 } 
 else if ( s == 'W' )
 {
  wc = ty;
  wn = num;
 } 
 else if ( s == 'E' )
 {
  ec = ty;
  en = num;
 }
}

void display ( vector <char> a , vector <char> b , char c )
{ 
 printf ( "%c: " , c );
 int i;
 for ( i = 0 ; i < a.size() - 1 ; i ++ )
  printf ( "%c%c " , a[i] , b[i] );
 printf ( "%c%c\n" , a[i] , b[i] ); 
}

void deal ( string pile , char start )
{

 if ( start == 'N' ) start = 2;
 else if ( start == 'E' ) start = 3;
 else if ( start == 'S' ) start = 4;
 else if ( start == 'W' ) start = 1;

 int nxt = start;
 for ( int i = 0 ; i < pile.size() ; i += 2 )
 {
  if ( nxt == 2 )
  {
   ec.pb (pile[i]);
   en.pb (pile[i + 1]);
   nxt = 3;
  }
  else if ( nxt == 3 )
  {
   sc.pb (pile[i]);
   sn.pb (pile[i + 1]);
   nxt = 4;
  }
  else if ( nxt == 4 )
  {
   wc.pb (pile[i]);
   wn.pb (pile[i + 1]);
   nxt = 1;
  }
  else if ( nxt == 1 )
  {
   nc.pb (pile[i]);
   nn.pb (pile[i + 1]);
   nxt = 2;
  }
 }

 /*display (sc , sn , 'S');
 display (wc , wn , 'W');
 display (nc , nn , 'N');
 display (ec , en , 'E'); 
 cout << endl << endl;*/

 sort_pile (nc , nn , 'N');
 sort_pile (ec , en , 'E');
 sort_pile (sc , sn , 'S');
 sort_pile (wc , wn , 'W'); 

 display (sc , sn , 'S');
 display (wc , wn , 'W');
 display (nc , nn , 'N');
 display (ec , en , 'E'); 
}

void clear_all ()
{
 nc.clear();nn.clear();
 sc.clear();sn.clear();
 ec.clear();en.clear();
 wc.clear();wn.clear(); 
}

int main ()
{
 char start;
 while ( scanf ( "%c" , &start ) == 1 && start != '#' )
 {
  getchar();
  string a , b;
  string pile = "";
  getline ( cin , a );
  getline ( cin , b );
  //cout << "A = " << a << endl << "B = " << b << endl << endl;  
  pile += parse_str (a);
  pile += parse_str (b);

  deal ( pile , start );
  clear_all();
 }
 return 0;
}


Waiting 4 help!!!
Thankx in advance..
aeiou
New poster
Posts: 21
Joined: Wed May 07, 2008 11:32 am

555 - Bridge Hands

Post by aeiou »

Still no reply!! :cry: :cry:
anyone pls help me...
brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 555 - Bridge Hands

Post by brianfry713 »

aeiou you're not checking for a King.
Check input and AC output for thousands of problems on uDebug!
MauriWilde
New poster
Posts: 14
Joined: Sun Jan 20, 2013 1:58 am

555 - Bridge Hands Wrong Answer

Post by MauriWilde »

I got Wrong Answer in this problem, and I can't find what goes wrong with my code. Could somebody please give me some input/output cases or tell me the mistake in my code?

This is my C++ code:

Code: Select all

Accepted
Thank you very much ^-^
Last edited by MauriWilde on Fri Jun 21, 2013 6:11 am, edited 1 time in total.
brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 555 - Bridge Hands Wrong Answer

Post by brianfry713 »

Code: Select all

        else if (b[1] == 'Q')
            y += 12;
        else if (b[1] == 'Q')
            y += 13;
Check input and AC output for thousands of problems on uDebug!
MauriWilde
New poster
Posts: 14
Joined: Sun Jan 20, 2013 1:58 am

Re: 555 - Bridge Hands Wrong Answer

Post by MauriWilde »

o.O

How I didn't see that!!??

Accepted :D

Thank you!! ^-^
longted
New poster
Posts: 6
Joined: Wed Aug 20, 2014 10:39 am

uva 555

Post by longted »

I know my code output is wrong.I also know qsort paramaters are wrong.but i dont't know how to modify it. who can tell me how to modify qsort paramaters. ths

Code: Select all

//
#include<iostream>
#include<string.h>
#define MAX_SIZE 512
using namespace std;
typedef struct
{
	char suit;
	char point;
}card;
int pointtovalues(char point)
{
	if(point>='0' && point <='9')
		return point-'0';
	else if(point=='T')
		return 10;
	else if(point=='J')
		return 11;
	else if(point=='Q')
		return 12;
	else if(point=='K')
		return 13;
	else if(point=='A')
		return 14;

	return -1;


}
int suittovalues(char psuit)
{
	if(psuit=='C')
		return 0;
	else if(psuit=='D')
		return 1;
	else if(psuit=='S')
		return 2;
	else
		return 3;

}
int compare (const void * a, const void * b)
{
	card *pa=(card *)a;
	card *pb=(card *)b;
	if(pa->suit==pb->suit)//compare point
	{
		int temp_a=pointtovalues(pa->point);
		int temp_b=pointtovalues(pb->point);
		return (temp_a>temp_b?1:-1);
	
	}
	else
	{
		int temp_a=suittovalues(pa->suit);
		int temp_b=suittovalues(pb->suit);
		return (temp_a>temp_b?1:-1);
	
	
	}
}
int main()
{

	char data[MAX_SIZE]={NULL},direction,temp_direction;
	card my_card[52];
	card p[4][13];

	int current_pos=-1;
	int my_card_count=0;
	while(cin>>temp_direction)
	{
	int count_times=2;
		if(temp_direction=='#')
			break;
		direction=temp_direction;
		while(count_times)
		{
			cin>>data;
			for(int i=0;i<strlen(data);)
			{
				my_card[my_card_count].suit=data[i];
				i++;
				my_card[my_card_count].point=data[i];
				i++;
				my_card_count++;
		
			}
			count_times--;
		}
	}
	if(direction=='N')
		current_pos=1;
	else if(direction=='E')
		current_pos=2;
	else if(direction=='S')
		current_pos=3;
	else
		current_pos=0;
	int distrubed_card_count=0,start_osition=current_pos;
	int  j=0;
	for(int i=0;i<52;i++)
	{
		current_pos=(current_pos+1)%4;
		p[current_pos][j]=my_card[distrubed_card_count];
	
		distrubed_card_count++;
		
		if(start_osition==current_pos)
			j++;
		

/*		for(int j=0;j<13;j++)
		{
			p[current_pos][j]=my_card[distrubed_card_count];
			distrubed_card_count++;
		}*/
	}
	for(int i=0;i<4;i++)
		qsort(p[i],4,sizeof(p[0][0]),compare);
	cout<<"S: ";
		for(int i=0;i<13;i++)
			cout<<p[3][i].suit<<p[3][i].point<<" ";
		cout<<endl;
	cout<<"W: ";
		for(int i=0;i<13;i++)
			cout<<p[0][i].suit<<p[0][i].point<<" ";
		cout<<endl;
	cout<<"N: ";
		for(int i=0;i<13;i++)
			cout<<p[1][i].suit<<p[1][i].point<<" ";
		cout<<endl;
	cout<<"E: ";
		for(int i=0;i<13;i++)
			cout<<p[2][i].suit<<p[2][i].point<<" ";
		cout<<endl;
	return 0;
}

lighted
Guru
Posts: 587
Joined: Wed Jun 11, 2014 9:56 pm
Location: Kyrgyzstan, Bishkek

Re: uva 555

Post by lighted »

Your program is ok. :)
Just change line

Code: Select all

qsort(p[i],13,sizeof(p[0][0]),compare);
Don't print extra spaces at the end of line.

Don't forget to remove your code after getting accepted. 8)

Post in existing threads for this problem. Use search by problem number.
http://acm.uva.es/board/search.php?keyw ... 056aa54933
Or post in threads for language C++. Don't open new thread.
A person who sees the good in things has good thoughts. And he who has good thoughts receives pleasure from life... Bediuzzaman
tidusleonart
New poster
Posts: 10
Joined: Fri Nov 21, 2014 9:18 am

Re: 555 - Bridge Hands

Post by tidusleonart »

Code: Select all

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

char values[13][2] = {"2","3","4","5","6","7","8","9","T","J","Q","K","A"};
char suits[4][2] = {"C","D","S","H"};

typedef vector<int> vi;

int valConv(char s)
{
	switch(s)
	{
	case '2': return 1;
	case '3': return 2;
	case '4': return 3;
	case '5': return 4;
	case '6': return 5;
	case '7': return 6;
	case '8': return 7;
	case '9': return 8;
	case 'T': return 9;
	case 'J': return 10;
	case 'Q': return 11;
	case 'K': return 12;
	case 'A': return 13;
	}
}
void getDeck(vi& deck, char hand[])
{
	for(int i = 0; hand[i] != '\0'; i+=2)
	{
		int val;
		if(hand[i] == 'C')
			val = 0;
		else if(hand[i] == 'D')
			val = 13;
		else if(hand[i] == 'S')
			val = 26;
		else if(hand[i] == 'H')
			val = 39;
		val += valConv(hand[i+1]);
		deck.push_back(val);
	}
}
void printHand(int round[4][13], int loc)
{
	for(int j = 0; j < 13; j++)
	{
		printf("%s%s ", suits[(round[loc][j]-1)/13], values[(round[loc][j]-1)%13] );
	}
	printf("\n");
}
int main()
{
	freopen("input.txt","r",stdin);
	//char pos[3];
	char line1[150],line2[150];
	vi deck;
	int round[4][13], loc;
	//while(scanf("%s", pos) && pos[0] != '#')
	while(true)
	{
		char pos;
		scanf(" %c", &pos);
		//cin >> pos;
		if(pos == '#')
			break;
		scanf("%s", line1);
		scanf("%s", line2);
		strcat(line1,line2);
		//printf("%s\n", line1);
		getDeck(deck, line1);
		if(pos == 'N')
			loc = 0;
		else if(pos == 'E')
			loc = 1;
		else if(pos == 'S')
			loc = 2;
		else if(pos == 'W')
			loc = 3;
		//cout << loc << endl;
		int j = 0;
		loc++;
		for(int i = 0; i < deck.size(); i++)
		{
			if ((i > 0) && (i % 4 == 0))
				j++;
			round[(loc++)%4][j] = deck[i];
		}
		for(int i = 0; i < 4; i++)
		{
			sort(round[i], round[i]+13);
		}
		printf("S: ");
		printHand(round,2);
		printf("W: ");
		printHand(round,3);
		printf("N: ");
		printHand(round,0);
		printf("E: ");
		printHand(round,1);
	}
	return 0;
}
Please help me with my code.....I don't know what i did wrong but I keep getting Runtime Error even though i tested on my computer and everything is working fine. Thanks! Please help me
tidusleonart
New poster
Posts: 10
Joined: Fri Nov 21, 2014 9:18 am

Re: 555 - Bridge Hands

Post by tidusleonart »

I got AC Thanks all .
Post Reply

Return to “Volume 5 (500-599)”