实验2

任务1

源代码:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define N 5
#define N1 397
#define N2 476
#define N3 21

int main() {
    int cnt;
    int random_major, random_no;

    srand(time(NULL));     

    cnt = 0;
    while(cnt < N) {
        random_major = rand() % 2;

        if(random_major) {
            random_no = rand() % (N2 - N1 + 1) + N1;
            printf("20248329%04d\n", random_no);
        }
        else {
            random_no = rand() % N3 + 1;
            printf("20248395%04d\n", random_no);
        }

        cnt++;
    }

    return 0;
}

运行结果:

 问题1:生成397与476之间的随机整数

问题2:生成1与21之间的随机整数

问题3:等概率地从两个班抽取末尾为1~21 或397~476的学号

任务2

源代码:

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

int main() {
    double a, b, c;
    double delta, p1, p2; 

    while(scanf("%lf%lf%lf", &a, &b, &c) != EOF) {
        if(a == 0) {
            printf("a = 0, invalid input\n");
            continue;
        }

        delta = b*b - 4*a*c;
        p1 = -b/2/a;
        p2 = sqrt(fabs(delta))/2/a;

        if(delta == 0)
            printf("x1 = x2 = %.2g\n", p1);
        else if(delta > 0)
            printf("x1 = %.2g, x2 = %.2g\n", p1+p2, p1-p2);
        else {
            printf("x1 = %.2g + %.2gi, ", p1, p2);
            printf("x2 = %.2g - %.2gi\n", p1, p2);
        }
    }

    return 0;
}

运行结果:

 

任务3

源代码:

#include<stdio.h>
int main(){
    char signal;
while(1){
    printf("please enter the traffic light color :\n");
    scanf(" %c", &signal);
    if(signal == 'r')
        printf("stop!\n");
    else if(signal == 'g')
        printf("go go go\n");
    else if(signal == 'y')
        printf("wait a minute\n");
    else
        printf("something must be wrong...\n");
}
    return 0;
} 

运行结果:

任务4

源代码:

#include <stdio.h>
int main()
{
    double expense, total, max, min;
    max = 0;
    min = 20000;
    total = 0;
    while(scanf("%lf",&expense)!=EOF && expense != -1)
    {
        if(expense>max)
            max = expense;
        if(expense<min)
            min = expense;
        total += expense;
    }
    printf("最大开销 = %lf\n",max);
    printf("最低开销 = %lf\n",min);
    printf("总开销 = %lf\n",total);
    
    return 0;
}

运行结果:

 任务5

源代码:

#include <stdio.h>
int main()
{
    int a, b, c;
    while(scanf("%d%d%d", &a, &b, &c )!= EOF)
    {
        if(a+b<=c||a+c<=b||b+c<=a)
        printf("不能构成三角形\n");
        else if(a==b&a==c)
        printf("等边三角形\n");
        else if(a==b||a==c||b==c)
        printf("等腰三角形\n"); 
        else if(a*a+b*b==c*c||a*a+c*c==b*b||b*b+c*c==a*a)
        printf("直角三角形\n");
        else
        printf("普通三角形\n");
    }
    return 0;
}

运行结果:

任务6

源代码:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int generateLuckyDay() {
    return rand() % 30 + 1;
}

void checkGuess(int guess, int luckyDay, int attempts, int *correct) {
    if (*correct) {
        return;
    }

    if (guess == luckyDay) {
        *correct = 1;
        printf("哇,猜中了:)\n");
    } else if (guess < luckyDay) {
        printf("你猜的日期早了,你的lucky day还没到呢\n");
    } else {
        printf("你猜的日期晚了,你的lucky day在前面哦\n");
    }
}

int main() {
    int luckyDay;
    int guess;
    int correct = 0;
    int attempts = 0;
    const int maxAttempts = 3;

    srand(time(NULL));

    luckyDay = generateLuckyDay();

    while (attempts < maxAttempts && !correct) {
        printf("猜猜2024年11月哪一天会是你的lucky day\n");
        scanf("%d", &guess);

        checkGuess(guess, luckyDay, attempts, &correct);

        attempts++;
    }

    if (!correct) {
        printf("次数用光啦。偷偷告诉你,11月你的lucky day是:%d\n", luckyDay);
    }

    return 0;
}

运行结果:

 

posted @ 2024-10-13 12:30  林子龙  阅读(4)  评论(0编辑  收藏  举报