作为复试机试前的练习,题目来自cspro.org
仅包含历年1、2题,比较暴力所以可能超时。
2024-3 货物调度
#include <iostream>
#include <vector>
#include <map>
#include <string>
#include <climits>
using namespace std;
struct item{
int a,b;
}item[1010];
struct repository{
int a,b;
}repository[1010];
int main() {
int n,m,v;
cin >> n >> m >> v;
for(int i = 0; i < n; i++) {
cin >> repository[i].a >> repository[i].b;
}
for(int i = 0; i < m; i++) {
cin >> item[i].a >> item[i].b;
}
int mincost = INT_MAX;
for(int mask = 0; mask < (1 << m); mask++) {
int cost = 0;
int weight = 0;
vector<int> c(n);
for(int i = 0; i < m; i++) {
if(mask & (1 << i)) {
c[item[i].b]++;
weight += item[i].a;
}
}
for(int j = 0; j < n; j++){
if(c[j] > 0){
cost += repository[j].a;
cost += c[j] * repository[j].b;
}
}
if(weight - cost >= v && cost < mincost){
mincost = cost;
}
}
cout << mincost;
return 0;
}
C++2024-3 相似度计算
#include <unordered_set>
int main() {
// ... existing code ...
unordered_set<string> set1, set2;
// Input for set1
for(int i = 0; i < n; i++) {
cin >> s;
Atoa(s);
set1.insert(s);
}
// Input for set2
for(int i = 0; i < m; i++) {
cin >> s;
Atoa(s);
set2.insert(s);
}
// Count common strings
int result = 0;
for(const auto& str : set1) {
if(set2.count(str) > 0) {
result++;
}
}
cout << result << endl;
// Count total unique strings
cout << set1.size() + set2.size() - result << endl;
return 0;
}
C++202312-1 仓库规划
#include <iostream>
#include <vector>
using namespace std;
// 检查仓库a是否可以成为仓库b的上级
bool isSuperior(const vector<int>& a, const vector<int>& b) {
for (int i = 0; i < a.size(); ++i) {
if (a[i] <= b[i]) return false;
}
return true;
}
int main() {
int n, m;
cin >> n >> m; // 读入仓库数量和位置编码的维度
vector<vector<int> > codes(n, vector<int>(m));
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
cin >> codes[i][j]; // 读入每个仓库的位置编码
}
}
for (int i = 0; i < n; ++i) {
int superior = -1; // 初始化上级仓库编号为-1,表示没有上级
for (int j = 0; j < n; ++j) {
if (i != j && isSuperior(codes[j], codes[i])) {
if (superior == -1 || j < superior) {
superior = j; // 找到编号最小的满足条件的上级仓库
}
}
}
cout << (superior + 1) << endl;
}
return 0;
}
C++202312-2 因子化简
#include <iostream>
#include <cmath>
using namespace std;
bool isPrime(int n) {
if (n <= 1) return false;
for (int i = 2; i <= sqrt(n); i++) {
if (n % i == 0) return false;
}
return true;
}
// 函数用于简化n的值
long long simplifyNumber(int n, int k) {
long long result = 1;
for (int p = 2; p <= n; p++) {
if (isPrime(p)) {
int count = 0;
while (n % p == 0) {
n /= p;
count++;
}
if (count >= k) {
result *= pow(p, count);
}
}
}
return result;
}
int main() {
int q;
cin >> q;
while (q--) {
int n, k;
cin >> n >> k;
cout << simplifyNumber(n, k) << endl;
}
return 0;
}
C++202309-1 坐标变换一
#include <iostream>
#include <vector>
using namespace std;
int main(){
int n,m,a,b;
cin >> n >> m;
int left = 0, right = 0;
for(int i = 0;i < n;i++){
cin >> a >> b;
left += a;
right += b;
}
for(int i = 0;i < m;i++){
cin >> a >> b;
cout << a + left <<' '<< b + right << endl;
}
return 0;
}
C++202309-2 坐标变换2
#include <iostream>
#include <vector>
#include <cmath>
#include <iomanip>
using namespace std;
int main(){
double n,m,a,b,x,y;
cin >> n >> m;
vector<vector<double> > operations(n,vector<double>(2));
for(int i = 0;i < n;i++){
cin >> operations[i][0] >> operations[i][1];
}
for(int j = 0;j < m;j++){
cin >> a >> b >> x >> y;
while(b >= a){
if(operations[a-1][0] == 1){
x = x * operations[a-1][1];
y = y * operations[a-1][1];
a++;
}else{
double temp = x;
double z = operations[a-1][1];
x = x * cos(z) - y * sin(z);
y = temp * sin(z) + y * cos(z);
a++;
}
}
cout << fixed << setprecision(3) << x << ' ' << y << endl;
}
return 0;
}
C++202305-1 重复局面
#include <iostream>
#include <vector>
#include <string>
using namespace std;
bool issimilar(const vector<string>& x, const vector<string>& y) {
for (int i = 0; i < 8; i++) {
if (x[i] != y[i]) {
return false;
}
}
return true;
}
int main() {
int n;
cin >> n;
vector<vector<string>> situations; // 存储所有局面
for (int i = 0; i < n; i++) {
vector<string> board(8);
for (int j = 0; j < 8; j++) {
cin >> board[j];
}
situations.push_back(board);
int count = 0;
for (int k = 0; k <= i; k++) {
if (issimilar(situations[i], situations[k])) {
count++;
}
}
cout << count << endl;
}
return 0;
}
C++202305-2 矩阵运算
#include <iostream>
#include <vector>
#include <cmath>
#include <iomanip>
using namespace std;
vector<vector<int> > matrixMultiply(const vector<vector<int> >& A, const vector<vector<int> >& B) {
int rowsA = A.size(); // A的行数
int colsA = A[0].size(); // A的列数
int rowsB = B.size(); // B的行数
int colsB = B[0].size(); // B的列数
// 结果矩阵初始化为0,大小为A的行数 x B的列数
vector<vector<int> > C(rowsA, vector<int>(colsB, 0));
// 执行矩阵乘法
for(int i = 0; i < rowsA; ++i) {
for(int j = 0; j < colsB; ++j) {
for(int k = 0; k < colsA; ++k) {
C[i][j] += A[i][k] * B[k][j];
}
}
}
return C;
}
int main(){
int n, d, x;
cin >> n >> d;
vector<vector<int> > q(n, vector<int>(d));
vector<vector<int> > k(d, vector<int>(n));
vector<vector<int> > v(n, vector<int>(d));
for(int i = 0; i < n; i++){
for(int j = 0; j < d; j++){
cin >> x;
q[i][j] = x;
}
}
for(int i = 0; i < n; i++){
for(int j = 0; j < d; j++){
cin >> x;
k[j][i] = x;
}
}
for(int i = 0; i < n; i++){
for(int j = 0; j < d; j++){
cin >> x;
v[i][j] = x;
}
}
vector<int> w(n);
for(int i = 0;i < n;i++){
cin >> x;
w[i] = x;
}
vector<vector<int> > temp = matrixMultiply(q, k);
for(int i = 0;i < n;i++){
for(int j = 0;j < n;j++){
temp[i][j] = temp[i][j] * w[i];
}
}
vector<vector<int> > result = matrixMultiply(temp, v);
for(int i = 0; i < n; i++){
for(int j = 0; j < d; j++){
cout << result[i][j] << " ";
}
cout << endl;
}
return 0;
}
C++202303-1 田地丈量
#include <iostream>
#include <vector>
#include <cmath>
#include <iomanip>
using namespace std;
int area(int a,int b,int c,int d,int x,int y){
int x1 = max(a,0);
int y1 = max(b,0);
int x2 = min(c,x);
int y2 = min(d,y);
int height = y2 - y1;
int width = x2 - x1;
if(height < 0 || width < 0){
return 0;
}
return height * width;
}
int main(){
int n,x,y,a,b,c,d,r = 0;
cin >> n >> x >> y;
for(int i = 0;i < n;i++){
cin >> a >> b >> c >> d;
r += area(a,b,c,d,x,y);
}
cout << r << endl;
}
C++202303-2 耕田计划
#include <iostream>
#include <vector>
#include <cmath>
#include <iomanip>
using namespace std;
int main(){
int n,m,k,maxval=0,max,min=INT_MAX;
cin >> n >> m >> k;
vector<vector<int> > area(n,vector<int>(2));
for(int i = 0;i < n;i++){
cin >> area[i][0] >> area[i][1];
}
while(m >= 0){
for(int i = 0;i < n;i++){
if(area[i][0] > maxval){
maxval = area[i][0];
max = i;
min = area[i][1];
}
}
if(area[max][0] > k && m >= min){
m = m - min;
area[max][0]--;
}else{
cout << area[max][0] << endl;
return 0;
}
maxval=0;
min=INT_MAX;
}
return 0;
}
C++202212-1 现值计算
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
int main() {
double n,y,r = 0;
cin >> n >> y;
vector<double> m(n + 1);
for(int i = 0;i <= n;i++){
cin >> m[i];
}
r += m[0];
for(int i = 1;i <= n;i++){
r += pow ((1 + y), -i) * m[i];
}
cout << r << endl;
return 0;
}
C++202212-2 训练计划
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
int main() {
int n,m;
bool success = true;
cin >> m >> n;
vector<int> former(n + 1),days(n + 1),early(n + 1),late(n + 1) ;
for(int i = 1;i <= n; i++){
cin >> former[i];
}
for(int i = 1;i <= n; i++){
cin >> days[i];
}
for(int i = 1;i <= n; i++){
int flag = former[i], count = 1;
while(flag != 0){
count = count + days[flag];
flag = former[flag];
}
early[i] = count;
}
for(int i = 1;i <= n; i++){
if(early[i] + days[i] - 1 > m){
success = false;
break;
}
}
for (int i = 1;i <= n; i++){
cout << early[i] << ' ';
}
if(!success){
return 0;
}
for (int i = 1;i <= n; i++){
late[i] = m + 1 - days[i];
int flag = former[i];
int count = late[i];
while(flag != 0){
count = count - days[flag];
if(late[flag] > count){
late[flag] = count;
}
flag = former[flag];
}
}
cout << endl;
for (int i = 1;i <= n; i++){
cout <<late[i] << ' ';
}
return 0;
}
C++202209-1 如此编码
#include <iostream>
#include <vector>
#include <climits>
using namespace std;
int main() {
int n,m,r;
cin >> n >> m;
vector<int> A(n),C(n),B(n);
for(int i = 1;i <= n;i++){
cin >> A[i];
}
C[0] = 1;
for(int i = 1;i <= n;i++){
C[i] = C[i - 1] * A[i];
}
for(int i = 1;i <= n; i++){
if (i == 1){
B[i] = ( m % C[i] ) / C[i - 1];
}else{
B[i] = ( m % C[i] - m % C[i - 1]) / C[i - 1];
}
}
for(int i = 1;i <= n;i++){
cout << B[i] << ' ';
}
return 0;
}
C++202209-2 何以包邮
#include <iostream>
#include <vector>
#include <climits>
using namespace std;
int main() {
int n, x;
cin >> n >> x;
vector<int> a(n);
for(int i = 0; i < n; i++) {
cin >> a[i];
}
int minSum = INT_MAX;
for(int mask = 0; mask < (1 << n); mask++) {
int sum = 0;
for(int i = 0; i < n; i++) {
if(mask & (1 << i)) {
sum += a[i];
}
}
if(sum >= x && sum < minSum) {
minSum = sum;
}
}
if(minSum == INT_MAX) {
cout << x << endl;
} else {
cout << minSum << endl;
}
return 0;
}
C++