Codeforces Round #479 (Div. 3) ABC

比赛链接

A题 解题思路:


A题,暴力做就行,k也就 50,如果 % 10 == 0 就除,如果 = 0 ,就减,最后输出即可。


代码:


#include <map>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

const long long M = 1e9 + 7;

const int N = 300005;

typedef long long ll ;

int main(){
    int n ,k;
    scanf("%d%d",&n,&k);
    while(k != 0){
        if (n % 10 == 0){
            n /= 10;
            k --;
        }
        else{
            int t = n % 10;
            if (t >= k){
                n -= k;
                k = 0;
            }
            else{
                n -= t;
                k -= t;
            }
        }
    }
    printf("%d\n",n);
    return 0;
}


B题 解题思路:


给予一个字符串,然后求出现最多次数的2个字符,我们从头遍历就好,然后我们存储相邻的字符(2个字符),然后我们存入map中,最终我们遍历map,在其中求出最大值。处理好最后的细节就OK(for 循环中注意下在最后时break即可)


代码:


#include <map>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

const long long M = 1e9 + 7;

const int N = 300;

typedef long long ll ;

map <string,int> ans;

int main(){
    int n;
    scanf("%d",&n);
    string st;
    cin>>st;
    for (int i = 0; i < n ; i ++){
        string s = "";
        if (i == n - 1) break; // 这里记得break,不然最后i + 2 会溢出
        for (int j = i ; j < i + 2; j ++){
            s += st[j];
        }
        ans[s] += 1;
    }
    int mx = -1;
    string ss ;
    for (map <string,int> ::iterator it = ans.begin();it != ans.end();it++){
        if (it->second > mx){
            ss = it->first;
            mx = it->second;
        }
    }
    cout<<ss<<endl;
    return 0;
}


C题 解题思路:


C题水题,不过细节要注意下,求 一个 x,让他大于等于数组中的 k 个数。

  1. 注意 k 为 0 的时候,如果数组里的最小值为 1,那么肯定不存在(x的取值范围是1 — 1e9)
  2. 排序后我们判断 ans[k - 1] 和 ans[ k ] 是否相同,如果相同,那么肯定不能让他们小于等于x 的个数是 k 个。


代码:


#include <map>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

const long long M = 1e9 + 7;

const int N = 300005;

typedef long long ll ;

int ans[N];

int main(){
    int n,k;
    scanf("%d%d",&n,&k);
    for (int i = 0 ; i < n; i++){
        scanf("%d",&ans[i]);
    }
    sort(ans,ans + n);
    if (k == 0){
        if (ans[0] == 1){
            printf("-1");
        }
        else{
            printf("1");
        }
        return 0;
    }
    if (k == n){
        printf("%d",ans[n-1]);
        return 0;
    }
    if (ans[k] == ans[k - 1]){
        printf("-1");
    }
    else{
        printf("%d",ans[k -1]);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值