Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

Re:AC,G++AC,C++AC

Posted by kuaichenyang at 2015-11-22 01:22:56 on Problem 1786
In Reply To:AC,G++AC,C++AC Posted by:kuaichenyang at 2015-11-22 01:12:55
我改用qsort排序,再用字符数组缓存,G++ AC 74ms,4457B

4685B->4980->4457B,开心

Code:

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

const int _MAXN=150;

typedef struct tagCard{
	char color;
	char number;
}Card;

char start;
Card cards[53];
Card N[14],E[14],S[14],W[14];

int cmp(const void *a,const void *b){
	Card A=*((Card *)a);
	Card B=*((Card *)b);

	if(A.color==B.color){
		if(isalpha(A.number)&&isalpha(B.number)){
			int t1,t2;
			if(A.number=='T'){
				t1=1;
			}
			if(A.number=='J'){
				t1=2;
			}
			if(A.number=='Q'){
				t1=3;
			}
			if(A.number=='K'){
				t1=4;
			}
			if(A.number=='A'){
				t1=5;
			}
			if(B.number=='T'){
				t2=1;
			}
			if(B.number=='J'){
				t2=2;
			}
			if(B.number=='Q'){
				t2=3;
			}
			if(B.number=='K'){
				t2=4;
			}
			if(B.number=='A'){
				t2=5;
			}
			return t1-t2;
		}
		return A.number-B.number;
	}
	int t1,t2;
	if(A.color=='C'){
		t1=1;
	}
	if(A.color=='D'){
		t1=2;
	}
	if(A.color=='S'){
		t1=3;
	}
	if(A.color=='H'){
		t1=4;
	}
	if(B.color=='C'){
		t2=1;
	}
	if(B.color=='D'){
		t2=2;
	}
	if(B.color=='S'){
		t2=3;
	}
	if(B.color=='H'){
		t2=4;
	}
	return t1-t2;
}

int main(){
	char input[10];
	while(scanf("%s",input),input[0]!='#'){
		//init
		char str[_MAXN];
		char tmp[_MAXN];
		scanf("%s",str);
		scanf("%s",tmp);
		strcat(str,tmp);
		int len=strlen(str);
		int number=0;
		for(int i=0;i<len;i+=2){
			cards[++number].color=str[i];
			cards[number].number=str[i+1];
		}
		//work
		int count=0;
		if(input[0]=='N'){
			for(int i=1;i<=number;i+=4){
				E[count]=cards[i];
				S[count]=cards[i+1];
				W[count]=cards[i+2];
				N[count++]=cards[i+3];
			}
		}
		if(input[0]=='E'){
			for(int i=1;i<=number;i+=4){
				S[count]=cards[i];
				W[count]=cards[i+1];
				N[count]=cards[i+2];
				E[count++]=cards[i+3];
			}
		}
		if(input[0]=='S'){
			for(int i=1;i<=number;i+=4){

				W[count]=cards[i];
				N[count]=cards[i+1];
				E[count]=cards[i+2];
				S[count++]=cards[i+3];
			}
		}
		if(input[0]=='W'){
			for(int i=1;i<=number;i+=4){
				N[count]=cards[i];
				E[count]=cards[i+1];
				S[count]=cards[i+2];
				W[count++]=cards[i+3];

			}
		}

		//sort
		qsort(N,count,sizeof(Card),cmp);
		qsort(E,count,sizeof(Card),cmp);
		qsort(S,count,sizeof(Card),cmp);
		qsort(W,count,sizeof(Card),cmp);

		//print
		char ans[10][_MAXN];

		printf("South player:\n");
		ans[0][0]='+';
		int sum=1;
		for(int i=0;i<count;i++){
			ans[0][sum++]='-';
			ans[0][sum++]='-';
			ans[0][sum++]='-';
			ans[0][sum++]='+';
		}
		ans[0][sum]='\0';
		ans[1][0]='|';
		sum=1;
		for(int i=0;i<count;i++){
			ans[1][sum++]=S[i].number;
			ans[1][sum++]=' ';
			ans[1][sum++]=S[i].number;
			ans[1][sum++]='|';
		}
		ans[1][sum]='\0';

		ans[2][0]='|';
		sum=1;
		for(int i=0;i<count;i++){
			ans[2][sum++]=' ';
			ans[2][sum++]=S[i].color;
			ans[2][sum++]=' ';
			ans[2][sum++]='|';
		}
		ans[2][sum]='\0';
		strcpy(ans[3],ans[1]);
		ans[4][0]='+';
		sum=1;
		for(int i=0;i<count;i++){
			ans[4][sum++]='-';
			ans[4][sum++]='-';
			ans[4][sum++]='-';
			ans[4][sum++]='+';
		}
		ans[4][sum]='\0';
		for(int i=0;i<5;i++){
			puts(ans[i]);
		}

		printf("West player:\n");
		ans[1][0]='|';
		sum=1;
		for(int i=0;i<count;i++){
			ans[1][sum++]=W[i].number;
			ans[1][sum++]=' ';
			ans[1][sum++]=W[i].number;
			ans[1][sum++]='|';
		}
		ans[1][sum]='\0';
		ans[2][0]='|';
		sum=1;
		for(int i=0;i<count;i++){
			ans[2][sum++]=' ';
			ans[2][sum++]=W[i].color;
			ans[2][sum++]=' ';
			ans[2][sum++]='|';
		}
		ans[2][sum]='\0';
		strcpy(ans[3],ans[1]);
		for(int i=0;i<5;i++){
			puts(ans[i]);
		}

		printf("North player:\n");
		ans[1][0]='|';
		sum=1;
		for(int i=0;i<count;i++){
			ans[1][sum++]=N[i].number;
			ans[1][sum++]=' ';
			ans[1][sum++]=N[i].number;
			ans[1][sum++]='|';
		}
		ans[1][sum]='\0';
		ans[2][0]='|';
		sum=1;
		for(int i=0;i<count;i++){
			ans[2][sum++]=' ';
			ans[2][sum++]=N[i].color;
			ans[2][sum++]=' ';
			ans[2][sum++]='|';
		}
		ans[2][sum]='\0';
		strcpy(ans[3],ans[1]);
		for(int i=0;i<5;i++){
			puts(ans[i]);
		}

		printf("East player:\n");
		ans[1][0]='|';
		sum=1;
		for(int i=0;i<count;i++){
			ans[1][sum++]=E[i].number;
			ans[1][sum++]=' ';
			ans[1][sum++]=E[i].number;
			ans[1][sum++]='|';
		}
		ans[1][sum]='\0';
		ans[2][0]='|';
		sum=1;
		for(int i=0;i<count;i++){
			ans[2][sum++]=' ';
			ans[2][sum++]=E[i].color;
			ans[2][sum++]=' ';
			ans[2][sum++]='|';
		}
		ans[2][sum]='\0';
		strcpy(ans[3],ans[1]);
		ans[4][0]='+';
		sum=1;
		for(int i=0;i<5;i++){
			puts(ans[i]);
		}
		printf("\n");
	}
	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator