Informatics Olympiad One Pass 1.5: Array (1)

​The first part of C++ language

Chapter 5 Arrays

The first one-dimensional array

1102 The number of the same number as the specified number

#include <iostream>
using namespace std;

int main() {
    int n, m, a[105], cnt = 0;
    cin >> n;

    for (int i = 0; i < n; i ++ ) cin >> a[i];

    cin >> m;

    for (int i = 0; i < n; i ++ ) {
        if (a[i] == m) cnt ++;
    }

    cout << cnt << endl;

    return 0;
}
1103 Potato Picking Apples
#include <iostream>
using namespace std;

int main() {
    int a[10], h, cnt = 0;

    for (int i = 0; i < 10; i ++ ) cin >> a[i];

    cin >> h;

    for (int i = 0; i < 10; i ++ ) {
        if (h + 30 >= a[i]) cnt ++;
    }

    cout << cnt << endl;

    return 0;
}
1104 Calculating book fees
#include <cstdio>
using namespace std;

int main() {
    int qty;
    double ttl, price[10] = {28.9, 32.7, 45.6, 78, 35, 86.2, 27.8, 43, 56, 65};

    for (int i = 0; i < 10; i ++ ) {
        scanf("%d", &qty);
        ttl += price[i] * qty;
    }

    printf("%.1lf", ttl);

    return 0;
}
1105 Restore the array in reverse order
#include <iostream>
using namespace std;

int main() {
    int n, a[105];

    cin >> n;

    for (int i = 1; i <= n; i ++ ) cin >> a[i];

    for (int i = n; i >= 1; i -- ) cout << a[i] << ' ';

    return 0;
}
1106 Age and Disease
#include <cstdio>
using namespace std;

int main() {
    int n, age, c[4] = {};

    scanf("%d", &n);

    for (int i = 0; i < n; i ++ ) {
        scanf("%d", &age);
        if (age >=0 && age <=18) c[0] ++;
        else if (age <= 35) c[1] ++;
        else if (age <= 60) c[2] ++;
        else c[3] ++;
    }

    printf("%.2lf%%\n", 100.0 * c[0] / n);
    printf("%.2lf%%\n", 100.0 * c[1] / n);
    printf("%.2lf%%\n", 100.0 * c[2] / n);
    printf("%.2lf%%\n", 100.0 * c[3] / n);

    return 0;
}
1107 The Tree Outside the School Gate
#include <iostream>
using namespace std;

int main() {
    int l, m, a[10005], cnt = 0;
    cin >> l >> m;

    for (int i = 0; i <= l; i ++ ) a[i] = 1;

    while (m--) {
        int s, e;
        cin >> s >> e;
        for (int i = s; i <= e; i ++ ) a[i] = 0;
    }

    for (int i = 0; i <= l; i ++ ) {
        if (a[i] == 1) cnt ++;
    }

    cout << cnt << endl;

    return 0;
}
1108 Vector dot product calculation
#include <iostream>
using namespace std;

int main() {
    int n, a[1005], b[1005], ans = 0;

    cin >> n;

    for (int i = 0; i < n; i ++ ) cin >> a[i];

    for (int i = 0; i < n; i ++ ) cin >> b[i];

    for (int i = 0; i < n; i ++ ) {
        ans += a[i] * b[i];
    }

    cout << ans << endl;

    return 0;
}
1109 switch lights
#include <iostream>
using namespace std;

int main() {
    int n, m;
    bool a[5005];

    cin >> n >> m;

    for (int i = 1; i <= n; i ++ ) a[i] = 1;

    for (int i = 1; i <= m; i ++ ) {
        for (int j = 1; j <= n; j ++ ) {
            if (j % i == 0) a[j] = !a[j];
        }
    }

    bool flag = true;
    for (int i = 1; i <= n; i ++ ) {
        if (!a[i]) {
            if (flag) {
                cout << i;
                flag = false;
            }
            else {
                cout << ',' << i;
            }
        }
    }

    return 0;
}
1110 Find a specific value
#include <iostream>
using namespace std;

int main() {
    int n, x, a[10005];

    cin >> n;

    for (int i = 1; i <= n; i ++ ) cin >> a[i];

    cin >> x;

    for (int i = 1; i <= n; i ++ ) {
        if (a[i] == x) {
            cout << i << endl;
            return 0;
        }
    }

    cout << -1 << endl;

    return 0;
}
1111 Unhappy
#include <iostream>
using namespace std;

int main() {
    int a, b, max = 8, day = 0;

    for (int i = 1; i <= 7; i ++ ) {
        cin >> a >> b;
        if (a + b > max) {
            max = a + b;
            day = i;
        }
    }

    if (day > 0) cout << day << endl;
    else cout << 0 << endl;

    return 0;
}
1112 Difference between maximum and minimum
#include <iostream>
#include <algorithm>
using namespace std;

int main() {
    int m, x, a[10005], maxx = 0, minn = 10000;

    cin >> m;

    for (int i = 0; i < m; i ++ ) {
        cin >> x;
        maxx = max(maxx, x);
        minn = min(minn, x);
    }

    cout << maxx - minn << endl;

    return 0;
}
1113 The sum of numbers that are not the same as the largest number
#include <iostream>
#include <algorithm>
using namespace std;

int main() {
    int n, a[105], maxx = 0, sum = 0;

    cin >> n;

    for (int i = 0; i < n; i ++ ) {
        cin >> a[i];
        maxx = max(maxx, a[i]);
    }

    for (int i = 0; i < n; i ++) {
        if (a[i] != maxx) sum += a[i];
    }

    cout << sum << endl;

    return 0;
}
1114 White blood cell count
#include <cstdio>
#include <algorithm>
#include <cmath>
using namespace std;

int main() {
    int n;
    double a[305], avg, diff = 0;

    scanf("%d", &n);
    scanf("%lf", &a[0]);
    double minn = a[0], maxx = a[0], sum = a[0];

    for (int i = 1; i < n; i ++ ) {
        scanf("%lf", &a[i]);
        maxx = max(maxx, a[i]);
        minn = min(minn, a[i]);
        sum += a[i];
    }

    avg = (sum - maxx - minn) / (n - 2);

    bool flag1 = true, flag2 = true;
    for (int i = 0; i < n; i ++ ) {
        if (a[i] == maxx && flag1) {
            flag1 = false;
        }
        else if (a[i] == minn && flag2) {
            flag2 = false;
        }
        else {
            diff = max(diff, abs(a[i] - avg));
        }
    }

    printf("%.2lf %.2lf\n", avg, diff);

    return 0;
}
1115 histogram
#include <iostream>
#include <algorithm>
using namespace std;

int n, a[10005], fmax = 0;

int main() {

    cin >> n;

    for (int i = 0; i < n; i ++ ) {
        int t;
        cin >> t;
        a[t] ++;
        fmax = max(fmax, t);
    }

    for (int i = 0; i <= fmax; i ++ ) cout << a[i] << endl;

    return 0;
}
1116 The longest platform
#include <iostream>
#include <algorithm>
using namespace std;

int main() {
    int n, a, b, cnt = 1, maxx = 0;
    cin >> n;
    cin >> a;

    for (int i = 1; i < n; i ++ ) {
        cin >> b;
        if (b == a) {
            cnt ++;
            maxx = max(maxx, cnt);
        }
        else {
            cnt = 1;
        }

        a = b;
    }

    cout << maxx << endl;

    return 0;
}
1117 integer deduplication
#include <iostream>
using namespace std;

int main() {
    int n, x, a[5005] = {};

    cin >> n;

    for (int i = 0; i < n; i ++ ) {
        cin >> x;
        if (!a[x]) {
            cout << x << ' ';
            a[x] = 1;
        }
    }

    return 0;
}
1118 Carpeting
#include <iostream>
using namespace std;

int main(){
    int n, a[10005][4];
    cin >> n;

    for (int i = 1; i <= n; i ++ ) {
        for (int j = 0; j < 4; j ++ ){
            cin >> a[i][j];
        }
    }

    int x, y;
    cin >> x >> y;

    for (int i = n; i >= 1; i -- ) {
        if (x >= a[i][0] && x <= a[i][0] + a[i][2] && y >= a[i][1] && y <= a[i][1] + a[i][3]) {
            cout << i << endl;
            return 0;
        }
    }

    cout << -1 << endl;

    return 0;
}

If your child is in the fourth grade or above, is interested in computer programming and has spare capacity in cultural lessons, please contact customer service (WeChat ID: xiaolan7321) to participate in informatics learning. We are professional informatics competition coaches. We use online small class teaching methods. The goal is to help primary and middle school students who love programming to achieve excellent results in informatics competitions at home and abroad.

Teaching features:

  • Online small class teaching, lay a good code foundation. Avoid the problem that students in large classes are either "can't keep up" or "not enough to eat".

  • Rich teaching experience, familiar with students' knowledge structure and learning ability, and arrange the schedule reasonably.

  • Practice with competitions, and continuously improve students' abilities through examinations and competitions.

Guess you like

Origin blog.csdn.net/davidliule/article/details/106139538