跳到正文

题解

第六次训练课总结

Gua20131127约 9 分钟
原文
文章信息与版本

文章信息

发布
2025年7月11日
收录
2025年12月12日
更新
2026年10月28日
篇幅
约 9 分钟

版本

  1. v1当前

T1:

解题思路:通过枚举三角形的三条边,找出符合要求的三角形。

代码:

C++
#include<bits/stdc++.h>
using namespace std;
int main(){
	int n;
	cin >> n;
	int ans = 0;
	for(int i = 1;i <= n;i ++){
		for(int j = i + 1;j <= n;j ++){
			for(int k = j + 1;k <= n;k ++){
        //枚举三条边
				if(i * i + j * j == k * k && k <= n){
					ans ++;
				}
        //找出三角形
			}
		}
	}
	cout << ans;
	return 0;
}

T2:

解题思路:给定n个字符串,m个单词。要我们判断每个单词是哪些字符串的后缀。

代码:

C++
#include<bits/stdc++.h>
using namespace std;
string a[100010];
int main(){
	int n , q;
	cin >> n >> q;
	for(int i = 1;i <= n;i ++){
		cin >> a[i];
	}
	for(int i = 1;i <= q;i ++){
        int ans = 0;
		int len;
        string b;
        cin >> len >> b;
        for(int j = 1;j <= n;j ++){
            //枚举这个字符串是哪些字符串的后缀
            if(a[j].size() < len)
            //如果一个字符串长度小于这个单词,不可能是后缀
                continue;
    		if(a[j].substr(a[j].size() - len) == b){
                ans ++;
            }
    	}
        cout << ans << endl;
	}
	return 0;
}

T3:

解题思路:在x ~ y这个区间内,找最大公约数的倍数(实际上在找P),并找出对应的Q。判断是否正确。

代码:

C++
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int lcm(int a, int b) {
	return a / __gcd(a, b) * b;
}

int main() {
	int x, y, ans = 0;
	cin >> x >> y;


	for (int p = x; p <= y; p += x) {
		int q = x * y / p;

		if (x * y % p == 0 && __gcd(p, q) == x && lcm(p, q) == y)
			ans ++;
	}
	cout << ans;
	return 0;
}

T4:

解题思路:从起点开始判断上下左右是否可以走过去(即为0),如果可以走,就往那个方向走,如果不行的话就接着枚举下一个方向。(如果可以走就从那个方向再往下枚举)。

代码:

C++
#include<bits/stdc++.h>
using namespace std;
struct node{
	int x , y;
};
int n , ans = 0;
char a[1010][1010];
int vis[1010][1010];
int d[4][2] = {{-1 , 0} , {0 , -1} , {0 , 1} , {1 , 0}};
int x1 , y3 , x3 , y4;
void bfs(){
  //初始化
	for(int i = 1;i <= n;i ++){
		for(int j = 1;j <= n;j ++){
			vis[i][j] = -1;
		}
	}
	queue<node>q;
	q.push({x1 , y3});
	vis[x1][y3] = 0;
	while(q.size()){
		node t = q.front();
		q.pop();
		for(int i = 0;i < 4;i ++){
      //四联通
			int px = t.x + d[i][0];
			int py = t.y + d[i][1];
			if(px < 1 || px > n || py < 1 || py > n){
        //越界
				continue;
			}
			if(a[px][py] == '1'){
        //障碍
				continue;
			}
			if(vis[px][py] != -1){
        //枚举过了
				continue;
			}
			vis[px][py] = vis[t.x][t.y] + 1;
			q.push({px , py});
		}
	}
}
int main(){
	cin >> n;
	for(int i = 1;i <= n;i ++){
		for(int j = 1;j <= n;j ++){
			cin >> a[i][j]; 
		} 
	}
	cin >> x1 >> y3 >> x3 >> y4;
    bfs();
	cout << vis[x3][y4];
	return 0;
}

T5:

解题思路:

1、给定n * n株花生苗。

2、我们现在要在限定的时间内去摘取最多的花生

3、规定我们必须从大的开始摘,不管距离的远近。

(1) 按照每株花生苗的花生数量从大小排序

(2) 从前往后摘取,如果不能摘了那就结束

代码:

C++
#include <bits/stdc++.h>
using namespace std;
int n, m, k, t, cnt, sum;

struct node {
	int x, y, z;
} a[410];

bool cmp(node x, node y) {
	return x.z > y.z;
}

int main() {
	cin >> n >> m >> k;
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			cin >> t;
			if (t != 0) {
				cnt++;
				a[cnt].z = t;
				a[cnt].x = i;
				a[cnt].y = j;
			}
		}
	}
	sort(a + 1, a + 1 + cnt, cmp);
  //这一株如果能摘,那么我们必须能够从当前的位置,走到这株花生,摘下花生,并且回到路边,这个总时间必须小于等于我可以用来采摘的时间。
	if (a[1].x * 2 + 1 <= k) {
		k -= a[1].x + 1;
		sum += a[1].z;
	} else {
		cout << 0;
		return 0;
	}
	for (int i = 2; i <= cnt; i++) {
		int u = abs(a[i].x - a[i - 1].x) + abs(a[i].y - a[i - 1].y);
		if (u + 1 + a[i].x <= k) {
			k -= 1 + u;
			sum += a[i].z;
		} else {
			break;
		}
	}
	cout << sum;
	return 0;
}

T6:

解题思路:

斐波那契数列:

f(1) = f(n - 1) + f(n - 2);

新的斐波那契数列:

f(1) = 1;

f(2) = a;

f(3) = 1 + a;

f(4) = 1 + 2 * a;

f(5) = 2 + 3 * a;

统一后:

代码
 a[i] b[i]

f(1) = 1 + 0 * a;

f(2) = 0 + 1 * a;

f(3) = 1 + 1 * a;

f(4) = 1 + 2 * a;

f(5) = 2 + 3 * a;

a[i] = a[i - 1] + a[i - 2];

b[i] = b[i - 1] + b[i - 2];

给定一个x,求出有多少个对应的n和a,要求f(n) == x。

我们可以考虑枚举n,因为不管是a[n]还是b[n],都是一个斐波那契数列,且x的取值比较小,所以n的值一定不会特别大。

代码:

C++
#include<bits/stdc++.h>
using namespace std;
#define int long long
int f[10010] , p[10010];
signed main(){
    int T;
    cin >> T;
    while(T --){
        int x;
        cin >> x;
        //先求f[i] , p[i]
        p[1] = 0 , f[1] = 1;
        for(int i = 2; ;i ++){
            if(i == 2){
                p[i] = 1 , f[i] = 0;
            }else{
                f[i] = f[i - 1] + f[i - 2];
                p[i] = p[i - 1] + p[i - 2];
            }
            //接下来求解f[i] + p[i] * a能否正好等于x
            int val = x - f[i];
            if(val <= 0){
                break;
            }
            if(val % p[i] == 0){
                cout << i << ' ' << val / p[i] << endl;
            }
        }
    }
    return 0;
}

T7未讲解。

原文