Codeforces Round 928 (Div. 4) 题解 (A-C)

A. Vlad and the Best of Five

标签:实现问题,编程技巧,模拟

问题:一个长度为5的字符串,由字母A和B组成,输出出现频率最高的字符

思路:依次遍历求出A和B分别出现的次数,进行比较再输出

#include <bits/stdc++.h>

using namespace std;

int main() {
    ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    int t;cin>>t;
    while(t--) {
        string s;cin>>s;
        int a[2]={0};
        for(int i=0;i<s.size();i++) {
            if(s[i]=='A') a[0]++;
            else a[1]++;
        }
        cout<<(a[0]>a[1] ? "A\n" : "B\n");
    }
    return 0;
}

 B. Vlad and Shapes

标签:实现问题,编程技巧,模拟

问题:判断网格中所有1形成的是不是一个正方形,如果是输出"SQUARE",如果不是输出"TRIANGLE"

思路:先记录每一行1出现的个数为ans,进行比较求出最长边的个数为Max,并记录出网格中存在1的个数为sum,如果Max的平方等于sum,输出"SQUARE",否则输出"TRIANGLE"

#include <bits/stdc++.h>

using namespace std;
const int N=10+10;
char a[N][N];
int main() {
    ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    int t;cin>>t;
    while(t--) {
        int n;cin>>n;
        int sum=0,Max=0;
        for(int i=0;i<n;i++) {
            int ans=0;
            for(int j=0;j<n;j++) {
                cin>>a[i][j];
                if(a[i][j]=='1') sum++,ans++;
            }
            Max=max(Max,ans);
        }
        cout<<(Max*Max==sum ? "SQUARE\n" : "TRIANGLE\n");
    }
    return 0;
}

C. Vlad and a Sum of Sum of Digits

标签:动态规划,实现问题,编程技巧,模拟

问题:将1-n的数,替换为其数位之和,求出其数之和

样例解释:1-12:1,2,3,4,5,6,7,8,9,10,11,12\rightarrow1,2,3,4,5,6,7,8,9,1,2,3

                  1+2+3+4+5+6+7+8+9+1+2+3=51

思路:需要预处理个前缀和,a[i]=a[i-1]+temp(i)

           temp(i)用于求 i 的数位之和

           n<=2e5,令N=2e5+10

           则先求出1-N中的情况,这样需要 n 直接在a[N],输出a[n]的值即可

#include <bits/stdc++.h>

using namespace std;
const int N=2e5+10;
int a[N];

int temp(int x) {
    int ans=0;
    while(x) {
        ans+=x%10;
        x/=10;
    }
    return ans;
}

void solve() {
    int n;cin>>n;
    cout<<a[n]<<'\n';
}

int main() {
    ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    for(int i=1;i<N;i++) a[i]=a[i-1]+temp(i);
    int t;cin>>t;
    while(t--) solve();
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

July.19th

感谢各位对我的支持

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值