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

​The first part of C++ language

Chapter 4 Program Design of Loop Structure

The second section while statement

1085 Calculation of the bounce height of the ball

#include <cstdio>
using namespace std;

int main(){
    double h;
    scanf("%lf", &h);
    double ttl = h;

    int n = 1;
    while(n < 10){
        n++;
        h /= 2;
        ttl += h*2;
    }

    printf("%g\n%g\n", ttl, h/2);

    return 0;
}
1086 Kakutani Conjecture
#include <cstdio>
using namespace std;

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

    while (n != 1) {
        if ( n%2 == 0) {
            printf("%d/2=%d\n", n, n/2);
            n /= 2;
        }
        else{
            printf("%d*3+1=%d\n", n, n*3+1);
            n = n * 3 + 1;
        }
    }

    printf("End\n");

    return 0;
}
1087 series sum
#include <cstdio>
using namespace std;

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

    while (sum <= k) {
        n++;
        sum += 1.0 / n;
    }

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

    return 0;
}
1088 Separate the numbers of integers
#include <cstdio>
using namespace std;

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

    while (n) {
        printf("%d ", n%10);
        n /= 10;
    }

    return 0;
}
1089 Number Reversal
#include <cstdio>
using namespace std;

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

    if (n < 0) {
        printf("-");
        n *= -1;
    }

    while (n % 10 == 0) {
        n /= 10;
    }

    while (n) {
        printf("%d", n%10);
        n /= 10;
    }

    return 0;
}
1090 contains k 3 numbers
#include <cstdio>
using namespace std;

int main() {
    int m, k, cnt = 0;
    scanf("%d%d", &m, &k);

    int t = m;
    while (t) {
        if (t%10 == 3) cnt++;
        t /= 10;
    }

    if (cnt == k && m%19 == 0) {
        printf("YES\n");
    }
    else {
        printf("NO\n");
    }

    return 0;
}

The fourth section loop nesting

1091 Find the sum of factorials

#include <cstdio>
using namespace std;

int main() {
    int n, t = 1, s = 0;

    scanf("%d", &n);

    for (int i = 1; i <= n; i++) {
        t = 1;
        for (int j = 1; j <= i; j++) {
            t *= j;
        }
        s += t;
    }

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

    return 0;
}
1092 Find the value of e
#include <cstdio>
using namespace std;

int main() {
    int n;
    long long t = 1;
    double s = 1;

    scanf("%d", &n);

    for (int i = 1; i <= n; i++) {
        t = 1;
        for (int j = 1; j <= i; j++) {
            t *= j;
        }
        s += 1.0 / t;
    }

    printf("%.10lf\n", s);

    return 0;
}
1093 Calculate the value of a polynomial
#include <cstdio>
using namespace std;

int main() {
    int n;
    double x = 1, t = 1, s = 1;

    scanf("%lf%d", &x, &n);

    for (int i = 1; i <= n; i++) {
        t = 1;
        for (int j = 1; j <= i; j++) {
            t *= x;
        }
        s += t;
    }

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

    return 0;
}
1094 Number not related to 7
#include <cstdio>
using namespace std;

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

    for (int i = 1; i <= n; i++) {
        if (i%10 != 7 && i/10 != 7 && i%7 !=0) {
            sum += i * i;
        }
    }

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

    return 0;
}
1095 Number of 1
#include <cstdio>
using namespace std;

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

    for (int i = 1; i <= n; i++) {
        int t = i;
        while (t) {
            if (t%10 == 1) cnt++;
            t /= 10;
        }
    }

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

    return 0;
}
1096 Number Statistics
#include <cstdio>
using namespace std;

int main() {
    int l, r, cnt = 0;
    scanf("%d%d", &l, &r);

    for (int i = l; i <= r; i++) {
        int t = i;
        while (t) {
            if (t%10 == 2) cnt++;
            t /= 10;
        }
    }

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

    return 0;
}
1097 Draw a rectangle
#include <cstdio>
using namespace std;

int main() {
    int h, w, flag;
    char ch;
    scanf("%d %d %c %d", &h, &w, &ch, &flag);

    for (int j = 0; j < w; j++) printf("%c", ch);
    printf("\n");

    for (int i = 1; i < h - 1; i++) {
        printf("%c", ch);

        for (int j = 1; j < w - 1; j++) {
            if (flag) printf("%c", ch);
            else printf(" ");
        }

        printf("%c\n", ch);
    }

    for (int j = 0; j < w; j++) printf("%c", ch);

    return 0;
}
1098 Prime factor decomposition
#include <cstdio>
using namespace std;

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

    for (int i = 2; i <= n/i; i++) {
        if (n % i == 0) {
            printf("%d", n / i);
            break;
        }
    }

    return 0;
}
1099 n-th smallest prime number
#include <cstdio>
using namespace std;

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

    int i, j;
    for (i = 2, j = 0; j < n; i++) {
        bool flag = true;
        for (int k = 2; k <= i/k; k++) {
            if (i%k == 0) {
                flag = false;
                break;
            }
        }
        if (flag) j++;
    }

    printf("%d", i - 1);

    return 0;
}
1100 gold coins
#include <cstdio>
using namespace std;

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

    for (int i = 1;  ; i++) {
        for (int j = 1; j <= i; j++) {
            day++;
            if (day > n) {
                printf("%d\n", sum);
                return 0;
            }
            sum += i;
        }
    }

    return 0;
}
1101 Solving indefinite equations
#include <cstdio>
using namespace std;

int main() {
    int a, b, c, cnt = 0;
    scanf("%d%d%d", &a, &b, &c);

    for (int x = 0; x <= c; x++) {
        for (int y = 0; y <= c; y++) {
            if (a*x + b*y == c) cnt++;
        }
    }

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

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