2021年度训练联盟热身训练赛第三场

A.Circuit Math

AC代码:

#include <cstring>
#include <string.h>
#include <stack>
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
stack<int>st;
char zz[30];
int main()
{
    
    
	int n;
	cin >> n;
	for (int i = 0; i < n; i++)
	{
    
    
		cin >> zz[i];
	}
	char s[255];
	int ss=0;
	char c;
	while(cin>>c)
	{
    
    
		s[ss]=c;
		//cout<<"c"<<c<<'\n';
		//cout<<"s"<<s[ss]<<'\n';
		ss++;
	}
	//cout<<"ss"<<ss<<'\n';
	int len=ss;
	//cout<<len<<'\n';
	int num = 0;
	while (num < len)
	{
    
    
		if (s[num] >= 'A'&&s[num] <= 'Z')
		{
    
    
			//cout<<zz[s[num] - 'A']<<'\n';
			if (zz[s[num] - 'A'] == 'T')
				st.push(1);
			else st.push(0);
			num++;
		}
		else if (s[num] == '*')
		{
    
    
			int a = st.top();
			st.pop();
			int b = st.top();
			st.pop();
			if (a == 1 && b == 1)
				st.push(1);
			else st.push(0);
			num++;
		}
		else if (s[num] == '+')
		{
    
    
			int a = st.top();
			st.pop();
			int b = st.top();
			st.pop();
			if (a == 1 || b == 1)
				st.push(1);
			else st.push(0);
			num++;
		}
		else if (s[num] == '-')
		{
    
    
			int a = st.top();
			st.pop();
			if (a == 1)
				st.push(0);
			else st.push(1);
			num++;
		}
	}
	int xx = st.top();
	if (xx == 1)cout << "T" << '\n';
	else cout << "F" << '\n';
	return 0;
}

B.Diagonal Cut

一看就是和gcd有关。
然后用gcd写发现样例过了但是WA了。。
一会才知道原来还要看一个数%它和另一个数的gcd之后的数是奇数还是偶数。
都是奇数才有答案,有一个是偶数就是0,没有被平均分成两份的。

AC代码:

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

//gcd模板
ll gcd(ll a, ll b){
    
    
	return b == 0 ? a : gcd(b, a % b);
} 

ll m, n, ans;

int main(){
    
    
    cin >> m >> n;
    ans = gcd(a, b);
    m /= ans;
    n /= ans;
    if((m % 2 == 1) && (n % 2 == 1)){
    
    
        cout << ans << endl;
    }
	else{
    
    
        cout << 0 << endl;
    }
    return 0;
}

C.Gerrymandering

AC代码:

#include <cstring>
#include <string>
#include <set>
#include <iostream>
#include <cstdio>
//#include<unordered_set>
#include <algorithm>
using namespace std;
long long zz1[1010];
long long zz2[1010];
int main()
{
    
    
	int p, d;
	cin >> p >> d;
	long long sum1 = 0;
	long long sum2 = 0;
	long long sum3 = 0;
	while (p--)
	{
    
    
		int diq, a, b;
		cin >> diq >> a >> b;
		zz1[diq] += a;
		zz2[diq] += b;
		sum1 += a;
		sum1 += b;
	}
	for (int i = 1; i <= d; i++)
	{
    
    
		long long ans = (zz1[i] + zz2[i]) / 2 + 1;
		//cout<<ans<<'\n';
		if (zz1[i] > zz2[i])
		{
    
    
			cout << "A" << " ";
			long long x1 = zz1[i] - ans;
			cout << x1 << " " <<zz2[i] << '\n';
			sum2+= x1;
			sum3 +=zz2[i];
		}	
		else 
		{
    
    
			cout<<"B"<<" ";
			long long x2 = zz2[i] - ans;
			cout << zz1[i]<< " " <<x2  << '\n';
			sum3+= x2;
			sum2+=zz1[i];
		}
	}
	
	double xx=sum2-sum3;
	if(xx<0)xx=-xx;
	
	double yy = xx * 1.0 / sum1;
	printf("%.10lf\n", yy);
	return 0;
}

D.Missing Numbers

用f表示一个新的顺序对的数。(0,1,2,3,……这种)数组sr记录小盆友写的数。ff表示遍历到第几个数了。如果sr【ff】 = f则说明小盆友写的顺序是对的。如果sr【ff】!= f则说明小盆友写的顺序不对,需要输出中间落下的数,直到sr【ff】再次等于f为止。如果全部相等说明小盆友输入的都是对的,则输出good job夸奖一下小盆友~
AC代码:

#include <bits/stdc++.h>
using namespace std;
const int N = 1100 + 7;

int n, sr[N];
bool flag;

int main(){
    
    
	cin >> n;
	for (int i = 1; i <= n; ++i){
    
    
		cin >> sr[i];
	}
	int f = 1;
	flag = 0;
	int sum = 0;
	for (int ff = 1; ff <= n;){
    
    
		if(sr[ff] == f){
    
    
			sum ++;
			f++;
			ff++;
		}
		else{
    
    
			cout << f << endl;
			flag = 1;
			f++;
		}
	}
	if(!flag) cout << "good job" << endl;
	return 0;
}

K.Summer Trip

暴力出奇迹!
遍历,用map记录每个字母之前是否出现过来优化时间。
memset的时间复杂度是O(n)不是O(1)。
AC代码:

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

int vis[100000+5] ;

int main(){
    
    
	std::ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	string s;
	cin >> s;
	int l = s.size();
	ll sum = 0;
	map<char, int>mp;
	for(int i = 0; i < l; ++i){
    
    
	    int k = 1;
	    for(int j = i + 1; j < l && k <= 26; ++j){
    
    
	    	if(s[j] == s[i]) break;
            if(mp[s[j]] == 0){
    
    
            	mp[s[j]] = 1;
            	sum++;
            	k++;
			}
		}
		mp.clear();
	}
	cout << sum << endl;
	return 0 ;
}

M.Zipline

物理问题。晾衣架原理。
图片竟然上传不了!!气死了www~
AC代码:

#include <bits/stdc++.h>
using namespace std;

double n, w, g, h, r;
double mmin, mmax;

int main(){
    
    
	cin >> n;
	while(n--){
    
    
		cin >> w >> g >> h >> r;
		if(g == h) {
    
    
			mmin = w;
			mmax = sqrt((w*w/4) + (h-r)*(h-r)) * 2;
		}
		else if(g != h){
    
    
			double a = g + h - 2*r;
			double b = w*(g-r);
			double c = w*(h-r);
			mmin = sqrt(w*w + (h-g)*(h-g));
			mmax = sqrt((g-r) * (g-r) + ((b*b) / (a*a))) + sqrt((h-r)*(h-r) + ((c*c) / (a*a)));
		}
		printf("%.8lf %.8lf\n", mmin, mmax);
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/LXC_007/article/details/115252703