Informatics Olympiad 1.2: Sequence structure programming (2)

​The first part of C++ language

Chapter 2 Sequence Structure Program Design

Section 4 Data Input and Output

1024 floating point number with 3 decimal places
#include <cstdio>
using namespace std;

int main() {
    float a;

    scanf("%f", &a);
    printf("%.3f\n", a);
​
    return 0;
}
1025 Floating point number with 12 decimal places
#include <cstdio>
using namespace std;

int main() {
    double a;

    scanf("%lf", &a);
    printf("%.12lf\n", a);

    return 0;
}
1026 Space separated output
#include <cstdio>
using namespace std;

int main() {
    char a;
    int b;
    float c;
    double d;

    scanf("%c %d %f %lf", &a, &b, &c, &d);
    printf("%c %d %f %lf\n", a, b, c, d);

    return 0;
}
1027 Output floating point number
#include <cstdio>
using namespace std;

int main() {
    double a;

    scanf("%lf", &a);
    printf("%f\n%.5f\n%e\n%g\n", a, a, a, a);

    return 0;
}
1028 character diamond
#include <cstdio>
using namespace std;

int main() {
    char c;

    scanf("%c", &c);
    printf("  %c\n", c);
    printf(" %c%c%c\n", c, c, c);
    printf("%c%c%c%c%c\n", c, c, c, c, c);
    printf(" %c%c%c\n", c, c, c);
    printf("  %c\n", c);

    return 0;
}

Section 5 Example of Sequence Structure

1029 Calculate the remainder of the division of floating-point numbers
#include <iostream>
using namespace std;

int main() {
    double a, b, r;

    cin >> a >> b;
    r = a - int(a / b) * b;
    cout << r << endl;

    return 0;
}
1030 Calculate the volume of the ball
#include <cstdio>
using namespace std;

int main() {
    double r, V;

    scanf("%lf", &r);
    V = 4.0 / 3.0 * 3.14 * r * r * r;
    printf("%.2lf\n", V);

    return 0;
}
1031 Reverse output a three-digit number
#include <cstdio>
using namespace std;

int main() {
    int a;

    scanf("%d", &a);
    printf("%d%d%d\n", a%10, a/10%10, a/10/10);

    return 0;
}
1032 Elephant Drinking Water
#include <cstdio>
#include <cmath>
using namespace std;

int main() {
    int h, r, x;

    scanf("%d %d", &h, &r);
    x = ceil(20000 / (3.14 * r * r * h));
    printf("%d\n", x);
    return 0;
}
1033 Calculate the length of the line segment
#include <cstdio>
#include <cmath>
using namespace std;

int main() {
    double xa, ya, xb, yb, len;

    scanf("%lf %lf %lf %lf", &xa, &ya, &xb, &yb);
    len = sqrt((xb - xa) * (xb - xa) + (yb - ya) * (yb - ya));
    printf("%.3lf\n", len);

    return 0;
}
1034 Calculate the area of ​​a triangle
#include <cstdio>
#include <cmath>
using namespace std;

int main() {
    float x1, y1, x2, y2, x3, y3;
    double a, b, c, p, s;

    scanf("%f %f %f %f %f %f", &x1, &y1, &x2, &y2, &x3, &y3);
    a = sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
    b = sqrt((x3 - x2) * (x3 - x2) + (y3 - y2) * (y3 - y2));
    c = sqrt((x1 - x3) * (x1 - x3) + (y1 - y3) * (y1 - y3));
    p = (a + b + c) / 2;
    s = sqrt(p * (p - a) * (p - b) * (p - c));

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

    return 0;
}
1035 Calculation of the last term of the arithmetic sequence
#include <cstdio>
using namespace std;

int main() {
    int a1, a2, n;

    scanf("%d %d %d", &a1, &a2, &n);
    printf("%d\n", a1 + (a2 - a1) * (n - 1));

    return 0;
}
1036 AxB problem
#include <cstdio>
using namespace std;

int main() {
    int A, B;

    scanf("%d %d", &A, &B);
    printf("%lld\n", (long long)A * (long long)B);

    return 0;
}
1037 Calculate the power of 2
#include <cstdio>
#include <cmath>
using namespace std;

int main() {
    int n;

    scanf("%d", &n);
    printf("%d\n", (int)pow(2, n));

    return 0;
}
1038 Apple and Bug
#include <cstdio>
#include <cmath>
#include <algorithm>

using namespace std;

int main() {
    int n, x, y, left, ans;

    scanf("%d %d %d", &n, &x, &y);
    left = n - ceil(1.0 * y / x);
    printf("%d\n", max(0, left));

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