洛谷语言入门月赛(2022年9月)

A 公园门票

简单的计算题

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


int read() {
    int x = 0, f = 1, ch = getchar();
    while ((ch < '0' || ch > '9') && ch != '-') ch = getchar();
    if (ch == '-') f = -1, ch = getchar();
    while (ch >= '0' && ch <= '9') x = (x << 3) + (x << 1) + ch - '0', ch = getchar();
    return x * f;
}

int32_t main() {
    int a = read() , b = read();
    cout << a * 60 + b * 40 - min( a , b ) * 10 << "\n";
    return 0;
}

B 口算练习

输入输出加计算

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


int read() {
    int x = 0, f = 1, ch = getchar();
    while ((ch < '0' || ch > '9') && ch != '-') ch = getchar();
    if (ch == '-') f = -1, ch = getchar();
    while (ch >= '0' && ch <= '9') x = (x << 3) + (x << 1) + ch - '0', ch = getchar();
    return x * f;
}

int32_t main() {
    int a = read() , b = read();
    cout << (int) floor( 1.0 * a / b ) << "\n";
    return 0;
}

C 课程QQ群

循环

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

int read() {
    int x = 0, f = 1, ch = getchar();
    while ((ch < '0' || ch > '9') && ch != '-') ch = getchar();
    if (ch == '-') f = -1, ch = getchar();
    while (ch >= '0' && ch <= '9') x = (x << 3) + (x << 1) + ch - '0', ch = getchar();
    return x * f;
}

int32_t main() {
    int n = read() , k = read() , cnt = 0;
    for( int x ; n ; n -- )
        x = read() , cnt += ( k == x );
    cout << cnt << "\n";
    return 0;
}

D 集卡

循环

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


int read() {
    int x = 0, f = 1, ch = getchar();
    while ((ch < '0' || ch > '9') && ch != '-') ch = getchar();
    if (ch == '-') f = -1, ch = getchar();
    while (ch >= '0' && ch <= '9') x = (x << 3) + (x << 1) + ch - '0', ch = getchar();
    return x * f;
}

int32_t main() {
    for( int T = read() , f ; T ; T -- ){
        f = 0;
        for( int n = read() , x ; n ; n -- ){
            x = read();
            if( x == 0 ) f = 1;
        }
        cout << ( f ? "yes\n" : "no\n");
    }
    return 0;
}

E 排排队

循环加vector

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


int read() {
    int x = 0, f = 1, ch = getchar();
    while ((ch < '0' || ch > '9') && ch != '-') ch = getchar();
    if (ch == '-') f = -1, ch = getchar();
    while (ch >= '0' && ch <= '9') x = (x << 3) + (x << 1) + ch - '0', ch = getchar();
    return x * f;
}

int32_t main() {
    vector<int> a , b;
    for( int n = read() , x ; n ; n -- ){
        x = read();
        if( x & 1 ) a.push_back(x);
        else b.push_back(x);
    }
    for( int it : a )
        printf("%d " , it );
    printf("\n");
    for( int it : b )
        printf("%d " , it );
    printf("\n");
    return 0;
}

F 山峰

双重循环+二维数组

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

const int N = 1005;
const int dx[] = { 0 , 0, 1 , -1 } , dy[] = { 1 , -1 , 0 , 0 };
int n , m , a[N][N];
vector<pair< int , int >> res;

int read() {
    int x = 0, f = 1, ch = getchar();
    while ((ch < '0' || ch > '9') && ch != '-') ch = getchar();
    if (ch == '-') f = -1, ch = getchar();
    while (ch >= '0' && ch <= '9') x = (x << 3) + (x << 1) + ch - '0', ch = getchar();
    return x * f;
}

int32_t main() {
    n = read() , m = read();
    for( int i = 1 ; i <= n ; i ++ )
        for( int j = 1 ; j <= m ; j ++ )
            a[i][j] = read();
    for( int T = read() , x1 , x2 ,y1 , y2 ; T ; T -- )
        x1 = read() , y1 = read() , x2 = read() , y2 = read() , swap( a[x1][y1] , a[x2][y2] );
    for( int i = 1 ; i <= n ; i ++ )
        for( int j = 1 ; j <= m ; j ++ ){
            int f = 1;
            for( int k = 0 ; f && k < 4 ; k ++ )
                if( a[i][j] <= a[ i+dx[k] ][ j+dy[k] ] ) f = 0;
            if( f ) res.push_back( { i , j } );
        }
    cout << res.size() << "\n";
    for( auto [x,y] : res )
        cout << x << " " << y << "\n";
    return 0;
}

G Luogu Academic

循环+字符串

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

const int N = 1e6+5;


int32_t main() {
    int cnt = 0;
    char s[N];
    scanf("%s" , s );
    for( int i = 0 ; i + 4 < strlen(s) ; i ++ )
        if( s[i] == 'l' && s[i+1] == 'u' && s[i+2] == 'o' && s[i+3] == 'g' && s[i+4] == 'u' ) cnt ++;
    printf( "%d\n" , cnt );
    return 0;
}

也可以 Python

print( input().count("luogu"))

H 零食售卖点

排序+循环

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

const int N = 1e6+5;

int read() {
    int x = 0,  ch = getchar();
    while (ch < '0' || ch > '9') ch = getchar();
    while (ch >= '0' && ch <= '9') x = (x << 3) + (x << 1) + ch - '0', ch = getchar();
    return x ;
}

int32_t main() {
    vector<int> v;
    int res = INT_MIN;
    for( int n = read() ; n ; n -- ) v.push_back(read());
    sort( v.begin() , v.end() );
    for( int i = 1 ; i < v.size() ; i ++ )
        res = max( res , v[i] - v[i-1] );
    cout << res << "\n";
    return 0;
}
posted @ 2022-09-13 15:40  PHarr  阅读(384)  评论(0编辑  收藏  举报