SDUT 2022 Spring Individual Contest(for 21) - 1

记录一下比赛中知识盲区

C Cheap kangaroo

三目运算实现辗转相除求两数最大公约数

辗转相除求最大公约数
ll gcd(ll a, ll b)
{
	return b > 0 ? gcd(b, a % b) : a;
}

ac

#include <bits/stdc++.h>
#define ll long long
#define TLE ios::sync_with_stdio(false);
using namespace std;
const int INF = 0x3f3f3f3f;
const int N = 1e5 + 10;
const double PI = 3.1415926535;
ll gcd(ll a, ll b)
{
	return b > 0 ? gcd(b, a % b) : a;
}
void solve()
{
	ll ans=INF,sum=0;
	ll n, a[N];
	cin >> n;
	for (int i = 1; i <= n; i++)
	{
		cin >> a[i];
		sum += a[i];
	}
	for (int i = 1; i <= n; i++)
	{
		ans = min(ans, gcd(a[i], a[i - 1]));
	}
	cout << sum<<" " << ans << endl;
}

int main()
{
	TLE;
	int _;
	cin >> _;
	while (_--)
	{
		solve();
	}
	return 0;
}

D Magica bamboos

奇数减奇数等于偶数,偶数减偶数等于偶数。

ac代码

#include <bits/stdc++.h>
#define ll long long
using namespace std;
#define TLE ios::sync_with_stdio(false);
const int N = 1e5 + 10;
int a[N];
//判断奇偶
bool odd(int n)
{
	if (n % 2 == 0)return false;
	else return true;
}
void solve()
{
	int n,a,ans=1;
	cin >> n;
	cin >> a;
	if (odd(a))//只有后续数字与第一个数字奇偶性相同才能保证数组中所有元素俩俩之差不出现奇数
	{
		for (int i = 2; i <= n; i++)
		{
			cin >> a;
			if (!odd(a))ans = 0;
		}
	}
	else
	{
		for (int i = 2; i <= n; i++)
		{
			cin >> a;
			if (odd(a))ans = 0;
		}
	}
	if (ans == 1)cout << "yes" << endl;
	else cout << "no" << endl;
}

int main()
{
	TLE;
	int _;
	cin >> _;
	while (_--)
	{
		solve();
	}
	return 0;
}

另外,一直判断相邻元素差即可。

#include<bits/stdc++.h>
#include <iostream>
#include<algorithm>
using namespace std;
const int N = 1e5 + 10;
int n, a[N];
int main()
{
    ios::sync_with_stdio(0);
    int t;
    cin >> t;
    while (t--) {
        cin >> n;
        for (int i = 1; i <= n; ++i)
            cin >> a[i];
        bool flag = 0;
        for (int i = 1; i < n; ++i)
        {
            int k = a[i + 1] - a[i];
            if (k % 2 !=0)
            {
                flag = 1;
                break;
            }
        }
        if (flag==0) cout << "yes" << endl;
        else cout << "no" << endl;
    }
    return 0;
}

H

//判断回文
bool rev(string s)
{
	string t;
	t = s;
	reverse(s.begin(), s.end());
	if (s == t)return true;
	else return false;
}

ac

#include <bits/stdc++.h>
#define ll long long
using namespace std;
#define TLE ios::sync_with_stdio(false);
bool pp(string s,string p)//字符串长度1e3可以写n2的暴力
{
	for (int i = 0; i < s.length(); i++)
	{
		int j;
		for ( j = 0; j < p.length(); j++)
		{
			if (s[i] == p[j])break;
		}
		if (j >= p.length())return false;
	}
	return true;
}
bool rev(string s)
{
	string t;
	t = s;
	reverse(s.begin(), s.end());
	if (s == t)return true;
	else return false;
}
int main()
{
	int n;
	cin >> n;
	string s,p="AHIMOTUVWXY";
	while (n--)
	{
		cin >> s;
		if (pp(s, p) && rev(s))cout << "yes" << endl;
		else cout << "no" << endl;
	}
	return 0;
}

M

//学一个新的unorder_map
unordered_map<string, double>mp;
mp["JD"]=1;

 ac

#include <bits/stdc++.h>
#define ll long long
#define TLE ios::sync_with_stdio(false);
using namespace std;
const int INF = 0x3f3f3f3f;
const int N = 1e5 + 10;
unordered_map<string, double>mp;
void solve()
{
	mp.clear();
	int n, m;
	string s;
	double k, ans=0;
	mp["JD"] = 1.0;
	cin >> n >> m;
	while (n--)
	{
		cin >> s >> k;
		mp[s] = k;
	}
	while (m--)
	{
		cin >> k >> s;
		ans += k * mp[s];
	}
	printf("%.6lf\n", ans);
}

int main()
{
	TLE;
	int _;
	cin >> _;
	while (_--)
	{
		solve();
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值