【PAT A】A1036, A1031

/*
*Time: April 23, 2018 19:57:53 - April 23, 2018 20:16:37
*Title: 1036. Boys vs Girls
*Score: 25
*Compiler: g++
*Title description:
This time you are asked to tell the difference between the lowest grade of all the male students
and the highest grade of all the female students.

Input Specification:

Each input file contains one test case. Each case contains a positive integer N, followed by N lines of student
information. Each line contains a student's name, gender, ID and grade, separated by a space, where name and ID are
strings of no more than 10 characters with no space, gender is either F (female) or M (male), and grade is an integer
 between 0 and 100. It is guaranteed that all the grades are distinct.

Output Specification:

For each test case, output in 3 lines. The first line gives the name and ID of the female student with the highest grade,
and the second line gives that of the male student with the lowest grade. The third line gives the difference gradeF-gradeM.
If one such kind of student is missing, output "Absent" in the corresponding line, and output "NA" in the third line instead.

Sample Input 1:

3
Joe M Math990112 89
Mike M CS991301 100
Mary F EE990830 95

Sample Output 1:

Mary EE990830
Joe Math990112
6

Sample Input 2:

1
Jean M AA980920 60

Sample Output 2:

Absent
Jeans AA980920
NA
*/
//Thinking: This question is not difficult in terms of thinking, and there are no special format requirements, as long as you are a little more careful
#include <iostream>
using namespace std;
#include <string.h>
struct student{
	string name;
	char gender;
	string ID;
	int grade;
};
intmain()
{
	int N;
	cin>>N;
	student stu[N];
	for (int i=0; i<N; i++)
	{
		cin>>stu[i].name>>stu[i].gender>>stu[i].ID>>stu[i].grade;
	}
	int Fgrade = 0;//Record the highest score of girls
	int Mgrade = 100;//Record the lowest score for boys
	int Fseq = -1;//Record the number of the student with the highest score for the girl
	int Mseq = -1;//Record the number of the student with the lowest score for boys
	for (int i=0; i<N; i++)
	{
		if (stu[i].gender == 'F')
		{
			if (stu[i].grade > Fgrade)//Record the highest score of girls
			{
				Fgrade = stu[i].grade;
				Fseq = i;
			}
		}
		else//Record the lowest score for boys
		{
			if (stu[i].grade < Mgrade)
			{
				Mgrade = stu[i].grade;
				Mseq = i;
			}
		}
	}
	if (Fseq == -1) cout<<"Absent"<<endl; else cout<<stu[Fseq].name<<" "<<stu[Fseq].ID<<endl;//The number is still - 1 means that no girl is recorded, then Abeset
	if (Mseq == -1) cout<<"Absent"<<endl; else cout<<stu[Mseq].name<<" "<<stu[Mseq].ID<<endl;
	if (Mseq == -1 || Fseq == -1) cout<<"NA"<<endl; else cout<<stu[Fseq].grade-stu[Mseq].grade<<endl;//any one Gender is not recorded to the ID, then NA
	return 0;
}



/*
*Time: April 23, 2018 20:28:33 - April 23, 2018 20:50:20
*Title: 1031.Hello World for U
*Score: 20
*Compiler: g++
*Title description:
Given any string of N (>=5) characters, you are asked to form the characters into the shape of U. For example,
 "helloworld" can be printed as:
h  d
el
l  r
that

That is, the characters must be printed in the original order, starting top-down from the left vertical line with n1
 characters, then left to right along the bottom line with n2 characters, and finally bottom-up along the vertical
 line with n3 characters. And more, we would like U to be as squared as possible -- that is, it must be satisfied that
 n1 = n3 = max { k| k <= n2 for all 3 <= n2 <= N } with n1 + n2 + n3 - 2 = N.

Input Specification:

Each input file contains one test case. Each case contains one string with no less than 5 and no more than 80 characters
 in a line. The string contains no white space.

Output Specification:

For each test case, print the input string in the shape of U as specified in the description.
Sample Input:

helloworld!

Sample Output:

h   !
and
l   l
lowor
*/
//Thinking: This question is enough to calculate the values ​​of n1, n2, and n3. The question says n1 = n3 = max { k| k <= n2 for all 3 <= n2 <= N }
//So n1 = n3 = (N+2)/3; N+2 means that there are two overlapping corners, a total of n1 rows, n2 columns, and n2-2 spaces are added between the first n1-1 rows
#include <iostream>
using namespace std;
#include <string.h>
#define Debug false
intmain()
{
	string str;
	cin>>str;
	int len = str.size();
	int n1 = (len+2) / 3;//Calculate n1,n3
	int n3 = n1;
	int n2 = len + 2 - n1 - n3;//Calculate n2, in fact, this can be directly used len - n1 - n2 to get the number of spaces without calculation
	int spaceNum = n2 -2;
	if (Debug) cout<<"n1:"<<n1<<" n2:"<<n2<<" n3:"<<n3<<" spaceNum:"<<spaceNum<<endl;//Debug, basic If there is a problem, calculate it in front, this is very necessary
	for (int i=0; i<n1; i++)
	{
		if (i != n1-1)//First n-1 lines
		{
			cout<<str[i];//The first output of the line
			for (int j=0; j<spaceNum; j++)//output the space in the middle
				cout<<" ";
			cout<<str[len-1-i]<<endl;//The last line of output
		}
		else
			for (int i=n1-1; i<n1+1+spaceNum; i++)//Special processing of the n1th line, output n1-1 to n1+spaceNum total spaceNum+2 (that is, n2) characters
				cout<<str[i];	
	}
	
	return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324737947&siteId=291194637