Hottest 30 of codeforce

Posted riotian

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Hottest 30 of codeforce相关的知识,希望对你有一定的参考价值。

1. 4A.Watermelon

题目链接:https // s.com/problemset/problem/4/A

题意:两人分瓜,但每一部分都得是偶数

分析:直接 对2取余,且 w != 2

#include<bits/stdc++.h>
using namespace std;
int main() {
    int n;
    cin >> n;
    if (n % 2 == 0 && n != 2)cout << "YES
";
    else cout << "NO" << endl;
    return 0;
}

2. 1A.Theatre Square

题目链接:https // s.com/problemset/problem/1/A

题意:铺地板,给定边长为a的正方形砖块。允许铺的面积大于广场面积

分析:注意数据很大 用__int64 存储,向上取整$ (n + a - 1) / a $

#include <iostream>
using namespace std;
int main() {
    __int64 a, n, m;
    cin >> n >> m >> a;
    cout << ((n + a - 1) / a) * ((m + a - 1) / a) << endl;
    return 0;
}

3. 71A.Way Too Long Words

题目链接:https // s.com/problemset/problem/71/A

题意:如果字符串长度大于10则需要压缩长度

分析:水题

#include<bits/stdc++.h>
using namespace std;
int main() {
    string s;
    int n;
    cin >> n;
    while (n--) {
        cin >> s;
        int len = s.length();
        if (len > 10) cout << s[0] << len - 2 << s[len - 1] << endl;
        else cout << s << endl;
    }
    return 0;
}

4. 158A.Next Round

题目链接:https // s.com/problemset/problem/158/A

题目描述:题意很简单,要求按照大小顺序输入n个数据并判断大于第k个数据的非零数字个数

#include<bits/stdc++.h>
using namespace std;
int main() {
    int n, k, c = 0, t, a[51]; 
    cin >> n >> k;
    for (int i = 0; i < n; i++)
        cin >> a[i];
    for (int i = 0; i < n; i++)
        if (a[i] >= a[k - 1] && a[i] > 0)
            c++;
    cout << c << ‘
‘;
    return 0;
}

5. 231A. Team

题目链接:https // s.com/problemset/problem/231/A

#include<bits/stdc++.h>
using namespace std;
int main() {
    int n, a, b, c, cnt = 0;
    for (scanf("%d", &n); n; --n) {
        scanf("%d %d %d", &a, &b, &c); if (a + b + c >= 2)++cnt;
    }
    cout << cnt << endl;
    return 0;
}

6. 118A.String Task

题目链接:https // s.com/problemset/problem/118/A

题目描述:输入一串字符,如果英文字母是大写则转换为小写,是元音则去掉,是辅音则在前加“.”,输出处理后的字母。

分析:先判断字符串种是否为英文字母,然后将大写转换为小写,利用第二个字符串,是元音则跳过,是辅音则先加“.”再加上辅音字母,最后输出处理后的字符串。

#include<bits/stdc++.h>
using namespace std;
int main() {
    char s;
    while (cin >> s) {
        s = tolower(s);
        if (!(s == ‘e‘ || s == ‘a‘ || s == ‘y‘ || s == ‘u‘ || s == ‘o‘ || s == ‘i‘)) { cout << "." << s; }
    }
    cout << endl;
    return 0;
}

7. 50A.Domino piling

题目链接:https // s.com/problemset/problem/50/A

#include<bits/stdc++.h>
using namespace std;
int n, m;
int main() {
    cin >> n >> m;
    cout << ((n * m) >> 1) << endl;
    return 0;
}

8. 282A.Bit++

题目链接:https // s.com/problemset/problem/282/A

#include <iostream>
#include <string>
#define LL long long
using namespace std;
int main()
{
    string str;
    LL x = 0 , n, a;
    cin >> n;
    for (a = 1; a <= n; a++)
    {
        cin >> str;
        if (str == "++X" || str == "X++")
            x++;
        else x--;
    }
    cout << x;
    return 0;

}

9. 112A.Petya and Strings

题目链接:https // s.com/problemset/problem/112/A

分析:比较字符串,两个字符串都变为小写,然后通过str自带的字典序比较输出结果

#include<bits/stdc++.h>
using namespace std;
string s1, s2;
int main() {
    cin >> s1 >> s2;
    for (auto& p    s1)p = tolower(p);
    for (auto& p    s2)p = tolower(p);
    if (s1 > s2)cout << 1;
    else if (s1 == s2)cout << 0;
    else cout << -1;
    return 0;
}

10. 263A.Beautiful Matrix

题目链接:https // s.com/problemset/problem/263/A

分析:找到唯一的1,计算坐标到中心的距离即 $ abs(i - 3) + abs(j - 3) $

#include<bits/stdc++.h>
using namespace std;
int main(){
    int ans, a;
    for (int i = 1; i <= 5; i++)
        for (int j = 1; j <= 5; j++){
            cin >> a;
            if (a == 1) { ans = abs(i - 3) + abs(j - 3); break; }
        }
    cout << ans;
    return 0;
}

11 339A - Helpful Maths

#include<bits/stdc++.h>
using namespace std;
int main() {
    string s;
    cin >> s;
    sort(s.begin(), s.end());
    int len = s.length();
    if (len == 1)cout << s;
    else {
        for (int i = len / 2; i < len; ++i)
            if (i != s.length() - 1)cout << s[i] << "+";
            else cout << s[i];
    }
    return 0;
}

看dalao题解的时候发现了一个cin函数 - cin.ignore() [讲解](https //blog.csdn.net/qq_36274515/article/details/77714152)

#include <bits/stdc++.h>
using namespace std;
int main(){
	int a[100],i=0,j;
	while(cin>>a[i++])cin.ignore(1);//读入数字后ignore +
	sort(a,a+--i);
	for(j=0;j<i;j++){cout<<a[j];if(j<i-1)cout<<‘+‘;}
    return 0;
}

12. 281A - Word Capitalization

#include<bits/stdc++.h>
using namespace std;
int main() {
    string s;
    cin >> s;
    s[0] = toupper(s[0]);
    cout << s;
    return 0;
}

13. 96A - Football

字符串查找:find函数,这里有个技巧,find返回值为 size_type 如果在字符串中未找到即返回npos4294967295)== -1 ,而找到了就会返回下标。

#include<bits/stdc++.h>
using namespace std;
int main() {
    string s1 = "1111111", s2 = "0000000", s;
    cin >> s;
    //令返回值 +1 , -1 -> 0 or 下标变为正数 再配合 || 即可判断
    printf("%s", s.find(s1) + 1 || s.find(s2) + 1 ? "YES"    "NO");
    return 0;
}
//当然正常写法也是可以的利用for循环

14. 266A - Stones on the Table

#include <bits/stdc++.h>
using namespace std;
int main() {
    char a, b; int c = 0,t;
    cin >> t >> a;    //不断读入并比较
    while (t--) { b = getchar(); a == b ? c++ : a = b; }
    cout << c << endl;
}

15. 236A - Boy or Girl

#include<bits/stdc++.h>
using namespace std;
int main() {
    string s; set<char>_set;
    cin >> s;
    for (auto c : s)_set.insert(c);
    if (_set.size() % 2 == 0)cout << "CHAT WITH HER!";
    else cout << "IGNORE HIM!";
    return 0;
}

优化

#include<bits/stdc++.h>
using namespace std;
int main() {
    char ch; string s;
    while (cin >> ch) if (s.find(ch) == -1) s += ch;
    if (s.size() % 2 == 0) cout << "CHAT WITH HER!";
    else cout << "IGNORE HIM!";
}

16. 69A - Young Physicist

#include<bits/stdc++.h>
using namespace std;
int n, a, b, c, x, y, z;
int main() {
    cin >> n;
    while (n--) { cin >> a >> b >> c; x += a, y += b, z += c; }
    if (x == 0 &&  y == 0 &&  z == 0)cout << "YES";
    else cout << "NO";
    return 0;
} 

17. 122A - Lucky Division

//根据题意发现是对一些数是否除尽
#include<bits/stdc++.h>
using namespace std;
int main() {
    int n;
    cin >> n;
    cout << (n % 4 == 0 || n % 7 == 0 || n % 47 == 0 || n % 74 == 0 || n % 477 == 0 ? "YES" : "NO");
    return 0;
}

18. 58A - Chat room

#include<bits/stdc++.h>
using namespace std;
int main(){
    string s, A = "hello"; int i = 0;
    cin >> s;
    for (auto c : s)if (c == A[i])++i;
    if (i == 5)cout << "YES";
    else cout << "NO";
}

19. 546A - Soldier and Bananas

#include<bits/stdc++.h>
using namespace std;
int main() {
    long long n, k, w, t;
    cin >> k >> n >> w;
    t = (1 + w) * w * k / 2;
    if (n >= t)cout << 0;
    else cout << t - n;
    return 0;
}

20. 116A - Tram

#include<bits/stdc++.h>
using namespace std;
int main() {
    int n, a, b, Max = 0,t = 0; cin >> n;
    while (n--) { cin >> a >> b; t += b - a; Max = max(Max, t); }
    cout << Max;
    return 0;
}

21. 791A - Bear and Big Brother

#include<bits/stdc++.h>
using namespace std;
int main(){
    int a, b, t = 0; cin >> a >> b;
    while (a <= b) { a *= 3, b *= 2; ++t; }
    cout << t;
    return 0;
}

22. 160A - Twins

#include<bits/stdc++.h>
using namespace std;
int main(){
    int t, n = 0, h = 0, i, j, a[105];
    cin >> t;
    for (i = 0; i < t; i++){
        cin >> a[i];
        h += a[i];
    }
    sort(a, a + t);
    for (i = 0; 2 * n <= h; i++) n += a[t - i - 1];
    cout << i << endl;
}

23. 977A.Wrong Subtraction

#include<bits/stdc++.h>
using namespace std;
int main() {
    __int64 n; int k;cin >> n >> k;
    while (k--) if (n % 10 == 0)n /= 10; else n--;
    cout << n;
    return 0;
}

24.617A.Elephant

#include<bits/stdc++.h>
using namespace std;
int main() {
    __int64 n; cin >> n;
    cout << (n + 4) / 5;
    return 0;
}

25. 133A. HQ9+

#include<bits/stdc++.h>
using namespace std;
int main() {
    string s; bool flag = false; cin >> s;
    for (auto c : s)if (c == ‘H‘ || c == ‘Q‘ || c == ‘9‘) { flag = true; break; }
    if (flag)cout << "YES";
    else cout << "NO";
    return 0;
}

26. 158B - Taxi

#include <iostream>
#include <algorithm>
using namespace std;
int main(){
    int n;
    while (cin >> n){
        int a[5] = { 0 }, cnt,sum = 0;
        while(cin >> cnt)a[cnt]++;
        for (int i = 4; i >= 1; i--){
            if (i == 4)
                sum += a[i];
            else if (i == 3){
                sum += a[3];
                a[1] -= min(a[3], a[1]);
            }
            else if (i == 2){
                sum += a[2] / 2;
                if (a[2] % 2 == 0)
                    a[2] = 0;
                else{
                    a[2] = 1;
                    a[1] -= 2;
                    sum++;
                }
            }
            else{
                if (a[1] > 0){
                    if (a[1] % 4 == 0)
                        sum += a[1] / 4;
                    else sum += a[1] / 4 + 1;
                }
            }
        }
        cout << sum << endl;
    }
    return 0;
}

转化为数论解法

#include<iostream>
#include<algorithm>
using namespace std;
int A[5], N,  t;
int main() {
    cin >> N; while (std::cin >> t)A[t]++;
    A[1] = max(A[1] - A[3], 0);
    cout << A[3] + A[4] + (A[1] + 2 * A[2] + 3) / 4;
    return 0;
}

27. 266B - Queue at the School

#include<bits/stdc++.h>
using namespace std;
int main() {
    string s; int n, m;
    cin >> n >> m >> s;
    while (m--) for (int i = 0; i < n - 1; ++i)if (s[i] == ‘B‘ && s[i + 1] == ‘G‘) { swap(s[i], s[i + 1]); ++i; }
    cout << s;
    return 0;
}

28. 110A - Nearly Lucky Number

#include<bits/stdc++.h>
using namespace std;
int main() {
    string s; int i = 0; cin >> s;
    for (auto c : s)i += (c == ‘4‘ || c == ‘7‘);
    if (i == 4 || i == 7)cout << "YES";
    else cout << "NO";
    return 0;
}

29. 41A - Translation

#include<bits/stdc++.h>
using namespace std;
int main() {
    string s, ss; cin >> s >> ss;
    int len = s.length();
    for (int i = 0; i < len; ++i)if (s[i] != ss[len - i - 1]) { cout << "NO"; return 0; }
    cout << "YES";
    return 0;
}

30. 59A - Word

#include <bits/stdc++.h>
using namespace std;
int main(){
    string s; cin >> s;
    int uc = 0, lc = 0;
    for (char c : s)(isupper(c) ? uc : lc)++;
    for (char c : s) cout << char(lc >= uc ? tolower(c) : toupper(c));
    return 0;
}

以上是关于Hottest 30 of codeforce的主要内容,如果未能解决你的问题,请参考以下文章

敲击键盘时触发 hotTest

题解[CodeForces171C]A Piece of Cake

Codeforces 989C A Mist of Florescence 构造

Codeforces 556D - Case of Fugitive

Codeforces 920G - List Of Integers

Codeforces466C Number of Ways