Question 4 C of PTA 2022

Posted by Codewarrior123 on Mon, 03 Jan 2022 05:23:22 +0100

preface

Start 2022 and participate in PTA new year challenge:

I'm really a konjac. Really, I just know I'm a konjac. I didn't expect that I'm so weak... So weak that I want to scold myself:

You confused vegetable dog who can't even solve the problem! Don't settle down, Deep Learning!!!

The lessons learned are summarized as follows:

  1. Limited time, "urgent" is the taboo.
  2. Careful examination and clear thinking.
  3. Don't rush to knock the code, first correctly translate the key information in the question, and reduce a lot of deletion and modification.

To do anything, first cultivate your mind and keep your mind calm.

I have a lot of room for progress. I want to make progress a little every day!

2022, solid and thick.

stem

According to the code, simple recall (may be different):

Problem description

Xiaohua buys three fixed number lottery tickets every week. The lottery number is a four digit positive integer. The winning number will be published every week. The winning situation is as follows (compare the purchase number with the winning number):

First prize - everyone is the same

Second prize - one, ten, one hundred are the same, and one thousand are different

Third prize - one, ten are the same, and hundreds are different

Fourth prize - the same place, ten different places

Fifth prize - different places

Please output the highest award that Xiaohua can win after n (0 < n < = 100) weeks (the first prize is the highest).

Input requirements

First line n, n weeks

Next, enter the weekly winning number on the n lines

In the next 3 lines, enter three sets of fixed numbers of florets

Output requirements

After n weeks of output, Xiaohua can win the highest award

First prize - YiDengJiang!!!

Second prize - ErDengJiang

Third prize - SanDengJiang

Fourth prize - SiDengJiang

Fifth prize - WuDengJiang
   

input data

4

1234

2345

3456

4567

0987

0986

0985

output data

SiDengJiang

analysis

important factor

  1. For string input, remember to getchar() to solve carriage return
  2. The judgment idea is from the fifth prize to the first prize

answer

1.0 LOW code of AC at that time

#include<stdio.h>
int main(){
	int n,i,j,get=5,min=5,temp;
	char a[5],b[5],c[5];
	char awa[101][5];
	scanf("%d",&n);
	getchar();
	for(i=0;i<n;i++){
		scanf("%c%c%c%c",&awa[i][0],&awa[i][1],&awa[i][2],&awa[i][3]);
		getchar();
	}
	scanf("%c%c%c%c",&a[0],&a[1],&a[2],&a[3]);getchar();
	scanf("%c%c%c%c",&b[0],&b[1],&b[2],&b[3]);getchar();
	scanf("%c%c%c%c",&c[0],&c[1],&c[2],&c[3]);
//	for(i=0;i<n;i++){
//		printf("%c%c%c%c\n",awa[i][0],awa[i][1],awa[i][2],awa[i][3]);
//	}
	for(i=0;i<n;i++){
		while(1){
			if(a[3]!=awa[i][3]){
				get=5;
				break;
			}
			else{
				if(a[2]!=awa[i][2]){
					get=4;break;
				}
				else{
					if(a[1]!=awa[i][1]){
						get=3;break;
					}
					else{
						if(a[0]!=awa[i][0]){
							get=2;break;
						}
						else{
							get=1;break;
						}
					}
				}
			}
		}
		if(get<min){
			temp=get;
			get=min;
			min=temp;
		}
	}
		for(i=0;i<n;i++){
		while(1){
			if(b[3]!=awa[i][3]){
				get=5;
				break;
			}
			else{
				if(b[2]!=awa[i][2]){
					get=4;break;
				}
				else{
					if(b[1]!=awa[i][1]){
						get=3;break;
					}
					else{
						if(b[0]!=awa[i][0]){
							get=2;break;
						}
						else{
							get=1;break;
						}
					}
				}
			}
		}
		if(get<min){
			temp=get;
			get=min;
			min=temp;
		}
	}
		for(i=0;i<n;i++){
		while(1){
			if(c[3]!=awa[i][3]){
				get=5;
				break;
			}
			else{
				if(c[2]!=awa[i][2]){
					get=4;break;
				}
				else{
					if(c[1]!=awa[i][1]){
						get=3;break;
					}
					else{
						if(c[0]!=awa[i][0]){
							get=2;break;
						}
						else{
							get=1;break;
						}
					}
				}
			}
		}
		if(get<min){
			temp=get;
			get=min;
			min=temp;
		}
	}
	if(min==1) printf("YiDengJiang!!!");
	if(min==2) printf("ErDengJiang");
	if(min==3) printf("SanDengJiang");
	if(min==4) printf("SiDengJiang");
	if(min==5) printf("WuDengJiang");
	return 0;
}

2.0 function call one-dimensional array (without OJ)

#include<stdio.h>
char a[4][5];
char awa[101][5];
int n,i,j,get=5,min=5,temp;
void search(char a[], char awa[]);
int main(){
	scanf("%d",&n);getchar();
	for(i=0;i<n;i++){
		scanf("%c%c%c%c",&awa[i][0],&awa[i][1],&awa[i][2],&awa[i][3]);getchar();
	}
	for(i=0;i<3;i++){
		scanf("%c%c%c%c",&a[i][0],&a[i][1],&a[i][2],&a[i][3]);getchar();
	}
	for(i=0;i<3;i++){
		for(j=0;j<n;j++){
			search(a[i],awa[j]);
		}
	}
	switch(min){
		case 1: printf("YiDengJiang!!!");break;
		case 2: printf("ErDengJiang");break;
		case 3: printf("SanDengJiang");break;
		case 4: printf("SiDengJiang");break;
		case 5: printf("WuDengJiang");break;
	}
	return 0;
}
void search(char a[], char awa[]){
	while(1){
		if(a[3]!=awa[3]){
			get=5;break;
		}
		else{
			if(a[2]!=awa[2]){
				get=4;break;
			}
			else{
				if(a[1]!=awa[1]){
					get=3;break;
				}
				else{
					if(a[0]!=awa[0]){
						get=2;break;
					}
					else{
						get=1;break;
					}
				}
			}
		}
	}
	if(get<min){
		temp=get;
		get=min;
		min=temp;
	}
}

3.0 function call 2D array (without OJ)

Function fragment

search(a,awa,3,n);        // in main function
void search(char a[][5], char awa[][5], int r1, int r2){
	for(i=0;i<r1;i++){
		for(j=0;j<r2;j++){
			while(1){
				if(a[i][3]!=awa[j][3]){
					get=5;break;
				}
				else{
					if(a[i][2]!=awa[j][2]){
						get=4;break;
					}
					else{
						if(a[i][1]!=awa[j][1]){
							get=3;break;
						}
						else{
							if(a[i][0]!=awa[j][0]){
								get=2;break;
							}
							else{
								get=1;break;
							}
						}
					}
				}
			}
			if(get<min){
				temp=get;
				get=min;
				min=temp;
			}
		}
	}
}

expand

In fact, I just want to practice calling arrays through functions. At present, the pointer is not very familiar with and the content is not well mastered.

But I will refuel!

I can't understand the function call array on the Internet. I suggest reading the classic C Primer Plus (Sixth Edition). It's very clear in the book. I realized it at once—— 10.7.3 functions and multidimensional arrays

Topics: C pta