净星 发布的文章

#include <stdio.h>

int main() {
    int score;
    int i;
    int maxScore = 0;
    float avgScore = 0;

    for (i = 0; i < 10; i++) {
        printf("请输入第%d位同学的成绩:", i+1);
        scanf_s("%d", &score);
        avgScore += score;
        if (score > maxScore) {
            maxScore = score;
        }
    }

    avgScore /= 10;

    printf("平均分是: %f\n", avgScore);
    printf("最高分是: %d\n", maxScore);

    return 0;
}

例:求Fibonacci数列前40个数,每行输出4个数。这个数列有如下特点:第1,2两个数为1,1。从第3个数开始,该数是其前面两个数之和。

#include <stdio.h>

int main() {
    int i, n = 40;
    int f1 = 1, f2 = 1, f3;

    printf("%d %d ", f1, f2);

    for (i = 3; i <= n; i++) {
        f3 = f1 + f2;
        printf("%d ", f3);
        f1 = f2;
        f2 = f3;

        if (i % 4 == 0) {
            printf("\n");
        }
    }

    return 0;
}

例:试找出满足下列条件的所有两位数
1) 其十位数不大于2
2) 将个位与十位对换,得到的两位数是原两位数
的两倍多

#include <stdio.h>

int main() {
    int i = 0;
    while (i <= 2) {
        int j = 0;
        while (j <= 9) {
            int num = i * 10 + j;
            int reversed = j * 10 + i;
            if (reversed >= num * 2 && reversed < num * 3) {
                printf("%d\n", num);
            }
            j++;
        }
        i++;
    }

    return 0;
}

#include <stdio.h>
#include <math.h>

int main() {
    float x, y, r;

    printf("请输入点M的坐标x: ");
    scanf_s("%f", &x);

    printf("请输入点M的坐标y: ");
    scanf_s("%f", &y);

    printf("请输入圆的半径: ");
    scanf_s("%f", &r);

    float d = sqrt(x * x + y * y);

    if (d < r) {
        printf("点M在圆内\n");
    }
    if (d == r) {
        printf("点M在圆上\n");
    }
    if (d > r) {
        printf("点M在圆外\n");
    }
    return 0;
}

#include <stdio.h>

int main() {
    float p, w, s, d, f;
    printf("请输入基本运费p,货物重w,距离s:\n");
    scanf_s("%f,%f,%f", &p, &w, &s);
    int n = s / 250;
    switch (n) {
    case 0:
        d = 0;
        break;
    case 1:
        d = 0.02;
        break;

    case 2:
    case 3:
        d = 0.05;
        break;

    case 4:
    case 5:
    case 6:
    case 7:
        d = 0.08;
        break;

    case 8:
    case 9:
    case 10:
    case 11:
        d = 0.1;
        break;
    default:
        d = 0.15;
        break;
    }
    f = p * w * s * (1 - d);
    printf("总运费f=%f\n", f);
    return 0;
}