Informatics Olympiad One Pass 1.4: Program Design of Loop Structure (1)

The first part of C++ language

Chapter 4 Program Design of Loop Structure

The first section for statement

1059 Find the average age
#include <cstdio>
using namespace std;

int main() {
    int n, age, sum = 0;
    scanf("%d", &n);

    for (int i = 0; i < n; i++) {
        scanf("%d", &age);
        sum += age;
    }

    printf("%.2lf\n", 1.0 * sum / n);

    return 0;
}
1060 mean
#include <cstdio>
using namespace std;

int main() {
    int n;
    double a, sum = 0;
    scanf("%d", &n);

    for (int i = 0; i < n; i++) {
        scanf("%lf", &a);
        sum += a;
    }

    printf("%.4lf\n", 1.0 * sum / n);

    return 0;
}
1061 Find the sum and mean of integers
#include <cstdio>
using namespace std;

int main() {
    int n, a, sum = 0;
    scanf("%d", &n);

    for (int i = 0; i < n; i++) {
        scanf("%d", &a);
        sum += a;
    }

    printf("%d %.5lf\n", sum, 1.0 * sum / n);

    return 0;
}
1062 highest score
#include <cstdio>
#include <algorithm>
using namespace std;

int main() {
    int n, score, maxi = 0;
    scanf("%d", &n);

    for (int i = 0; i < n; i++) {
        scanf("%d", &score);
        maxi = max(maxi, score);
    }

    printf("%d\n", maxi);

    return 0;
}
1063 Maximum span value
#include <cstdio>
#include <algorithm>
using namespace std;

int main() {
    int n, a, maxi = 1, mini = 1000;
    scanf("%d", &n);

    for (int i = 0; i < n; i++) {
        scanf("%d", &a);
        maxi = max(maxi, a);
        mini = min(mini, a);
    }

    printf("%d\n", maxi - mini);

    return 0;
}
1064 Olympic medal count
#include <cstdio>

using namespace std;

int main() {
    int n, g, s, b;
    int sum_g = 0, sum_s = 0, sum_b = 0;
    scanf("%d", &n);

    for (int i = 0; i < n; i++) {
        scanf("%d %d %d", &g, &s, &b);
        sum_g += g;
        sum_s += s;
        sum_b += b;
    }

    printf("%d %d %d %d\n", sum_g, sum_s, sum_b, sum_g + sum_s + sum_b);

    return 0;
}
1065 Sum of Odd Numbers
#include <cstdio>
using namespace std;

int main() {
    int m, n, sum = 0;
    scanf("%d %d", &m, &n);

    for (int i = m; i <= n; i++) {
        if (i % 2 == 1) sum += i;
    }

    printf("%d\n", sum);

    return 0;
}
1066 Accumulation of numbers satisfying conditions
#include <cstdio>
using namespace std;

int main() {
    int m, n, sum = 0;
    scanf("%d %d", &m, &n);

    for (int i = m; i <= n; i++) {
        if (i % 17 == 0) sum += i;
    }

    printf("%d\n", sum);

    return 0;
}
1067 Number of integers
#include <cstdio>
using namespace std;

int main() {
    int k, a;
    int cnt_1 = 0, cnt_5 = 0, cnt_10 = 0;
    scanf("%d", &k);

    for (int i = 1; i <= k; i++) {
        scanf("%d", &a);
        if (a == 1) cnt_1 ++;
        if (a == 5) cnt_5 ++;
        if (a == 10) cnt_10 ++;
    }

    printf("%d\n%d\n%d\n", cnt_1, cnt_5, cnt_10);

    return 0;
}
1068 The number of the same number as the specified number
#include <iostream>
using namespace std;

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

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

    cout << cnt << endl;

    return 0;
}
1069 Power calculation
#include <iostream>
using namespace std;

int main() {
    int a, n, ans = 1;
    cin >> a >> n;

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

    cout << ans << endl;

    return 0;
}
1070 population growth
#include <cstdio>
using namespace std;

int main() {
    double x;
    int n;
    scanf("%lf %d", &x, &n);

    for (int i = 0; i < n; i++) {
        x *= (1 + 0.1/100);
    }

    printf("%.4lf\n", x);

    return 0;
}
1071 Fibonacci Number
#include <iostream>
using namespace std;

int main() {
    int k;
    cin >> k;

    if (k <= 2) cout << 1 << endl;
    else {
        int f1 = 1, f2 = 1, f3;
        for (int i = 3; i <= k; i++) {
            f3 = f1 + f2;
            f1 = f2;
            f2 = f3;
        }
        cout << f3 << endl;
    }

    return 0;
}
1072 Cocktail Therapy
#include <iostream>
using namespace std;

int main() {
    int n;
    cin >> n;

    int a, b;
    double x, y;

    cin >> a >> b;
    x = 1.0 * b / a;

    for (int i = 2; i <=n; i++) {
        cin >> a >> b;
        y = 1.0 * b / a;

        if (y - x > 0.05) cout << "better" << endl;
        else if (x - y > 0.05) cout << "worse" << endl;
        else cout << "same" << endl;
    }

    return 0;
}
1073 Rescue
#include <iostream>
#include <cmath>
using namespace std;

int main() {
    int n, ppl;
    cin >> n;

    double x, y, len, t = 0;

    for (int i = 1; i <= n; i++) {
        cin >> x >> y >> ppl;
        len = sqrt(x*x + y*y);
        t = t + 2 * len / 50 + ppl * 1.5;
    }

    cout << ceil(t) << endl;

    return 0;
}
1074 Jinjin Savings Plan
#include <iostream>
using namespace std;

int main() {
    int budget, jj = 0, mm = 0;

    for (int i = 1; i <= 12; i++) {
        cin >> budget;

        int t = jj + 300 - budget;

        if (t < 0) {
            cout << -1 * i;
            return 0;
        }

        jj = t % 100;
        mm += t / 100;

    }

    cout << jj + mm * 120 << endl;

    return 0;
}
1075 Pharmacy Management
#include <iostream>
using namespace std;

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

    for (int i = 1; i <= n; i++) {
        cin >> a;
        if (a <= m) m -= a;
        else cnt ++;
    }

    cout << cnt << endl;

    return 0;
}
1076 Normal blood pressure
#include <iostream>
using namespace std;

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

    for (int i = 1; i <= n; i++) {
        cin >> a >> b;
        if (a >= 90 && a <= 140 && b >= 60 && b <= 90) {
            cnt ++;
            if (cnt > max) {
                max = cnt;
            }
        }
        else {
            cnt = 0;
        }
    }

    cout << max << endl;

    return 0;
}
1077 count the 4 digits that meet the conditions
#include <iostream>
using namespace std;

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

    for (int i = 0; i < n; i++) {
        cin >> a;
        if (a%10 - a/1000 - a/100%10 - a/10%10 > 0) {
            cnt ++;
        }
    }

    cout << cnt << endl;

    return 0;
}
1078 Sum of the sequence of scores
#include <cstdio>
using namespace std;

int main() {
    int n, p = 1, q = 2;
    scanf("%d", &n);

    double sum = 0;

    for (int i = 1; i <= n; i++) {
        sum += 1.0 * q / p;
        q = q + p;
        p = q - p;
    }

    printf("%.4lf\n", sum);

    return 0;
}
1079 Calculate the value of the score addition and subtraction expression
#include <cstdio>
using namespace std;

int main() {
    int n;
    scanf("%d", &n);

    double sum = 0;

    for (int i = 1; i <= n; i++) {
        if (i % 2 == 1) {
            sum += 1.0 / i;
        }
        else {
            sum -= 1.0 / i;
        }
    }

    printf("%.4lf\n", sum);

    return 0;
}
1080 remainder same problem
#include <iostream>
using namespace std;

int main() {
    int a, b, c;
    cin >> a >> b >> c;

    for (int i = 2; i <= 1e6; i++) {
        if (a%i == b%i && b%i == c%i) {
            cout << i << endl;
            break;
        }
    }

    return 0;
}
1081 cents of apples
#include <iostream>
using namespace std;

int main() {
    int n, ttl = 0;

    cin >> n;

    for (int i = 1; i <= n; i++) {
        ttl += i;
    }

    cout << ttl << endl;

    return 0;
}
1082 Seeking a certain decimal place
#include <iostream>
using namespace std;

int main() {
    int a, b, n;

    cin >> a >> b >> n;

    for (int i = 1; i <= n; i++) {
        a = a%b;
        a *= 10;
    }

    cout << a/b << endl;

    return 0;
}
1083 Calculate the day of the week
#include <iostream>
using namespace std;

int main() {
    int a, b, day = 1;

    cin >> a >> b;

    for (int i = 1; i <= b; i++) {
        day = day * a % 7;
    }

    if (day == 0) cout << "Sunday" << endl;
    else if (day == 1) cout << "Monday" << endl;
    else if (day == 2) cout << "Tuesday" << endl;
    else if (day == 3) cout << "Wednesday" << endl;
    else if (day == 4) cout << "Thursday" << endl;
    else if (day == 5) cout << "Friday" << endl;
    else if (day == 6) cout << "Saturday" << endl;

    return 0;
}
1084 end of power
#include <cstdio>
using namespace std;

int main() {
    int a, b, ans = 1;

    scanf("%d %d", &a, &b);

    for (int i = 1; i <= b; i++) {
        ans = ans * a % 1000;
    }

    printf("%03d", ans);

    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/106139517