牛客寒假算法基础集训营5

牛客寒假算法基础集训营5

题目链接:https://ac.nowcoder.com/acm/contest/3006#question

A 模板

题意:给出两个字符串通过三种操作,将以字符串转变为另一个,求出最少的操作数。

三种操作为:

  • 将其中任意一个字母替换为另一个
  • 把最后一个字母删除
  • 在尾部添加一个字母

思路:由于操作中增删字母的操作只在尾部进行,将两个字符串从头开始遍历比较,记录不同字符的个数再加上两个字符串长度的差值即可。

代码:

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

typedef long long ll;
const long long mod = 1e9 + 7;
const int INF = 0x3f3f3f3f;

int main()
{
    int n, m;
    string s1, s2;
    cin >> n >> m;
    cin >> s1 >> s2;
    int ans;
    if(m > n){
        ans = m - n; //得到差值
        for(int i = 0; i < n; i++){ //两个字符串不同字符的个数
            if(s1[i] != s2[i]) ans++;
        }
    }
    else{
        ans = n - m;
        for(int i = 0; i < m; i++){
            if(s1[i] != s2[i]) ans++;
        }
    }
    cout << ans << endl;
    return 0;
}

E Enjoy the game

初始一共有n张卡牌

先手第一步最少要拿1张牌,最多要拿n−1张牌。

接下来每一步,双方最少要拿1张牌,最多拿等同于上一步对方拿的牌数的牌。

拿走最后一张牌的人将取得游戏的胜利。

当牌数为 2 n 2^n 的时候,,后手必胜,否则先手必胜

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

typedef long long ll;
const long long mod = 1e9 + 7;
const int INF = 0x3f3f3f3f;
const double PI = 3.141592;

int main()
{
    ll n;
    cin >> n;
    int flag = 0;
    while(n != 1){
        if(n % 2 == 0) n /= 2;
        else{
            flag = 1;
            break;
        }
    }
    if(flag == 0) cout << "Alice" << endl;
    else cout << "Bob" << endl;
    return 0;
}

I I题是个签到题

题意:签到题。判断I题过题数是否大于等于80%,或者排过序以后是否在前三位即可。

代码:

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

typedef long long ll;
const long long mod = 1e9 + 7;
const int INF = 0x3f3f3f3f;

typedef struct{int num, val;} test;

bool mycmp(test t1, test t2){
    if(t1.val >= t2.val) return 1;
    return 0;
}

int main()
{
    int n, m;
    test arr[15];
    cin >> n >> m;
    for(int i = 0; i < n; i++){
        cin >> arr[i].val;
        arr[i].num = i + 1;
    }
    int num9 = arr[8].val;
    if(num9 >= m * 0.8) cout << "Yes" << endl;
    else{
        sort(arr, arr + n, mycmp);
        if(arr[2].val <= num9) cout << "Yes" << endl;
        else cout << "No" << endl;
    }
    return 0;
}

J 牛牛战队的秀场

题意:给出圆的半径r,画出圆的内接正n边形,随便选取正nn边形的一个顶点为1号顶点,按顺时针的顺序把其他的点叫做2号顶点,3号顶点……求i号顶点和j号顶点间的最短距离。

思路:数学题,推公式

每条边对应的圆心角为 Π 2 / n Π*2/n ,半径为r,边长为 2 r s i n ( Π / n ) 2*r*sin(Π/n)

i号顶点到j号顶点的最少边数为: m i n ( i j + n m o d n , ( j i + n ) m o d n ) min((i-j+n)mod n, (j-i+n)modn)

代码:

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

typedef long long ll;
const long long mod = 1e9 + 7;
const int INF = 0x3f3f3f3f;
const double PI = 3.141592;
int main()
{
    int n;
    double r;
    int i, j;
    cin >> n >> r;
    cin >> i >> j;
    double dis = min((i - j + n) % n, (j - i + n) % n);
    double temp = PI / n;
    double ans = sin(temp) * 2.0 * r * dis;
    printf("%.6f\n", ans);
    return 0;
}

发布了31 篇原创文章 · 获赞 5 · 访问量 2977

猜你喜欢

转载自blog.csdn.net/weixin_43763903/article/details/105276507