23级ACM实验室第一次招新测试题解

发布时间 2023-11-06 23:55:33作者: ~CHC~

A. 还是Hello World?

思路:无

代码:

c++:

#include <bits/stdc++.h>
using namespace std;
int main() {
	cin.tie(0)->ios::sync_with_stdio(0);
	cout << "Hello,World!" << endl;
	return 0;
}

B. 这题真不难,放轻松~

思路:无

代码:

C++:

#include <bits/stdc++.h>
using namespace std;
int main() {
	cin.tie(0)->ios::sync_with_stdio(0);
	int n;
	cin >> n;
	for (int i = 1; i <= n; i++) {
		int y;
		cin >> y;
		if (y % 6 == 0) {
			cout << y << endl;
			break;
		}
	}
	return 0;
}

C. 我是谁?我在哪?

思路:

两遍循环先输出奇数,再输出偶数

代码:

C++:

#include <bits/stdc++.h>
using namespace std;
const int N = 110;
int a[N];
int main() {
	cin.tie(0)->ios::sync_with_stdio(0);
	int n;
	cin >> n;
	for (int i = 1; i <= n; i++) {//输出奇数
		cin >> a[i];
		if (a[i] & 1)
			cout << a[i] << " ";
	}
	for (int i = 1; i <= n; i++)//输出偶数
		if (!(a[i] & 1))
			cout << a[i] << " ";
	return 0;
}

D. 找最大值

思路:

排个序,从后往前找第一个奇数

代码:

C++:

#include <bits/stdc++.h>
using namespace std;
const int N = 10000000;
int a[N];
int main() {
	cin.tie(0)->ios::sync_with_stdio(0);
	int n;
	cin >> n;
	for (int i = 1; i <= n; i++)
		cin >> a[i];
	sort(a + 1, a + n + 1);
	for (int i = n; i >= 1; i--)
		if (a[i] & 1) {
			cout << a[i] << endl;
			break;
		}
	return 0;
}

E. 消失的数字

思路:

2种:

  1. 排序后如果a[i]!=i就输出这个数。
  2. n*(n+1)/2减去每一个数,最后剩余的数即为答案。

代码:

C++:

#include <bits/stdc++.h>
using namespace std;
const int N = 110;
int a[N];
int main() {
	cin.tie(0)->ios::sync_with_stdio(0);
	int n;
	cin >> n;
	//1.
	for (int i = 1; i < n; i++)
		cin >> a[i];
	sort(a + 1, a + n);
	for (int i = 1; i <= n; i++)
		if (a[i] != i) {
			cout << i << endl;
			break;
		}

	//2.
//	int y = n * (n + 1) / 2;
//	for (int i = 1; i < n; i++) {
//		cin >> a[i];
//		y -= a[i];
//	}
//	cout << y << endl;
	return 0;
}

F. !!!超难!!!危险!!!

思路:

写个判断素数的函数直接一个一个判断就行

代码:

C++:

#include <bits/stdc++.h>
using namespace std;
const int N = 110;
int a[N];
bool isprime(int num) {
	if (num < 2 || num == 4)
		return 0;
	if (num == 2 || num == 3)
		return 1;
	if (num % 6 != 1 && num % 6 != 5)
		return 0 ;
	for (int i = 5; i*i <= num; i += 6) {
		if (num % i == 0 || num % (i + 2) == 0)
			return 0 ;
	}
	return 1 ;
}
int main() {
	cin.tie(0)->ios::sync_with_stdio(0);
	int n, sum = 0;
	cin >> n;
	for (int i = 1; i <= n; i++) {
		int y;
		cin >> y;
		if (isprime(y)) sum++;
	}
	cout << sum;
	return 0;
}

G. 《四月是你的谎言》

思路:

结构体的简单运用

代码:

#include <bits/stdc++.h>
using namespace std;
const int N = 110;
int a[N];
struct node {
	string s1, s2;
	int d;
} e[N];
int main() {
	cin.tie(0)->ios::sync_with_stdio(0);
	int n;
	cin >> n;
	for (int i = 1; i <= n; i++) {
		cin >> e[i].s1 >> e[i].d >> e[i].s2;
	}
	int q;
	cin >> q;
	while (q--) {
		string s;
		cin >> s;
		for (int i = 1; i <= n; i++)
			if (s == e[i].s1) {
				cout << e[i].s2 << endl;
				break;
			}
	}
	return 0;
}

H. 区间和

思路:

前缀和直接减就行

代码:

C++:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e6 + 100;
ll a[N];
int main() {
	cin.tie(0)->ios::sync_with_stdio(0);
	int t, n;
	cin >> t >> n;
	for (int i = 1; i <= n; i++)
		cin >> a[i], a[i] += a[i - 1];
	while (t--) {
		int l, r;
		cin >> l >> r;
		cout << a[r] - a[l - 1] << endl;
	}
	return 0;
}

I. 分月饼

思路:

lowbit ( n ) 定义为非负整数 n 在二进制表示下 “ 最低位的 1 及其后面的所有的 0 ” 的二进制构成的数值,运用了反码与补码的知识
因此lowbit(n)即为大小为 \(n\) 的月饼最终分成的段数
后面只需要记录没分之前的月饼再被分之后的段数的前缀和就行,然后判断所询问的段数是由最开始的哪块月饼分成的即可,然后输出那块被瓜分后的月饼的大小即可。
本题正解即为此,考察范围为简单位运算和前缀和,当然有其他方法做,可参考其他方法

代码:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5 + 10;
ll t = 1;
ll lowbit(ll x) {
    return x & (-x);
}

struct node {
    ll b, c;
} e[N];

void solve() {
    ll n;
    cin >> n;
    vector<ll>a(n + 1);

    for (ll i = 1; i <= n; i++) {
        cin >> a[i];
        e[i].b = lowbit(a[i]);//一段月饼最终会分成几段
        e[i].c = e[i - 1].c + e[i].b;//最终月饼段数的前缀和
    }

    ll q;
    cin >> q;

    while (q--) {
        ll x;
        cin >> x;

        while (t <= n) {
            if (e[t].c >= x) {
                cout << a[t] / e[t].b << endl;
                break;
            }

            t++;
        }

    }

}

int main() {
    cin.tie(0)->ios::sync_with_stdio(0);
    solve();
    return 0;
}