Pat 甲级 训练真题集 1004&1005&1006&1007&1008!

1004. Counting Leaves (30)

Input

Each input file contains one test case. Each case starts with a line containing 0 < N < 100, the number of nodes in a tree, and M (< N), the number of non-leaf nodes. Then M lines follow, each in the format:

ID K ID[1] ID[2] ... ID[K]

Output

For each test case, you are supposed to count those family members who have no child for every seniority level starting from the root. The numbers must be printed in a line, separated by a space, and there must be no extra space at the end of each line.

The sample case represents a tree with only 2 nodes, where 01 is the root and 02 is its only child. Hence on the root 01 level, there is 0 leaf node; and on the next level, there is 1 leaf node. Then we should output “0 1” in a line.

Sample Input

2 1
01 1 02

Sample Output

0 1

对于这个题是要找到每一层的叶子节点数

由于是找到每一层的叶子节点,所以可以用层次遍历BFS

利用队列,先进先出,取出一个点后查看它是否有下一代,若有下一代,则push进入队列

代码如下:(参照了陈越姥姥在慕课网中的数据结构课与PAT解题报告

#include<iostream>
#include<queue>
using namespace std;

#define MAX 100

int map[MAX][MAX];
int all_id[MAX];
int N,M;
queue<int> que;

//由于总是从01开始,所以bfs就可以没有参数  利用队列进队列出队列以0分层
void bfs(){
	que.push(1);    //将根节点push进去
	que.push(0);    //利用push(0)进行分层
	int deaf=0;
	while(!que.empty()){
		int node=que.front();    //获得当前节点
		que.pop();                    //将其pop
		if(node!=0){
			int flag=1;           //用于判断是否有下一代
			for(int i=1;i<=N;++i){
				if(map[node][i]==1){
					que.push(i);
					flag=0;
				}
			}
			deaf+=flag;           //flag若为0,说明有下一代,为1则没有
		}else{
			if(que.empty()){
				cout<<deaf;
				break;
			}else{
				que.push(0);
				cout<<deaf<<" ";
				deaf=0;
			}
		}
	}
}

int main(){
	cin>>N>>M;
	int id,K,idk;
	for(int i=0;i<M;++i){
		cin>>id>>K;
		for(int j=0;j<K;++j){
			cin>>idk;
			all_id[id]=1;
			map[id][idk]=1;      //初始化树
		}
	}
	bfs();
	return 0;
}

1005. Spell It Right (20)

Given a non-negative integer N, your task is to compute the sum of all the digits of N, and output every digit of the sum in English.

Input Specification:

Each input file contains one test case. Each case occupies one line which contains an N (<= 10100).

Output Specification:

For each test case, output in one line the digits of the sum in English words. There must be one space between two consecutive words, but no extra space at the end of a line.

Sample Input:

12345

Sample Output:

one five

这个题就是要你分离输入的数,将每个数加起来,然后用英文输出

我在这个题中利用了stringstream,它用来转化数字与字符串很方便

以下为代码

#include<iostream>
#include<string>
#include<sstream>
using namespace std;

void print_English(string s){
	if(s=="0"){
		cout<<"zero";
		return;
	}
	if(s=="1"){
		cout<<"one";
		return;
	}
	if(s=="2"){
		cout<<"two";
		return;
	}
	if(s=="3"){
		cout<<"three";
		return;
	}
	if(s=="4"){
		cout<<"four";
		return;
	}
	if(s=="5"){
		cout<<"five";
		return;
	}
	if(s=="6"){
		cout<<"six";
		return;
	}
	if(s=="7"){
		cout<<"seven";
		return;
	}
	if(s=="8"){
		cout<<"eight";
		return;
	}
	if(s=="9"){
		cout<<"nine";
		return;
	}
}
void digit_to_English(int sum){
	stringstream ss;
	string s;
	string s1;
	ss<<sum;
	ss>>s;
	for(int i=0;i<s.size();++i){
		s1=s[i];
		print_English(s1);
		if(i!=s.size()-1){
			cout<<" ";
		}
	}

}
int main(){
	string x;
	cin>>x;
	stringstream ss;
	
	int sum=0;
	int a=0;
	for(auto begin=x.begin();begin!=x.end();++begin){
		ss<<*begin;
		ss>>a;
		sum+=a;
		ss.clear();
	}
	digit_to_English(sum);
	return 0;
}

1006. Sign In and Sign Out (25)

At the beginning of every day, the first person who signs in the computer room will unlock the door, and the last one who signs out will lock the door. Given the records of signing in’s and out’s, you are supposed to find the ones who have unlocked and locked the door on that day.

Input Specification:

Each input file contains one test case. Each case contains the records for one day. The case starts with a positive integer M, which is the total number of records, followed by M lines, each in the format:

ID_number Sign_in_time Sign_out_time

where times are given in the format HH:MM:SS, and ID number is a string with no more than 15 characters.

Output Specification:

For each test case, output in one line the ID numbers of the persons who have unlocked and locked the door on that day. The two ID numbers must be separated by one space.

Note: It is guaranteed that the records are consistent. That is, the sign in time must be earlier than the sign out time for each person, and there are no two persons sign in or out at the same moment.

Sample Input:

3
CS301111 15:30:28 17:00:10
SC3021234 08:00:00 11:25:25
CS301133 21:45:00 21:58:40

Sample Output:

SC3021234 CS301133

这道题的大致题意为找到开门的人与关门的人,根据时间来做

以下为代码,利用STL中的sort函数,不过自己重载了操作符<

#include<iostream>
#include <algorithm>
#include<string>
#include<vector>
using namespace std;
class Row{
public:
	string ID_number;
	string Sign_in_time;
	string Sign_out_time;
	bool operator < (const Row &r)const {
		return Sign_in_time < r.Sign_in_time;
	}
};
bool greater_out_time (const Row &r1,const Row &r2){
	return r1.Sign_out_time > r2.Sign_out_time;
	}
int main(){
	int M;
	cin>>M;
	string ID_number;
	string Sign_in_time;
	string Sign_out_time;
	vector<Row> vec;
	for(int i=0;i<M;++i){
		cin>>ID_number>>Sign_in_time>>Sign_out_time;
		Row r;
		r.ID_number=ID_number;
		r.Sign_in_time=Sign_in_time;
		r.Sign_out_time=Sign_out_time;
		vec.push_back(r);
	}
	sort(vec.begin(),vec.end());   //根据sign_in_time排序  从小到大
	cout<<vec[0].ID_number<<" ";

	sort(vec.begin(),vec.end(),greater_out_time);   //根据sign_out_time排序从大到小
	cout<<vec[0].ID_number;
	return 0;
}

1007. Maximum Subsequence Sum (25)

Given a sequence of K integers { N1, N2, …, NK }. A continuous subsequence is defined to be { Ni, Ni+1, …, Nj } where 1 <= i <= j <= K. The Maximum Subsequence is the continuous subsequence which has the largest sum of its elements. For example, given sequence { -2, 11, -4, 13, -5, -2 }, its maximum subsequence is { 11, -4, 13 } with the largest sum being 20.

Now you are supposed to find the largest sum, together with the first and the last numbers of the maximum subsequence.

Input Specification:

Each input file contains one test case. Each case occupies two lines. The first line contains a positive integer K (<= 10000). The second line contains K numbers, separated by a space.

Output Specification:

For each test case, output in one line the largest sum, together with the first and the last numbers of the maximum subsequence. The numbers must be separated by one space, but there must be no extra space at the end of a line. In case that the maximum subsequence is not unique, output the one with the smallest indices i and j (as shown by the sample case). If all the K numbers are negative, then its maximum sum is defined to be 0, and you are supposed to output the first and the last numbers of the whole sequence.

Sample Input:

10
-10 1 2 3 4 -5 -23 3 7 -21

Sample Output:

10 1 4

这道题的题意是求最大子序列,输出最大和与第一个数,最后一个数

若全为负数,则输出为0,整个的第一个数与最后一个数,若有相同和子序列,输出第一个子序列(i,j最小的)

代码如下

#include<iostream>
using namespace std;
#define MAX 10000

int sequence[MAX];
int main(){
	int K;
	cin>>K;
	int value;
	for(int i=0;i<K;++i){
		cin>>value;
		sequence[i]=value;
	}
	int sum=-1;
	int sumx=-1;
	int index=K;
	int start;
	int startindex;
	int end;
	for(int i=0;i<K;++i){
		if(sequence[i]<0){
			continue;
		}else{
			index=i;
			break;
		}
	}
	if(index==K){
		cout<<"0"<<" "<<sequence[0]<<" "<<sequence[K-1];
		return 0;
	}
	sum=sumx=sequence[index];
	start=startindex=end=index;
	for(int i=index+1;i<K;++i){
		sumx+=sequence[i];
		if(sumx>sum){
			sum=sumx;
			start=startindex;
			end=i;
		}else if(sumx<0){
			sumx=0;
			startindex=i+1;
		}
	}
	cout<<sum<<" "<<sequence[start]<<" "<<sequence[end];
	return 0;
}

1008. Elevator (20)

The highest building in our city has only one elevator. A request list is made up with N positive numbers. The numbers denote at which floors the elevator will stop, in specified order. It costs 6 seconds to move the elevator up one floor, and 4 seconds to move down one floor. The elevator will stay for 5 seconds at each stop.

For a given request list, you are to compute the total time spent to fulfill the requests on the list. The elevator is on the 0th floor at the beginning and does not have to return to the ground floor when the requests are fulfilled.

Input Specification:

Each input file contains one test case. Each case contains a positive integer N, followed by N positive numbers. All the numbers in the input are less than 100.

Output Specification:

For each test case, print the total time on a single line.

Sample Input:

3 2 3 1

Sample Output:

41

这个题的题意为要你求出所以花费时间,

可分为一个上楼时间,一个下楼时间,一个停留时间

停留时间为N*5 上楼时间为上楼次数*6 下楼时间为下楼次数*4

代码如下

#include<iostream>
using namespace std;

#define MAX 101

int floors[MAX];

int main(){
	int N;
	cin>>N;
	int floor;
	floors[0]=0;
	for(int i=1;i<=N;++i){
		cin>>floor;
		floors[i]=floor;
	}
	int move_up=0;
	int move_down=0;
	for(int i=1;i<=N;++i){
		if(floors[i]>floors[i-1]){
			move_up+=floors[i]-floors[i-1];
		}else{
			move_down+=floors[i-1]-floors[i];
		}
	}
	int sum=0;
	sum=N*5+move_up*6+move_down*4;
	cout<<sum;

	return 0;
}

打赏一个呗

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦