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

AC,G++AC,C++AC

Posted by kuaichenyang at 2015-11-22 01:12:55 on Problem 1786
G++ TLE C++ AC,是因为你的代码中对IO接口调用太多导致的,用一个字符数组缓存下,然后一起输出就可以了,我的G++ 94ms,C++不用字符数组缓存 204ms
Code(G++,字符数组缓存):

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

const int _MAXN=150;

typedef struct tagCard{
	char color;
	char number;
	bool operator>(const tagCard other){
		if(this->color==other.color){
			if(isalpha(this->number)&&isalpha(other.number)){
				int t1,t2;
				if(this->number=='T'){
					t1=1;
				}
				if(this->number=='J'){
					t1=2;
				}
				if(this->number=='Q'){
					t1=3;
				}
				if(this->number=='K'){
					t1=4;
				}
				if(this->number=='A'){
					t1=5;
				}
				if(other.number=='T'){
					t2=1;
				}
				if(other.number=='J'){
					t2=2;
				}
				if(other.number=='Q'){
					t2=3;
				}
				if(other.number=='K'){
					t2=4;
				}
				if(other.number=='A'){
					t2=5;
				}
				return t1>t2;
			}
			return this->number>other.number;
		}
		int t1,t2;
		if(this->color=='C'){
			t1=1;
		}
		if(this->color=='D'){
			t1=2;
		}
		if(this->color=='S'){
			t1=3;
		}
		if(this->color=='H'){
			t1=4;
		}
		if(other.color=='C'){
			t2=1;
		}
		if(other.color=='D'){
			t2=2;
		}
		if(other.color=='S'){
			t2=3;
		}
		if(other.color=='H'){
			t2=4;
		}
		return t1>t2;
	}
}Card;

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

void swapCard(Card &a,Card &b){
	Card tmp=a;
	a=b;
	b=tmp;
}

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
		for(int i=0;i<count-1;i++){
			for(int j=0;j<count-1-i;j++){
				if(E[j]>E[j+1]){
					swapCard(E[j],E[j+1]);
				}
			}
		}
		for(int i=0;i<count-1;i++){
			for(int j=0;j<count-1-i;j++){
				if(S[j]>S[j+1]){
					swapCard(S[j],S[j+1]);
				}
			}
		}
		for(int i=0;i<count-1;i++){
			for(int j=0;j<count-1-i;j++){
				if(W[j]>W[j+1]){
					swapCard(W[j],W[j+1]);
				}
			}
		}
		for(int i=0;i<count-1;i++){
			for(int j=0;j<count-1-i;j++){
				if(N[j]>N[j+1]){
					swapCard(N[j],N[j+1]);
				}
			}
		}
		//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;
}

C++,直接调用printf

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

const int _MAXN=150;

typedef struct tagCard{
	char color;
	char number;
	bool operator>(const tagCard other){
		if(this->color==other.color){
			if(isalpha(this->number)&&isalpha(other.number)){
				int t1,t2;
				if(this->number=='T'){
					t1=1;
				}
				if(this->number=='J'){
					t1=2;
				}
				if(this->number=='Q'){
					t1=3;
				}
				if(this->number=='K'){
					t1=4;
				}
				if(this->number=='A'){
					t1=5;
				}
				if(other.number=='T'){
					t2=1;
				}
				if(other.number=='J'){
					t2=2;
				}
				if(other.number=='Q'){
					t2=3;
				}
				if(other.number=='K'){
					t2=4;
				}
				if(other.number=='A'){
					t2=5;
				}
				return t1>t2;
			}
			return this->number>other.number;
		}
		int t1,t2;
		if(this->color=='C'){
			t1=1;
		}
		if(this->color=='D'){
			t1=2;
		}
		if(this->color=='S'){
			t1=3;
		}
		if(this->color=='H'){
			t1=4;
		}
		if(other.color=='C'){
			t2=1;
		}
		if(other.color=='D'){
			t2=2;
		}
		if(other.color=='S'){
			t2=3;
		}
		if(other.color=='H'){
			t2=4;
		}
		return t1>t2;
	}
}Card;

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

void swapCard(Card &a,Card &b){
	Card tmp=a;
	a=b;
	b=tmp;
}

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
		for(int i=0;i<count-1;i++){
			for(int j=0;j<count-1-i;j++){
				if(E[j]>E[j+1]){
					swapCard(E[j],E[j+1]);
				}
			}
		}
		for(int i=0;i<count-1;i++){
			for(int j=0;j<count-1-i;j++){
				if(S[j]>S[j+1]){
					swapCard(S[j],S[j+1]);
				}
			}
		}
		for(int i=0;i<count-1;i++){
			for(int j=0;j<count-1-i;j++){
				if(W[j]>W[j+1]){
					swapCard(W[j],W[j+1]);
				}
			}
		}
		for(int i=0;i<count-1;i++){
			for(int j=0;j<count-1-i;j++){
				if(N[j]>N[j+1]){
					swapCard(N[j],N[j+1]);
				}
			}
		}
		//print
		printf("South player:\n");
		printf("+---+---+---+---+---+---+---+---+---+---+---+---+---+\n");
		printf("|");
		for(int i=0;i<count;i++){
			printf("%c %c|",S[i].number,S[i].number);
		}
		printf("\n");
		printf("|");
		for(int i=0;i<count;i++){
			printf(" %c |",S[i].color);
		}
		printf("\n");
		printf("|");
		for(int i=0;i<count;i++){
			printf("%c %c|",S[i].number,S[i].number);
		}
		printf("\n");
		printf("+---+---+---+---+---+---+---+---+---+---+---+---+---+\n");

		printf("West player:\n");
		printf("+---+---+---+---+---+---+---+---+---+---+---+---+---+\n");
		printf("|");
		for(int i=0;i<count;i++){
			printf("%c %c|",W[i].number,W[i].number);
		}
		printf("\n");
		printf("|");
		for(int i=0;i<count;i++){
			printf(" %c |",W[i].color);
		}
		printf("\n");
		printf("|");
		for(int i=0;i<count;i++){
			printf("%c %c|",W[i].number,W[i].number);
		}
		printf("\n");
		printf("+---+---+---+---+---+---+---+---+---+---+---+---+---+\n");

		printf("North player:\n");
		printf("+---+---+---+---+---+---+---+---+---+---+---+---+---+\n");
		printf("|");
		for(int i=0;i<count;i++){
			printf("%c %c|",N[i].number,N[i].number);
		}
		printf("\n");
		printf("|");
		for(int i=0;i<count;i++){
			printf(" %c |",N[i].color);
		}
		printf("\n");
		printf("|");
		for(int i=0;i<count;i++){
			printf("%c %c|",N[i].number,N[i].number);
		}
		printf("\n");
		printf("+---+---+---+---+---+---+---+---+---+---+---+---+---+\n");

		printf("East player:\n");
		printf("+---+---+---+---+---+---+---+---+---+---+---+---+---+\n");
		printf("|");
		for(int i=0;i<count;i++){
			printf("%c %c|",E[i].number,E[i].number);
		}
		printf("\n");
		printf("|");
		for(int i=0;i<count;i++){
			printf(" %c |",E[i].color);
		}
		printf("\n");
		printf("|");
		for(int i=0;i<count;i++){
			printf("%c %c|",E[i].number,E[i].number);
		}
		printf("\n");
		printf("+---+---+---+---+---+---+---+---+---+---+---+---+---+\n");
		printf("\n");
	}
	return 0;
}


其实不用写冒泡,直接调用qsort排序也是可以的,感觉我写多了,我改一下qsort能使代码变得更短些

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