CSP-J-2022题目及参考代码

参考文章(本文参考代码自己写的,和这个不同)

1. 乘方

https://www.luogu.com.cn/problem/P8813
CSP-J-2022题目及参考代码_第1张图片

#include 
using namespace std;

using LL = long long;

int main() {
#ifdef LOCAL
	freopen("t1.in", "r", stdin);
#endif
	LL a, b;
	scanf("%lld%lld", &a, &b);
	if(a == 1) {
        printf("1");
        return 0;
    }
	
	LL max_num = 1e9;
	//long max_num = 1000000000;
	
	LL t = 1;
	for(LL i = 1; i <= b; i++){
		t = t * a;
		if(t > max_num){
			t = -1;
			break;
		}
	}

	printf("%lld\n", t);
	
	return 0;
}

2. 解密

https://www.luogu.com.cn/problem/P8814
CSP-J-2022题目及参考代码_第2张图片

#include 
using namespace std;

using LL = long long;

int main() {
#ifdef LOCAL
	freopen("t2.in", "r", stdin);
#endif
	//pq = n, (p - 1) * (q - 1) = e * d - 1	 
	//m = n - e * d + 2
	//p = (m - sqrt(m * m - 4n)) / 2
	//q = (m + sqrt(m * m - 4n)) / 2 
	LL n, e, d, m, p, q;

	int k;
	scanf("%d", &k);	

	for(int i = 0; i < k; i++){
		scanf("%lld%lld%lld", &n, &e, &d);
		m = n - e * d + 2;
		LL x = m * m - 4 * n;
		if(x < 0){
			printf("NO\n");
			continue;
		}

		LL x1 = (LL)sqrt(x);
		if(x == x1 * x1){
			if((m - x1) % 2 == 0){
				p = (m - x1) / 2;
				q = (m + x1) / 2;
				printf("%d %d\n", p, q);
				continue;
			}
		}else if(x == (x1 + 1) * (x1 + 1)){
			if((m - x1 + 1) % 2 == 0){
				p = (m - x1 + 1) / 2;
				q = (m + x1 + 1) / 2;
				printf("%d %d\n", p, q);
				continue;
			}
		}
		printf("NO\n");
	}
	
	return 0;
}

3. 逻辑表达式

https://www.luogu.com.cn/problem/P8815

#include
using namespace std;

const int N = 1e6 + 10;
string s;
stack<char>	  st;
vector<char>  ve;

struct node{
	char c;
	int l,r;
}a[N];
int cnt , ans1 , ans2;

void change(){
	int len = s.size();
	for(int i = 0; i < len;i++){
		if(isdigit(s[i])){
			ve.push_back(s[i]);
		}
		else if(st.empty() || s[i] == '('){
			st.push(s[i]);
		}
		else if(s[i] == ')'){
			while(st.top() != '('){
				ve.push_back(st.top());
				st.pop();
			}
			st.pop();//左括号出栈
		}else if(s[i] == '&'){
			while(!st.empty() && st.top() == '&'){  
				ve.push_back(st.top());
				st.pop(); 
			}
			st.push(s[i]);
		}else{
			while((!st.empty() && st.top() == '&') || (!st.empty() && st.top() == '|')){
				ve.push_back(st.top());
				st.pop();
			} 
			st.push(s[i]);
		}	
	}
	
	while(!st.empty()){
		ve.push_back(st.top());
		st.pop();
	} 
}

void build(){
	stack<int> sts;
	for(auto k : ve){
		if(k == '0' || k == '1'){
			cnt++;
			a[cnt].c = k;
			a[cnt].l = -1;
			a[cnt].r = -1;
			sts.push(cnt);
		}else{
			int r = sts.top();
			sts.pop();
			
			int l = sts.top();
			sts.pop();

			cnt++;
			a[cnt].c = k;
			a[cnt].l = l;
			a[cnt].r = r;
			sts.push(cnt);
		}
	}
}

int dfs(int v){
	if(a[v].c == '1' || a[v].c == '0') return a[v].c - '0';
	int now = dfs(a[v].l);
	if(now == 1 && a[v].c == '|'){ans2++;return 1;}
	if(now == 0 && a[v].c == '&'){ans1++;return 0;}
	return dfs(a[v].r);
}

int main(){
	cin >> s;
	change();
	build();
	cout << dfs(cnt) << "\n" ; 
	cout << ans1 << " " << ans2;
	return 0;
}

4. 上升点列

https://www.luogu.com.cn/problem/P8816
CSP-J-2022题目及参考代码_第3张图片

#include 
using namespace std;

struct Point{
	int x;
	int y;
};

int cmp(Point a, Point b){
	if(a.x == b.x){
		return a.y < b.y;
	}else{
		return a.x < b.x;
	}
	
	return 1;
}

int main(){
#ifdef LOCAL
	freopen("t4.in", "r", stdin);
#endif
	int n, k;
	scanf("%d%d",&n, &k);
	//printf("%d %d\n", n, k);
		
	Point pt[n + 1];
	for(int i = 0; i < n; i++){
		int x, y;
		scanf("%d%d", &x, &y);
		//printf("%d %d\n", x, y);
		pt[i].x = x;
		pt[i].y = y;
	}
	
	//dp[i][j]:以第i个点结尾并且加j个点能构成的序列的最大长度
	int dp[n + 1][k + 1];
	memset(dp, 0, sizeof(dp));
	
	//对所有给定的点的x坐标从小到大排序
	sort(pt, pt + n, cmp);
	
	int ans;
	for(int i = 0; i < n; i++){//遍历给定的n个点
		dp[i][0] = 1;
		for(int j = 0; j < i; j++){//遍历比i点坐标x小的点
			if (pt[j].y > pt[i].y) continue; //满足题目要求横坐标、纵坐标值均单调不减,排序保障了x坐标,这里再保障y坐标
			int dxdy = abs(pt[i].x - pt[j].x) + abs(pt[i].y - pt[j].y);//j点到i点之间衔接距离
            for (int x = 0; x + dxdy - 1 <= k; x++) {
                int t = x + dxdy - 1;
                dp[i][t] = max(dp[i][t], dp[j][x] + dxdy);//由于j
			}
		}
	}
	
	for (int i = 0; i < n; i++){
        for (int j = 0; j <= k; j++){
            ans = max(ans, dp[i][j] + k - j);        	
		}
	}
    printf("%d", ans);
    return 0;
} 

你可能感兴趣的:(中国电子学会青少年等级考试,算法,c++,青少年编程,CSP/J)