/*
*时间:2018年4月23日19:57:53-2018年4月23日20:16:37
*题目:1036.Boys vs Girls
*分数:25
*编译器:g++
*题目描述:
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
Jean AA980920
NA
*/
//思路:这题在思路上没什么难的,也没什么特别的格式要求,只要稍微细心一些就可以
#include <iostream>
using namespace std;
#include <string.h>
struct student{
string name;
char gender;
string ID;
int grade;
};
int main()
{
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;//记录女生最高分
int Mgrade = 100;//记录男生最低分
int Fseq = -1;//记录女生最高分的学生的编号
int Mseq = -1;//记录男生最低分的学生的编号
for (int i=0; i<N; i++)
{
if (stu[i].gender == 'F')
{
if (stu[i].grade > Fgrade)//记录女生最高分
{
Fgrade = stu[i].grade;
Fseq = i;
}
}
else//记录男生最低分
{
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;//编号还是为-1的话表示没有记录到女生,则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;//任意一个性别没有记录到编号,则NA
return 0;
}
/*
*时间:2018年4月23日20:28:33-2018年4月23日20:50:20
*题目:1031.Hello World for U
*分数:20
*编译器:g++
*题目描述:
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
e l
l r
lowo
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 !
e d
l l
lowor
*/
//思路:这题稍微算一下n1、n2、n3的值就可以了,题目里说n1 = n3 = max { k| k <= n2 for all 3 <= n2 <= N }
//于是n1 = n3 = (N+2)/3; N+2是表示有两个角上是重叠的,一共n1行,n2列,前面n1-1行中间加n2-2个空格
#include <iostream>
using namespace std;
#include <string.h>
#define Debug false
int main()
{
string str;
cin>>str;
int len = str.size();
int n1 = (len+2) / 3;//计算n1,n3
int n3 = n1;
int n2 = len + 2 - n1 - n3;//计算n2,其实这部是可以不用算的直接用len - n1 - n2就可以得到space的个数
int spaceNum = n2 -2;
if (Debug) cout<<"n1:"<<n1<<" n2:"<<n2<<" n3:"<<n3<<" spaceNum:"<<spaceNum<<endl;//Debug,基本有问题就在前面计算,这个很有必要
for (int i=0; i<n1; i++)
{
if (i != n1-1)//前n-1行
{
cout<<str[i];//先输出行的第一个
for (int j=0; j<spaceNum; j++)//输出中间的space
cout<<" ";
cout<<str[len-1-i]<<endl;//再输出行的最后一个
}
else
for (int i=n1-1; i<n1+1+spaceNum; i++)//第n1行特殊处理,输出n1-1到n1+spaceNum共spaceNum+2(也就是n2)个字符
cout<<str[i];
}
return 0;
}
【PAT甲】A1036、A1031
最新推荐文章于 2020-05-14 23:30:35 发布