PAT(甲级)2020年冬季考试模拟(C++)

在这里插入图片描述

#include <iostream>
#include <vector>
#include <cmath>
using namespace std;

int main() {
	int n;
	cin >> n;
	vector<int> Fab;
	Fab.push_back(0);
	Fab.push_back(1);
	for (int i = 2; Fab[Fab.size() - 1] < n; i++) {
		Fab.push_back(Fab[i - 1] + Fab[i - 2]);
	}
	//Fab.push_back(Fab[Fab.size()-1] + Fab[Fab.size() - 2]);
	int a = Fab[Fab.size() - 2],b= Fab[Fab.size() - 1];
	if (abs(a-n) <= abs(b-n)) {
		cout << a;
	}
	else {
		cout << b;
	}

	return 0;
}

在这里插入图片描述

#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main() {
	string S, P;
	getline(cin, S);
	getline(cin, P);
	vector<string> ans;
	for (int i = 0; i < S.length(); i++) {
		if (S[i] != P[0]) continue;
		else {
			string s = "";
			s = s + P[0];
			int index = 1;
			for (int j = i+1; j < S.length()&&index<P.length(); j++) {
				if (S[j] == P[index]) {
					index++;
				}
				s += S[j];
			}
			if (index == P.length()) ans.push_back(s);
		}	
	}
	string res = S;
	for (int i = 0; i < ans.size(); i++) {
		if (ans[i].size() < res.size()) {
			res = ans[i];
		}
	}
	cout << res;
	return 0;
}

在这里插入图片描述

#include <iostream>
#include <set>
#include <string>
#include <algorithm>
#include <vector>
#include <stack>
using namespace std;
const int maxn = 1010;
struct Node
{
	string ID;
	Node* child[maxn];
	int childNum;
	Node(string _ID,int num) :ID(_ID),childNum(num) {}
	Node() {}
};

void insertNode(Node* root, string s, int L) {
	int level = s.length() - 4;
	string ID = s.substr(level, 4);
	if (level == L) {
		root->child[root->childNum++] = new Node(ID, 0);
	}
	else {
		insertNode(root->child[root->childNum - 1], s, L + 1);
	}
}

//vector<string> path;
stack<string> path;
void DFS(Node* root,string id) {
	if (root->ID == id) {
		vector<string> ans;
		stack<string> path0 = path;
		while (!path0.empty())
		{
			ans.push_back(path0.top());
			path0.pop();
		}
		reverse(ans.begin(), ans.end());
		cout << "0000";
		for (int i = 0; i < ans.size(); i++) {
			cout << "->" << ans[i];
		}
		//cout << endl;
		return;
	}
	for (int i = 0; i < root->childNum; i++) {
		string ID = root->child[i]->ID;
		path.push(ID);
		DFS(root->child[i], id);
		path.pop();
	}
}

int main() {
	int N;
	cin >> N;
	getchar();
	string s;
	getline(cin, s);
	Node* root = new Node(s, 0);
	int curLev = 0;
	set<string> IDs;
	IDs.insert(s);
	int lev[maxn];
	
	for (int i = 1; i < N; i++) {
		getline(cin, s);
		insertNode(root, s, 1);
		int level = s.length() - 4;
		string ID = s.substr(level, 4);
		IDs.insert(ID);
	}
	int K;
	cin >> K;
	string id;
	for (int i = 0; i < K; i++) {
		cin >> id;
		if (IDs.count(id) > 0) {
			DFS(root, id);
		}
		else {
			cout << "Error: " << id << " is not found." ;
		}
		if (i != K - 1) {
			cout << endl;//换行
		}
	}
	return 0;
}

在这里插入图片描述

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include <algorithm>
#include <map>
using namespace std;
struct Equation
{
    int reactant[21];
    int reaNum=0;
    int product;
    //bool flag = true;
}equation[52];//存放所有的公式

bool cmp(Equation a, Equation b) {
    for (int i = 0; i < a.reaNum && i < b.reaNum; i++) {
        if (a.reactant[i] != b.reactant[i]) {
            return a.reactant[i] < b.reactant[i];
        }
    } 
}

int main() {
    int N, M, K;
    scanf("%d", &N);
    int num;
    map<int,bool> reactant;//反应物的集合
    
    for (int i = 0; i < N; i++) {
        scanf("%d", &num);
        reactant[num]=true;
    }
    scanf("%d", &M);
    int product[12];
    for (int i = 0; i < M; i++) {
        scanf("%d", &product[i]);//输入需要生成的物质
    }
    scanf("%d", &K);
    //输入K个公式
    for (int i = 0; i < K; i++) {
        char c;
        while (scanf("%d",&num)!=EOF)
        {
            equation[i].reactant[equation[i].reaNum++]=num;
            scanf("%c", &c);
            scanf("%c", &c);
            if (c == '-') {
                scanf("%c", &c);
                scanf("%c", &c);
                scanf("%d", &num);
                equation[i].product = num;
                break;
            }
        }
    }
    sort(equation, equation + K, cmp);
    for (int i = 0; i < M; i++) {
        bool flag = false;
        for (int j = 0; j < K; j++) {
            if (equation[j].product == product[i]) {
                
                int k = 0;
                for (; k < equation[j].reaNum; k++) {
                    if (reactant[equation[j].reactant[k]] == false) {
                        break;
                    }
                }
                if (k == equation[j].reaNum) {
                    for (int x = 0; x < equation[j].reaNum; x++) {
                        int r = equation[j].reactant[x];
                        printf("%02d ", r);
                        if (x < equation[j].reaNum - 1) {
                            printf("+ ");
                        }
                        else {
                            printf("-> ");
                        }       
                        reactant[r] = false;
                    }
                    printf("%02d", product[i]);
                    flag = true;
                    break;
                }
            }
        }
        if (flag == false) {
            printf("%02d -> %02d", product[i], product[i]);
        }
        if (i < M - 1) printf("\n");
    }

    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一匹好人呀

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值