PTA 1018 hammer scissors cloth (20 points) C

Posted by totof06 on Sat, 08 Jan 2022 09:33:15 +0100

preface

Details determine success or failure.

stem

Everyone should be able to play the game of "hammer, scissors and cloth": two people give gestures at the same time, and the winning and losing rules are shown in the figure:

Now the record of the confrontation between the two is given. Please count the number of wins, draws and losses of both sides, and give what gestures the two sides make respectively, which has the greatest chance of winning.

Input format:

Enter the first line to give a positive integer N (≤ 105), that is, the number of times the two sides meet. Then N , lines, each line gives the information of a confrontation, that is, the gestures given by both parties at the same time. C represents "hammer", J represents "scissors", B represents "cloth", the first letter represents Party A, the second represents Party B, and there is a space in the middle.

Output format:

The output lines 1 and 2 give the winning, equalizing and negative times of a and B respectively, and the numbers are separated by a space. The third line gives two letters, respectively representing the gesture with the most winning times of a and B, with a space in the middle. If the solution is not unique, the solution with the smallest alphabetical order is output.

Input example:

10
C J
J B
C B
B B
B C
C C
C B
J B
B C
J J

No blank lines at the end

Output example:

5 3 2
2 3 5
B B

No blank lines at the end

Analysis

difficulty

  1. Processing of input multi line string (including spaces)
  2. When comparing the winning and losing, count the gestures with the most winning times. If there are multiple gestures, there is also a layer of comparison with the smallest alphabetical order
    1. I use the if judgment statement to find the most winning times and compare the least alphabetic order (small alphabetic order > =, large alphabetic order >), with few letters and corresponding to the 0, 1 and 2 subscripts of B and C arrays
  3. Two dimensional character array: a [number of strings] [string length]

answer

1.0 exhaustive

Two doubtful points

  1. why  if(i==n+1) break  ?
  2. why print  x2-1  ?

answer

Because gets() takes carriage return as the end flag of line input.

The carriage return after the first n input is regarded by gets() as the end flag of a[0], and a[0] is an empty string.

It is explained that only when i==n+1 jumps out of the loop (a[1] starts to store the confrontation record) and x2-1 (the 0 and 2 characters of the empty string are equal).

#include<stdio.h>
#include<stdlib.h>
#include<string.h> 
char a[100000][5];
int main(){
	int n,i=0,x1=0,x2=0,x3=0,y1=0,y2=0,y3=0,b[3]={0},c[3]={0};
	scanf("%d",&n);
	while(gets(a[i])){
		if(a[i][0]==a[i][2]){
			x2++;y2++;
		}
		if(a[i][0]=='C' && a[i][2]=='J'){
			x1++;y3++;b[0]++;
		}
		if(a[i][0]=='C' && a[i][2]=='B'){
			x3++;y1++;c[2]++;
		}
		if(a[i][0]=='J' && a[i][2]=='B'){
			x1++;y3++;b[1]++;
		}
		if(a[i][0]=='J' && a[i][2]=='C'){
			x3++;y1++;c[0]++;
		}
		if(a[i][0]=='B' && a[i][2]=='J'){
			x3++;y1++;c[1]++;
		}
		if(a[i][0]=='B' && a[i][2]=='C'){
			x1++;y3++;b[2]++;
		}
		i++;
		if(i==n+1) break;
	}
	printf("%d %d %d\n%d %d %d\n",x1,x2-1,x3,y1,y2-1,y3);
	int max1=b[0],max2=c[0];
	while(1){
		if(b[1]>max1){
			if(b[2]>=b[1]){
				printf("B ");break;
			}
			else{
				printf("J ");break;
			}
		}
		if(b[2]>=max1){
			if(b[1]>b[2]){
				printf("J ");break;
			}
			else{
				printf("B ");break;
			}
		}
	    else{
	    	printf("C ");break;
		}
	}
	while(1){
		if(c[1]>max2){
			if(c[2]>=c[1]){
				printf("B");break;
			}
			else{
				printf("J");break;
			}
		}
		if(c[2]>=max2){
			if(c[1]>c[2]){
				printf("J");break;
			}
			else{
				printf("B");break;
			}
		}
	    else{
	    	printf("C");break;
		}
	}
    return 0;
}

2.0 (properly optimize and solve doubts)

#include<stdio.h>
char a[100000][5];
int main(){
	int n,i=0,x1=0,x2=0,x3=0,b[3]={0},c[3]={0};    // x1:win x2:draw x3:lose  0:C 1:J 2:B
	scanf("%d\n",&n);
	while(gets(a[i]) && i<n){
		if(a[i][0]==a[i][2]) x2++;
		if(a[i][0]=='C' && a[i][2]=='J'){
			x1++;b[0]++;
		}
		if(a[i][0]=='C' && a[i][2]=='B'){
			x3++;c[2]++;
		}
		if(a[i][0]=='J' && a[i][2]=='B'){
			x1++;b[1]++;
		}
		if(a[i][0]=='J' && a[i][2]=='C'){
			x3++;c[0]++;
		}
		if(a[i][0]=='B' && a[i][2]=='J'){
			x3++;c[1]++;
		}
		if(a[i][0]=='B' && a[i][2]=='C'){
			x1++;b[2]++;
		}
		i++;
	}
	printf("%d %d %d\n%d %d %d\n",x1,x2,x3,x3,x2,x1);
	int max1=b[0];
	if(b[1]>max1){        // sort & judge letters' order at the same time
		if(b[2]>=b[1]) printf("B ");
		else printf("J ");
	}
	else if(b[2]>=max1){
		if(b[1]>b[2]) printf("J ");
		else printf("B ");
	}
    else printf("C ");
    int max2=c[0];        // the same
	if(c[1]>max2){
		if(c[2]>=c[1]) printf("B");
		else printf("J");
	}
	else if(c[2]>=max2){
		if(c[1]>c[2]) printf("J");
		else printf("B");
	}
    else printf("C");
    return 0;
}

expand

Reading of input multiline string (including spaces)

Method 1

#include<stdio.h>
#include<string.h> 
int main(){
	int i,n;
	char a[100][2];
	scanf("%d\n",&n);
	for(i=0;i<n;i++){
		if(i!=n-1) scanf("%c %c\n",&a[i][0],&a[i][1]);
		else scanf("%c %c",&a[i][0],&a[i][1]);
	}
	for(i=0;i<n;i++){
		printf("%c %c\n",a[i][0],a[i][1]);
	}
	return 0;
}

M2

#include<stdio.h>
#include<string.h> 
int main(){
	int i,n;
	char a[100][5];
	scanf("%d\n",&n);
	for(i=0;i<n;i++){
		gets(a[i]);
	}
	for(i=0;i<n;i++){
		puts(a[i]);
	}
	return 0;
}

M3

#include<stdio.h>
#include<string.h> 
int main(){
	int i,n;
	char a[100][5];
	scanf("%d\n",&n);
	while(gets(a[i])){
		i++;
		if(i==n) break;
	}
	for(i=0;i<n;i++){
		puts(a[i]);
	}
	return 0;
}

Topics: C pta