跳到正文

个人记录

2.3总结

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

文章信息

发布
2026年2月3日
收录
2026年2月3日
更新
2026年11月21日
篇幅
约 9 分钟

版本

  1. v2当前
  2. v1

总结格式

T1

思路

暴力枚举判断条件。。

第一次代码

如果代码错了,找到代码的错因,再改代码

循环优化层级

利用题目中的一些条件,做判断,把判断条件改为计算的公式,来做到减少层级的效果。

C++
for (int i=0; i<=x; i++) {
  int j = x-i;
}

第二题思路

循环列出其中两个数字,利用三个数的总和等于n 这个条件,直接算出第三个数。

保证每次列的数字,都大于上次的数字(组合型枚举)。

C++
for (int i=1..) {
  for (int j=i+1..) {
    k = n-i-j;
    if (k > j) {
      // 保证i,j,k是升序的,避免重复
    }
  }
}

数位分离:

C++
// 分离数字x每一位
int flag = 1;
while (x > 0) {
  int t = x % 10;
  x /= 10;
}
// 分离结束后,x会变成0

代码:

C++
#include <bits/stdc++.h>
#define int long long
#define qwq ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
using namespace std;

int n;

bool check(int x) {
	while (x > 0) {
		if (x % 10 == 3 or x % 10 == 7) return 0;
		x /= 10;
	}
	return 1;
}

signed main() {
	qwq;
	int res = 0;
	cin >> n;
	for (int i=1; i<=n; i++) {
		for (int j=i+1; j<=n; j++) {
			int k = n-i-j;
			if (k <= j) continue;

			if (check(i) and check(j) and check(k)) {
				res++;
			}
		}
	}
	cout << res;
	return 0;
}

T3

找到所有的三个三位数,使得它们当中数字 1∼91 \sim 9 各出现一次,并且约分后的比例是 A:B:CA:B:C

因为数字之间比例固定,枚举其中一个数字,利用比例算出另外两个数。

先把 A:B:CA:B:C 约分。

C++
int t = __gcd(a, __gcd(b, c));
a /= t, b /= t, c /= t;

枚举出其中的一个数字:

枚举 i,枚举数字的一份是多少,第一个数字就是 i*a,第二个数字就是 i*b,第三个数字 i*c

满足的条件:

  1. 使数字 1∼91 \sim 9 各出现一次,用桶实现,记得每次重置。
  2. 三个数都必须是三位数
C++
#include <bits/stdc++.h>
#define int long long
#define qwq ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
using namespace std;

int a, b, c;
int tt[15];

void f(int x) {
	while (x > 0) {
		tt[x%10]++;
		x /= 10;
	}
}

signed main() {
	qwq;

	cin >> a >> b >> c;
	int t = __gcd(a, __gcd(b, c));
	a /= t, b /= t, c /= t;

	int q = 1;
	for (int i=1; i<=333; i++) {
		int x = a*i, y = b*i, z = c*i;
		if (x < 100 or y < 100 or z < 100 or x > 999 or y > 999 or z > 999) continue;
		f(x);
		f(y);
		f(z);

		int flag = 1;
		for (int j=1; j<=9; j++) {
			if (tt[j] != 1) {
				flag = 0;
			}
			tt[j] = 0;
		}

		if (flag) {
			q = 0;
			cout << x << " " << y << " " << z << '\n';
		}
	}
	if (q) {
		cout << "No!!!";
	}
	return 0;
}

T4

枚举数组顺序,代码框架

C++
#include <bits/stdc++.h>
#define int long long
#define qwq ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
using namespace std;

int a[5];
char c[5];

void solve() {
	// 每次进行一组测试数据的处理
	cin >> a[1] >> a[2] >> a[3] >> a[4];
	cin >> c[1] >> c[2] >> c[3];

	for (int i=1; i<=4; i++) {
		for (int j=1; j<=4; j++) {
			if (i == j) continue;  // 有重复,就跳过
			for (int k=1; k<=4; k++) {
				if (i == k or j == k) continue;  // 有重复,就跳过
				for (int x=1; x<=4; x++) {
					if (i == x or j == x or k == x) continue;  // 有重复,就跳过
					/*
					计算 a[i] c[1] a[j] c[2] a[k] c[3] a[x]
					*/
				}
			}
		}
	}
}

signed main() {
	qwq;

	int t;
	cin >> t;
	while (t--) solve();
	return 0;
}

完整代码:

C++
#include <bits/stdc++.h>
#define int long long
#define qwq ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
using namespace std;

int a[5];
char c[5];

int f(int x, int y, char op) {
	// 计算 x op y 的结果
	if (op == '+') return x+y;
	if (op == '-') return x-y;
	if (op == '*') return x*y;
	if (op == '/') return x/y;
	return 0;
}

void solve() {
	// 每次进行一组测试数据的处理
	cin >> a[1] >> a[2] >> a[3] >> a[4];
	cin >> c[1] >> c[2] >> c[3];
	int res = INT_MIN;
	for (int i=1; i<=4; i++) {
		for (int j=1; j<=4; j++) {
			if (i == j) continue;  // 有重复,就跳过
			for (int k=1; k<=4; k++) {
				if (i == k or j == k) continue;  // 有重复,就跳过
				for (int x=1; x<=4; x++) {
					if (i == x or j == x or k == x) continue;  // 有重复,就跳过
					/*
					计算 a[i] c[1] a[j] c[2] a[k] c[3] a[x]
					*/
					int sum = f(a[i], a[j], c[1]);
					sum = f(sum, a[k], c[2]);
					sum = f(sum, a[x], c[3]);
					res = max(sum, res);
				}
			}
		}
	}
	cout << res << '\n';
}

signed main() {
	qwq;

	int t;
	cin >> t;
	while (t--) solve();
	return 0;
}

二维数组上的枚举

固定大小的枚举

C++
for (int i=1; i+c-1<=n; i++) {
    for (int j=1; j+c-1<=m; j++) {
        // 算出右下角 (i+c-1, j+c-1)
        int x = i+c-1, y = j+c-1;
        // 列出了左上角是 (i, j),右下角是 (x, y) 的二维子矩形
    }
}

不固定大小的枚举

C++
for (int i=1; i<=n; i++) {
    for (int j=1; j<=m; j++) {
        // 左上角(i, j)
        for (int x=i; x<=n; x++) {
            for (int y=j; y<=m; y++) {
                // 列出了左上角是 (i, j),右下角是 (x, y) 的二维子矩形
            }
        }
    }
}

遍历子矩形

如何遍历左上角是(i, j)右下角是(x,y)的子矩形?

C++
for (int t1=i; t1<=x; t1++) {
    for (int t2=j; t2<=y; t2++) {

    }
}
原文