Experiment 3 citation and structure

A. Three number theory size (Reference)

Title Description

Enter three integers, and then output the values in descending order.

Requirements: define a function with no return value. The function parameters are references to three integer parameters, such as int & A, int & B and int & C. The three parameters are sorted by referencing methods within the function. The main function calls this function to sort.

Requirement: three integers cannot be sorted directly. They must be through functions and reference methods.

input

Input t in the first line indicates that there are t test instances

From the second line, enter three integers per line

Enter t line

output

Each row outputs each instance in descending order, with three integers separated by a single space

sample input

3
2 4 6
88 99 77
111 333 222

sample output

6 4 2
99 88 77
333 222 111

Reference code

#include<iostream>
using namespace std;
void Sort(int& a,int& b,int& c);//Passing parameters by reference saves space 
void Swap(int& a,int& b); 
int main()
{
	int t;//Number of test groups
	cin>>t;
	int a,b,c;
	while(t--)
	{
		cin>>a>>b>>c;
		Sort(a,b,c);
		cout<<a<<" "<<b<<" "<<c<<endl;
	} 
} 
void Swap(int& a,int& b)//exchange 
{
		int temp=a;
		a=b;
		b=temp;
}
void Sort(int& a,int& b,int& c)
{
	if(a<b) Swap(a,b);
	if(a<c) Swap(a,c);
	if(b<c) Swap(b,c);
	 
}

B. Who is the second (structure)

Title Description

Define a structure, including year, month and day, to represent the date of birth of a student. Then find out whose birth date ranks second among the birth dates of a group of students

Requirement: the storage of birth date must use structure, and other types of data structures cannot be used.

It is required that the structure must be used for the input, access and output of birth date in the whole process of the program.

input

Entering t in the first line indicates that there are t birth dates

Enter three integers for each line, representing year, month and day respectively

Enter t instances in sequence

output

Output the second oldest birth date, in the format of year month day

sample input

6
1980 5 6
1981 8 3
1980 3 19
1980 5 3
1983 9 12
1981 11 23

sample output

1980-5-3

Reference code

#include<iostream>
#include<algorithm>
using namespace std;
struct Date
{
	int y;
	int m;
	int d;
};
bool cmp(Date a,Date b)
{
	if(a.y!=b.y) return a.y<b.y;
	if(a.m!=b.m) return a.m<b.m;
	if(a.d!=b.d) return a.d<b.d;
}
int main()
{
	int t;
	cin>>t;
	Date *p=new Date[t];
	for(int i=0;i<t;i++)
	  cin>>p[i].y>>p[i].m>>p[i].d;
	sort(p,p+t,cmp);//sort 
	cout<<p[1].y <<'-'<<p[1].m<<'-'<<p[1].d <<endl; 
	delete []p;
}

C. Plagiarism search (structure + pointer + function)

Title Description

Given the test papers of a group of students, it is required to compare the contents of the test papers to find out whether there is plagiarism.

Each test paper includes: student number (integer type), answer to question 1 (string type), answer to question 2 (string type), answer to question 3 (string type)

Requirement: use the structure to store the information of the test paper. Define a function, the return value is an integer, and the parameter is two structure pointers. The function operation is to compare the answers of each question in two test papers. If the similarity of the answers of the same question number exceeds 90%, it is considered that there is plagiarism, and the function returns the plagiarized question number, otherwise it returns 0. Similarity refers to the comparison of two characters in one position of two answers in the same question. If the same number is greater than or equal to 90% of the length of any answer, it is considered as plagiarism.

input

Input t in the first line indicates that there are t test papers

In the second line, enter the student number of the first test paper

On the third line, enter the answer to question 1 of the first test paper

On the fourth line, enter the answer to question 2 of the first test paper

On the fifth line, enter the answer to question 3 of the first test paper

Each test paper corresponds to 4 lines of input

Input the data of t test papers in turn

output

In one line, separate the two student numbers and topic numbers found to be plagiarized with a single space between the data
If it is found that title 1 is plagiarized, the title number is 1, and so on
The output sequence is output according to the input student number sequence

sample input

5
2088150555
aabcdef11
ZZ887766dd
cc33447799ZZ
2088150333
abcdef00
AABBCCDDEE
ZZ668899cc
2088150111
AABBCCDDEE
ZZ668899cc
abcdef00
2088150222
AABBCFDDeE
ZZ889966dd
abcdef000
2088150444
aabcdef00
AABBCDDDEE
cc668899ZZ

sample output

2088150333 2088150444 2
2088150111 2088150222 3

Reference code

#include<iostream>
using namespace std;
struct Paper
{
	int num;
	char a[20];
	char b[20];
	char c[20];	
}paper[15];

int cmp(char *pa,char *pb)//Compare the similarity of two strings 
{
	int count=0;//Same number of characters 
	float flag=0;//The length of the shorter array 
	while(*pa!='\0'&&*pb!='\0')
	{
		if(*pa==*pb) count++;
		flag++;
		pa++;
		pb++;
	} 
	flag*=0.9;
	if(count>=flag) return 1;
	else return 0;
}
int search(Paper *paper1,Paper *paper2)//Structure pointer 
{
	if(cmp(paper1->a,paper2->a)==1) return 1;
	if(cmp(paper1->b,paper2->b)==1) return 2;
	if(cmp(paper1->c,paper2->c)==1) return 3;
	return 0;
}

int main()
{
	int t;//Number of test papers
	cin>>t;
	int i,j;
	for(i=0;i<t;i++)
	{
		cin>>paper[i].num >>paper[i].a>>paper[i].b>>paper[i].c;
	}
	
	for(i=0;i<t;i++)//Each round 
	{
		for(j=i+1;j<t;j++)//Don't use what you have searched before to avoid repetition 
		{
			int res=search(paper+i,paper+j);
			if(res!=0) cout<<paper[i].num <<" "<<paper[j].num<<" "<<res<<endl; 
		}
	}
	 return 0; 
	 
}

D. Find maximum minimum (Reference)

Title Description

Write the function void find (int * num, int n, int & minindex, int & maxindex) to find the subscripts minindex and maxindex of the elements with the minimum and maximum values in the array num (the elements are num[0], num[1],..., num[n-1]) (if there is the same maximum value, take the subscript that appears first.)

Enter n, dynamically allocate n integer spaces, enter n integers, and call this function to find the minimum and maximum subscripts of the array. Finally, output the results in the sample format.

Change the function find function without scoring.

input

Number of tests

One row of test data for each group: the number of data n, followed by N integers

output

Each group of test data outputs two lines, which are the minimum value, the maximum value and its subscript. See the sample for the specific format. Multiple sets of test data are separated by blank lines.

sample input

2
5 10 20 40 -100 0
10 23 12 -32 4 6 230 100 90 -120 15

sample output

min=-100 minindex=3
max=40 maxindex=2

min=-120 minindex=8
max=230 maxindex=5

Reference code

#include<iostream>
using namespace std;
void find(int *num,int n,int &minIndex,int &maxIndex)
{
	minIndex=0;
	maxIndex=0;
	int i=0;
	for(i=0;i<n;i++)
	{
		if(*(num+i)<*(num+minIndex)) minIndex=i;
		if(*(num+i)>*(num+maxIndex)) maxIndex=i;
	}
}
int main()
{
	int t;//Number of test groups
	cin>>t;
	while(t--)
	{
		int n;
		cin>>n;
		int max,min;
		int *p=new int[n];
		for(int i=0;i<n;i++) cin>>p[i];
		find(p,n,min,max);
		cout<<"min="<<p[min]<<" "<<"minindex="<<min<<endl;
		cout<<"max="<<p[max]<<" "<<"maxindex="<<max<<endl;
		cout<<endl;
	} 
	
	
}

E. Poker sorting (structure)

Title Description

A user-defined structure represents a playing card, including types - spades, hearts, clubs, squares and kings; Size - 2,3,4,5,6,7,8,9,10,J,Q,K,A, Xiao Wang (represented by 0) and Da Wang (represented by 1). Input n, input n playing card information, and output their sorting results from large to small.

Assuming that the sorting rule of playing cards is that the king and Xiao Wang are the first and second largest, the remaining 52 playing cards are sorted according to the color first and then the size.

Decor: spades > hearts > clubs > diamonds.

Size: a > k > Q > J > > 10 > 9 >... > 2.

Tip: use Baidu sort function and strstr function.

input

Test times t

Each set of test data has two lines:

The first line: n, indicating the input of n playing cards

The second line: information of n playing cards. See the example for the format

output

For each group of test data, the sorting results from large to small are output

sample input

3
5
 Spades 4 hearts 10 clubs Q block K spade A
10
 King plum blossom 10 hearts K Diamonds 9 spades 2 clubs A block Q Wang 8 spades J
5
 heart K Plum blossom K spade K block K Xiao Wang

sample output

spade A Spades 4 hearts 10 clubs Q block K
 King and Wang spades J Spades 8 spades 2 hearts K Plum blossom A Plum blossom 10 diamonds Q Block 9
 Wang spade K heart K Plum blossom K block K

Reference code

#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
struct puke
{
	char a[20];//poker 
	int flower;//Represents the size in the decor
	int  num;// Represents the normal size 
};
//All playing cards are converted with numbers for comparison
void tongyi(puke *pu)
{
	//flower is used to express the size relationship between design and color and size king, which is convenient for comparison 
	if(strstr(pu->a,"king")) pu->flower =6;//Maximum
	if(strstr(pu->a,"Xiao Wang")) pu->flower =5;
	if(strstr(pu->a,"spade")) pu->flower =4;
	if(strstr(pu->a,"heart")) pu->flower =3;
	if(strstr(pu->a,"Plum blossom")) pu->flower =2;
	if(strstr(pu->a,"block")) pu->flower =1;
	//The common size is expressed in num, which is convenient to compare the size relationship of the same design and color
	if(strstr(pu->a,"A")) pu->num=14; 
	if(strstr(pu->a,"K")) pu->num=13;
	if(strstr(pu->a,"Q")) pu->num=12;
	if(strstr(pu->a,"J")) pu->num=11;
	if(strstr(pu->a,"10")) pu->num=10;
	if(strstr(pu->a,"9")) pu->num=9;
	if(strstr(pu->a,"8")) pu->num=8;
	if(strstr(pu->a,"7")) pu->num=7;
	if(strstr(pu->a,"6")) pu->num=6;
	if(strstr(pu->a,"5")) pu->num=5;
	if(strstr(pu->a,"4")) pu->num=4;
	if(strstr(pu->a,"3")) pu->num=3;
	if(strstr(pu->a,"2")) pu->num=2;
}
bool cmp(puke p1,puke p2)
{
	if(p1.flower!=p2.flower) return p1.flower>p2.flower;
	if(p1.num!=p2.num) return p1.num>p2.num;
}
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		int n;
		cin>>n;
		puke *p=new puke[n];
		for(int i=0;i<n;i++) 
		{
			cin>>p[i].a;
		    tongyi(&p[i]);
		}
		sort(p,p+n,cmp);
		for(int i=0;i<n;i++)  
		{
			if(i<n-1) cout<<p[i].a<<" ";
			else cout<<p[i].a<<endl;
		}
		delete[] p;
	}
	
	return 0;
}

Keywords: C++ Algorithm

Added by soulroll on Sat, 15 Jan 2022 13:02:29 +0200