PAT甲级2014冬季全解

1. Rational Arithmetic

For two rational numbers, your task is to implement the basic arithmetics, that is, to calculate their sum, difference, product and quotient.

Input Specification:
Each input file contains one test case, which gives in one line the two rational numbers in the format a1/b1 a2/b2. The numerators and the denominators are all in the range of long int. If there is a negative sign, it must appear only in front of the numerator. The denominators are guaranteed to be non-zero numbers.

Output Specification:
For each test case, print in 4 lines the sum, difference, product and quotient of the two rational numbers, respectively. The format of each line is number1 operator number2 = result. Notice that all the rational numbers must be in their simplest form k a/b, where k is the integer part, and a/b is the simplest fraction part. If the number is negative, it must be included in a pair of parentheses. If the denominator in the division is zero, output Inf as the result. It is guaranteed that all the output integers are in the range of long int.

Sample Input 1:
2/3 -4/2

Sample Output 1:
2/3 + (-2) = (-1 1/3)
2/3 - (-2) = 2 2/3
2/3 * (-2) = (-1 1/3)
2/3 / (-2) = (-1/3)

Sample Input 2:
5/3 0/6

Sample Output 2:
1 2/3 + 0 = 1 2/3
1 2/3 - 0 = 1 2/3
1 2/3 * 0 = 0
1 2/3 / 0 = Inf

有点小麻烦的模拟题,考试最不想遇到这种签到题。
先写打印单个数字的函数再调用会简单许多。
注意先约分、特判0、辗转相除法中绝对值的考虑。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {
	return b==0?abs(a):gcd(b, a % b);
}
bool flag = false;
void print_num(ll num1,ll num2){
	int cnt=0;
	if(num2==1){
		if(num1<0) cout<<"("<<num1<<")";
		else cout<<num1;
	}
	else if(num2==0) cout<<"Inf";
	else if(num1==0) cout<<0;
	else{
		if(flag){
			int div=gcd(num1,num2);
			num1 /= div;
			num2 /= div;
		}
		if(num1<0){
			num1 = 0-num1;
			while(num1>num2){
				cnt++;
				num1 -= num2;
			}
			if(cnt) cout<<"(-"<<cnt<<" "<<num1<<"/"<<num2<<")";
			else cout<<"(-"<<num1<<"/"<<num2<<")";
		}else{
			while(num1>num2){
				cnt++;
				num1 -= num2;
			}
			if(cnt) cout<<cnt<<" "<<num1<<"/"<<num2;
			else cout<<num1<<"/"<<num2;
		}
	}
}
int main(){
	char a[20],b[20],op[4] = {'+','-','*','/'};
	ll num1,num2,num3,num4,re[4];
	scanf("%s %s",a,b);
	sscanf(a,"%lld/%lld",&num1,&num2);
	sscanf(b,"%lld/%lld",&num3,&num4);
	int div=gcd(num1,num2);
	num1 /= div,num2 /= div;
	div=gcd(num3,num4);
	num3 /= div,num4 /= div;
	ll rate1 = num2*num4,rate2 = num2*num3;
	re[0] = num1*num4+num3*num2;
	re[1] = num1*num4-num3*num2;
	re[2] = num1*num3;
	re[3] = num1*num4;
	if(rate2<0){
		rate2 = 0-rate2;
		re[3] = 0-re[3];
	}
	for(int i=0;i<4;i++){
		flag = false;
		if(i!=0) cout<<endl;
		print_num(num1,num2);
		cout<<" "<<op[i]<<" ";
		print_num(num3,num4);
		cout<<" = ";
		flag = true;
		print_num(re[i],i==3?rate2:rate1);
	}
}

2. Insert or Merge

According to Wikipedia:

Insertion sort iterates, consuming one input element each repetition, and growing a sorted output list. Each iteration, insertion sort removes one element from the input data, finds the location it belongs within the sorted list, and inserts it there. It repeats until no input elements remain.

Merge sort works as follows: Divide the unsorted list into N sublists, each containing 1 element (a list of 1 element is considered sorted). Then repeatedly merge two adjacent sublists to produce new sorted sublists until there is only 1 sublist remaining.

Now given the initial sequence of integers, together with a sequence which is a result of several iterations of some sorting method, can you tell which sorting method we are using?

Input Specification:
Each input file contains one test case. For each case, the first line gives a positive integer N (≤100). Then in the next line, N integers are given as the initial sequence. The last line contains the partially sorted sequence of the N numbers. It is assumed that the target sequence is always ascending. All the numbers in a line are separated by a space.

Output Specification:
For each test case, print in the first line either “Insertion Sort” or “Merge Sort” to indicate the method used to obtain the partial result. Then run this method for one more iteration and output in the second line the resuling sequence. It is guaranteed that the answer is unique for each test case. All the numbers in a line must be separated by a space, and there must be no extra space at the end of the line.

Sample Input 1:
10
3 1 2 8 7 5 9 4 6 0
1 2 3 7 8 5 9 4 6 0

Sample Output 1:
Insertion Sort
1 2 3 5 7 8 9 4 6 0

Sample Input 2:
10
3 1 2 8 7 5 9 4 0 6
1 3 2 8 5 7 4 9 0 6

Sample Output 2:
Merge Sort
1 2 3 8 4 5 7 9 0 6

先模拟插入排序,再模拟归并,归并可以直接计算排序,先两个两个,再四个四个…不用递归

#include<bits/stdc++.h>
using namespace std;
vector<int> seq1,seq2,temp,final;
int flag=0;
bool jud(){
	for(int i=0;i<temp.size();i++){
		if(temp[i]!=seq2[i]) return false;
	}
	return true;
}
void print(){
	for(int i=0;i<temp.size();i++){
		if(i!=0) cout<<" ";
		cout<<temp[i];
	}
}
int main(){
	int n,num;
	cin>>n;
	for(int i=0;i<n;i++){
		cin>>num;
		seq1.push_back(num);
	}
	for(int i=0;i<n;i++){
		cin>>num;
		seq2.push_back(num);
	}
	final=seq1,temp=seq1;
	sort(final.begin(),final.end());
	for(int i=2;i<temp.size();i++){
		sort(temp.begin(),temp.begin()+i);
		if(jud()){
			cout<<"Insertion Sort"<<endl;
			sort(temp.begin(),temp.begin()+i+1);
			print();
			return 0;
		}
	}
	temp = seq1;
	cout<<"Merge Sort"<<endl;
	int len = 2;
	while(len<=temp.size()){
		vector<int> tool2;
		for(int i=0;i<temp.size();i+=len){
			vector<int> tool1;
			for(int j=i;j<i+len&&j<temp.size();j++){
				tool1.push_back(temp[j]);
			}
			sort(tool1.begin(),tool1.end());
			for(int j=0;j<tool1.size();j++){
				tool2.push_back(tool1[j]);
			}
		}
		temp = tool2;
		if(flag){
			print();
			return 0;
		}
		if(jud()) flag = 1;
		len*=2;
	}
}

3. Highest Price in Supply Chain

A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyone involved in moving a product from supplier to customer.

Starting from one root supplier, everyone on the chain buys products from one’s supplier in a price P and sell or distribute them in a price that is r% higher than P. It is assumed that each member in the supply chain has exactly one supplier except the root supplier, and there is no supply cycle.

Now given a supply chain, you are supposed to tell the highest price we can expect from some retailers.

Input Specification:
Each input file contains one test case. For each case, The first line contains three positive numbers: N (≤10^5), the total number of the members in the supply chain (and hence they are numbered from 0 to N−1); P, the price given by the root supplier; and r, the percentage rate of price increment for each distributor or retailer. Then the next line contains N numbers, each number S^i is the index of the supplier for the i-th member. S^root for the root supplier is defined to be −1. All the numbers in a line are separated by a space.

Output Specification:
For each test case, print in one line the highest price we can expect from some retailers, accurate up to 2 decimal places, and the number of retailers that sell at the highest price. There must be one space between the two numbers. It is guaranteed that the price will not exceed 10^10

dfs确定价格,注意使用double

#include<bits/stdc++.h>
using namespace std;
vector<int> mem[100001],tris;
double price[100001],P,r,maxn=0;
int N,K,who,root,MAXN=0x3f3f3f3f,cnt=0;
void dfs(int root,double now_price){
	if(mem[root].size()==0) tris.push_back(root);
	for(int i=0;i<mem[root].size();i++)
		if(price[root]*(1+r/100.0)>price[mem[root][i]]) price[mem[root][i]] = price[root]*(1+r/100.0);
	for(int i=0;i<mem[root].size();i++) dfs(mem[root][i],price[mem[root][i]]);	
}
int main(){
	cin>>N>>P>>r;
	for(int i=0;i<N;i++){
		cin>>who;
		if(who==-1) root = i;
		else mem[who].push_back(i);
	}
	price[root] = P;
	dfs(root,P);
	for(int i=0;i<tris.size();i++)
		if(price[tris[i]]>maxn) maxn = price[tris[i]];
	for(int i=0;i<tris.size();i++)
		if(price[tris[i]]==maxn) cnt++;	
	printf("%.2lf %d",maxn,cnt);
}

4. Acute Stroke

One important factor to identify acute stroke (急性脑卒中) is the volume of the stroke core. Given the results of image analysis in which the core regions are identified in each MRI slice, your job is to calculate the volume of the stroke core.

Input Specification:
Each input file contains one test case. For each case, the first line contains 4 positive integers: M, N, L and T, where M and N are the sizes of each slice (i.e. pixels of a slice are in an M×N matrix, and the maximum resolution is 1286 by 128); L (≤60) is the number of slices of a brain; and T is the integer threshold (i.e. if the volume of a connected core is less than T, then that core must not be counted).

Then L slices are given. Each slice is represented by an M×N matrix of 0’s and 1’s, where 1 represents a pixel of stroke, and 0 means normal. Since the thickness of a slice is a constant, we only have to count the number of 1’s to obtain the volume. However, there might be several separated core regions in a brain, and only those with their volumes no less than T are counted. Two pixels are connected and hence belong to the same region if they share a common side, as shown by Figure 1 where all the 6 red pixels are connected to the blue one.

Output Specification:
For each case, output in a line the total volume of the stroke core.

柳神的bfs代码,dfs会递归爆栈,有的测试点过不了

#include <cstdio>
#include <queue>
using namespace std;
struct node {
    int x, y, z;
};
int m, n, l, t;
int X[6] = {1, 0, 0, -1, 0, 0};
int Y[6] = {0, 1, 0, 0, -1, 0};
int Z[6] = {0, 0, 1, 0, 0, -1};
int arr[1300][130][80];
bool visit[1300][130][80];
bool judge(int x, int y, int z) {
    if(x < 0 || x >= m || y < 0 || y >= n || z < 0 || z >= l) return false;
    if(arr[x][y][z] == 0 || visit[x][y][z] == true) return false;
    return true;
}
int bfs(int x, int y, int z) {
    int cnt = 0;
    node temp;
    temp.x = x, temp.y = y, temp.z = z;
    queue<node> q;
    q.push(temp);
    visit[x][y][z] = true;
    while(!q.empty()) {
        node top = q.front();
        q.pop();
        cnt++;
        for(int i = 0; i < 6; i++) {
            int tx = top.x + X[i];
            int ty = top.y + Y[i];
            int tz = top.z + Z[i];
            if(judge(tx, ty, tz)) {
                visit[tx][ty][tz] = true;
                temp.x = tx, temp.y = ty, temp.z = tz;
                q.push(temp);
            }
        }
    }
    if(cnt >= t)
        return cnt;
    else
        return 0;
    
}
int main() {
    scanf("%d %d %d %d", &m, &n, &l, &t);
    for(int i = 0; i < l; i++)
        for(int j = 0; j < m; j++)
            for(int k = 0; k < n; k++)
                scanf("%d", &arr[j][k][i]);
    int ans = 0;
    for(int i = 0; i < l; i++) {
        for(int j = 0; j < m; j++) {
            for(int k = 0; k < n; k++) {
                if(arr[j][k][i] == 1 && visit[j][k][i] == false)
                    ans += bfs(j, k, i);
            }
        }
    }
    printf("%d", ans);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值