PAT Basic Level-Golden Rank Sample Volume 1

The first three questions are old questions, and the last two questions are new questions.

7-1 Pretend to sleep (10 points)

#include<iostream>
using namespace std;
int main(){
    int n,t1,t2;
    string name;
    cin>>n;
    for(int i=0;i<n;i++){
        cin>>name>>t1>>t2;
        if(t1<15||t1>20||t2<50||t2>70)
            cout<<name<<endl;
    }
    return 0;
}

7-2 Electronic Wang (10 points)

#include<iostream>
using namespace std;
int main(){
    int a,b;
    cin>>a>>b;
    int n=a+b;
    while(n--){
    	cout<<"Wang!";	
	}
    return 0;
}

7-3 Is it too fat? (10 points)

#include<iostream>
using namespace std;
int main(){
    int n;
    cin>>n;
    double h,w,biaozhun,k;
    for(int i=0;i<n;i++){
    	cin>>h>>w;//w: 市斤=2*公斤
		biaozhun=(h-100)*0.9;//公斤	
		if((w/2-biaozhun)<0){
			if(-1*(w/2-biaozhun)<biaozhun*0.1)
				cout<<"You are wan mei!"<<endl;
			else  cout<<"You are tai shou le!"<<endl;
		}else{
			if((w/2-biaozhun)<biaozhun*0.1)
				cout<<"You are wan mei!"<<endl;
			else  cout<<"You are tai pang le!"<<endl;
		}
	}
    return 0;
}

7-4 How many two (15 points)

An integer " degree of guilty two " is defined as the ratio of the number of 2 in the number to its digit. If the number is negative, the degree is increased by 0.5 times; if it is still an even number, it is increased by 1 time. For example, if the number -13142223336is an 11-digit number, there are 3 2s, and it is a negative number, and it is also an even number, then its degree of guilty two is calculated as: 3/11×1.5×2×100%, which is about 81.82%. This question asks you to calculate how two a given integer is.

Input format:

Enter the first line to give an integer no more than 50 digits N.

Output format:

Output Nthe degree of committing two in one line , keeping two decimal places.

Input sample:

-13142223336

Sample output:

81.82%

Code:

#include<iostream>
#include <cstdio>
#include<cstring>
using namespace std;
int main(){
    string s;
    cin>>s;
    double res=1.0;
    res*=(s[0]=='-')? 1.5:1.0;  //程度1
    res*=((s[s.length()-1]-'0')%2==0)?2:1; //程度2
    int k=0;
    if(s[0]=='-') k=1;
    int cnt=0;
    for(int i=0;i<s.length();i++){
    	if(s[i]=='2')  cnt++;
	}
	res*=(double)cnt/(s.length()-k)*100;
	printf("%.2f%%",res);
	return 0;
}

7-5 One group of one (15 points)

"One group, one study group" is a common learning organization in elementary and middle schools. Teachers put students with high academic performance in a group with those with low academic performance. For this question, please write a program to help the teacher automatically complete this assignment, that is, after getting the ranking of the whole class, among the students who are not currently grouped, divide the students with the highest ranking and the students of the opposite sex with the lowest ranking into one. group.

Input format:

Enter the first line to give a positive even number N(≤50), that is, the number of students in the class. After Nthat, give each student's gender (0 for girls and 1 for boys) and name (a non-empty string of no more than 8 English letters) in order from high to low, separated by a space. It is guaranteed that the male to female ratio in this class is 1:1, and there is no tie.

Output format:

Each line outputs the names of a group of two students, separated by a space. The high-ranking students are at the top, and the low-ranking students are at the bottom. The output order of the group is arranged from high to low according to the ranking of the previous students.

Input sample:

8
0 Amy
1 Tom
1 Bill
0 Cindy
0 Maya
1 John
1 Jack
0 Linda

Sample output:

Amy Jack
Tom Linda
Bill Maya
Cindy John

 Code:

#include<iostream>
using namespace std;
struct student{
	int sex;
	string name;
	int flag=0;
};
int main(){
	int n,cnt=0;
	cin>>n;
	student stu[n];
	for(int i=0;i<n;i++)
		cin>>stu[i].sex>>stu[i].name;
	for(int i=0;i<n;i++){
		for(int j=n-1;j>=0;j--){
			if(stu[i].sex!=stu[j].sex&&stu[i].flag==0&&stu[j].flag==0)
			{
				stu[i].flag=1;
				stu[j].flag=1;
				cnt+=2;
				cout<<stu[i].name<<" "<<stu[j].name<<endl;
			}
			if(cnt==n) return 0;	
		}
	}
	return 0;
}

Attention to detail!

Guess you like

Origin blog.csdn.net/WKX_5/article/details/114301217