第十一届蓝桥杯国赛JavaB

A:563

#include <iostream>

using namespace std;
 
int main(int argc, char** argv) {
	int cnt = 0;
	for(int i = 1; i <= 2020; i++){
		int num = i;
		while(num){
			if(num % 10 == 2){
				cnt++;
				break;
			}
			num /= 10;
		}
	}
	cout<<cnt;
	return 0;
}

B:20312088

#include <iostream>
#include <cstring>
#include <algorithm>
#include <set>
#include <queue>


using namespace std;

typedef long long LL;
typedef pair<int,int> PII;
int st[10000][10000];
struct Node{
	int x,y,step;
}; 
int dx[4] = {0,1,0,-1}, dy[4] = {1,0,-1,0};

LL bfs(){
	queue<Node> q;
	q.push({3000,3000,0});
	q.push({2020+3000,11+3000,0});
	q.push({11+3000,14+3000,0});
	q.push({2000+3000,2000+3000,0});
	
	LL cnt = 0;
	while(q.size()){
		Node t = q.front(); q.pop();
		st[t.x][t.y] = 1;
		cnt++;
		if(t.step == 2020) continue;
		for(int i = 0; i < 4; i++){
			int a = t.x + dx[i];
			int b = t.y + dy[i];
			if(!st[a][b]){
				q.push({a,b,t.step+1});
				st[a][b] = 1;
			}
		}
	}
	return cnt;
}
int main(int argc, char** argv) {
	cout<<bfs();
	return 0;
}

C:39001250856960000
对于一个大于1正整数n可以分解质因数n = p1^a1 * p2^a2 * … * pk^ak,约数个数定理:
n的正约数个数 = (a1 + 1) * (a2 + 1) * … * (ak + 1)

#include <iostream>

using namespace std;

typedef long long LL;

int cnt[100000];
int main(int argc, char** argv) {
	LL p = 1;
	for(int i = 2; i <= 100; i++){
		p *= i;
		for(int j = 2; p >= j; j++){
			if(p % j == 0){
				while(p % j == 0){
					cnt[j] ++;
					p /= j;
				}
			}
		}
	}
	LL res = 1;
	for(int i = 1; i <= 200; i++) res *= cnt[i] + 1;
	cout<<res;
	return 0;
}

D:动态规划
3616159

#include <iostream>
#include <queue>
#include <cstring>
#include <set>

using namespace std;

typedef long long LL;

int f[300];
int main(){
	//string s = "lanqiao";
	string s = "tocyjkdzcieoiodfpbgcncsrjbhmugdnojjddhllnofawllbhfiadgdcdjstemphmnjihecoapdjjrprrqnhgccevdarufmliqijgihhfgdcmxvicfauachlifhafpdccfseflcdgjncadfclvfmadvrnaaahahndsikzssoywakgnfjjaihtniptwoulxbaeqkqhfwl";
	int n = s.length();
	
	for(int i = 0; i < n; i++){
		f[i] = 1;
		for(int j = 0; j < i; j++){
			if(s[i] > s[j]) f[i] += f[j];
			else if(s[i] == s[j]) f[i] -= f[j];
		}
	}
	int cnt = 0;
	for(int i = 0; i < n; i++) cnt += f[i];
	cout<<cnt;
	return 0;
}

E:玩具蛇
答案:552

#include <iostream>
#include <cstring>
#include <algorithm>
#include <set>
#include <vector>

using namespace std;

typedef long long LL;
const int N = 10;

int path[N][N];
bool st[N][N];
LL cnt;
int dx[4] = {0,1,0,-1}, dy[4] = {1,0,-1,0};
vector<int> v;

void dfs(int x,int y,int k){
	if(k == 16){
		cnt++;
		return;
	}else{
		st[x][y] = true;
		for(int i = 0; i < 4; i++){
			int a = x + dx[i];
			int b = y + dy[i];
			if(a < 0 || a >= 4 || b < 0 || b >= 4) continue;
			if(st[a][b]) continue;
			dfs(a,b,k+1);
		}
		st[x][y] = false;
	}
}
int main(){
	for(int i = 0; i < 4; i++)
		for(int j = 0; j < 4; j++){
			memset(st,0,sizeof st);
			dfs(i,j,1);
		}
	cout<<cnt<<endl;
	return 0;
}

F : 最长公共子序列模型

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

public class Main {
	
	static int N = 1010;
	static int n,m;
	static int f[][] = new int[N][N];
	static int a[] = new int[N];
	static int b[] = new int[N];
	static int num = 0;
	static Map<String, Integer> map = new HashMap<String, Integer>();
	
	
	static int get_Arrays(String s,int p[]) {
		int cnt = 0, st = 0;
		for(int i = 1; i < s.length(); i++) {
			if(s.charAt(i) >= 'A' && s.charAt(i) <= 'Z') {
				String key = s.substring(st,i);
				if(map.containsKey(key)) p[++cnt] = map.get(key);
				else {
					map.put(key, ++num);
					p[++cnt] = num;
				}
				st = i;
			}
		}
		String key = s.substring(st,s.length());
		if(map.containsKey(key)) p[++cnt] = map.get(key);
		else {
			map.put(key, ++num);
			p[++cnt] = num;
		}
		return cnt;
	}
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String s1 = br.readLine();
		String s2 = br.readLine();

		n = get_Arrays(s1, a);
		m = get_Arrays(s2, b);
		
		int res = 0;
		for(int i = 1; i <= n; i++)
			for(int j = 1; j <= m; j++) {
				f[i][j] = Math.max(f[i-1][j], f[i][j-1]);
				if(a[i] == b[j]) {
					f[i][j] = Math.max(f[i][j], f[i-1][j-1] + 1);
				}
				res = Math.max(res, f[i][j]);
			}
		System.out.println(res);
	}

}

皮亚诺曲线:

#include <iostream>
#include <cstring>
#include <cmath>

using namespace std;

typedef long long LL;

LL k,x1,y1,x2,y2;

LL dfs(LL x,LL y,LL n){
	
	LL pre_len = pow(3,n - 1); //小一阶的长度
	LL sum_len = 0; //总长度
	
	LL x3 =  x / pre_len, y3 = y / pre_len;
	//判断位置
	LL flag = 0;
	
	if(x3 == 0){
		if(y3 == 0) flag = 1;
		else if(y3 == 1) flag = 2;
		else if(y3 == 2) flag = 3;
	}
	else if(x3 == 1){
		if(y3 == 2) flag = 4;
		else if(y3 == 1) flag = 5;
		else flag = 6;
	}else{
		if(y3 == 0) flag = 7;
		else if(y3 == 1) flag = 8;
		else flag = 9;
	}
	sum_len += pre_len * pre_len * (flag - 1);
	if(n == 1) return sum_len;
	
	LL pre_x, pre_y; //下一层递归的坐标
	if(flag == 1) pre_x = x, pre_y = y;
	else if(flag == 2) pre_x = -(x - pre_len), pre_y = y - pre_len;
	else if(flag == 3) pre_x = x, pre_y = y - 2*pre_len;
	else if(flag == 4) pre_x = x - pre_len, pre_y = -(y - 2*pre_len);
	else if(flag == 5) pre_x = -(x - pre_len), pre_y = -(y - pre_len);
	else if(flag == 6) pre_x = x - pre_len, pre_y = -y;
	else if(flag == 7) pre_x = x - 2*pre_len, pre_y = y;
	else if(flag == 8) pre_x = -(x - 2*pre_len), pre_y = y - pre_len;
	else pre_x = x - 2*pre_len, pre_y = y - 2*pre_len;
	
	return sum_len + dfs(pre_x,pre_y,n - 1);
}

int main(){
	cin >>k>>x1>>y1>>x2>>y2;
	cout<<abs(dfs(x1,y1,k) - dfs(x2,y2,k));
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值