首页 > 其他分享 >23级ACM实验室第一次招新测试题解

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

时间:2023-11-06 23:55:05浏览次数:41  
标签:招新 23 int ll cin ACM sync using include

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;
}

标签:招新,23,int,ll,cin,ACM,sync,using,include
From: https://www.cnblogs.com/wsccz/p/23ACM_test_001.html

相关文章

  • 2023.11.06 sh僵尸进程
    //简介:系统top显示中很多zombie僵尸进程,使系统进程数量已达到最大值35567。/查看sh子进程父进程全为基站产品的oam_2160二进程程序产生的(其原因为异常情况下,未正常处理系统调用:合理修改了pclose()调用)//参考文献https://blog.csdn.net/TiktokLiveTool/article/details/13211514......
  • 2023年11第一周博客总结
    本周进行了软考的最后冲刺,每天都很累,所以没怎么写博客,昨天刚考完,今天进行一下本周的周结,在最后一周的时间内,每天按照一道大题的进度攻克四道大题,最后也成功攻克下来了,对于算法题,采取的自求多福的策略,反正考试的时候都写上了,而且感觉应该能拿56分,对于其他题,有两题奔着满分,对于客观......
  • 20231101
    //abide,appendix,consequential,consignee,constraint,contractor,defendant,domicile,equity,injury,prior,quotas,reside,specify,subsidy,thereafter,unveiled,incompliancewith,landingcharges,netweightabide-遵守Toabidemeanstocomplyw......
  • 20231103
    //arbitrage,audit,authentic,bottleneck,circulate,confidentiality,crux,essential,flop,goodwill,inconsistent,inner,insurance,integral,marine,moisture,outer,sturdy,supplementary,trace,transit,unmentioned,fringebenefitsarbitrage-套利......
  • 20231102
    //annex,array,bolster,contain,dumping,foothold,franchisee,hard-core,imply,occurrence,overhaul,parley,prepaid,prevail,rough,specification,stake,sublet,weightyannex-附件Anannexreferstoanadditionalorsupplementarypartordocument......
  • 20231105
    //calculate,collection,currency,facilitate,installment,irrevocable,leeway,minimum,remittance,sightcredit,sightdraftcalculate-计算Calculatemeanstodetermineorestimateanumericalvalueorresultthroughmathematicalorlogicalprocesses.......
  • 20231104
    //bailment,bear,breakage,concealment,coverage,demurrage,depreciation,excessive,hazard,hyperinflation,long-distance,perk,premium,replacement,suitable,surcharge,vacant,additionalrisk,invoicevaluebailment-财产寄托Bailmentreferstothea......
  • 20231106
    //circumstance,consult,customary,deposit,explain,reluctant,remit,settle,universally,beshortof,cashondelivery,creditnote,creditperiod,debitnote,deferredpayment,downpayment,fullpayment,multiplepayment,openingbankcircumstance......
  • 20231107
    //alteration,amend,consent,difference,hesitate,incur,include,insert,modification,replace,sentence,severe,stipulation,bringup,pointoutalteration-改动Alterationreferstotheactofmakingchangesormodificationstosomething.Itindicat......
  • 20231106学习总结ini文件读写
    INI(Initialization)文件是一种常见的配置文件格式,通常用于存储应用程序的配置信息。INI文件通常包含一系列的节(sections)和键-值对(key-valuepairs)。每个节包含一组相关的键-值对。;这是注释[Database]Server=localhostPort=3306Username=myuserPassword=mypassword......