实验2

实验任务一
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define N 5
#define N1 80
#define N2 35

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()%N1+1;
            printf("20256343%04d\n",random_no);
        }
        else{
            random_no=rand()%N2+1;
            printf("20256136%04d\n",random_no);
        
        }
    
        cnt++;
    }
    return 0;
}

学号

 

该语句作用是先以班级为单位分开,起一个归纳的作用。

 

程序功能是抽人回答问题。

实验任务二

 

1:去掉该语句第二次运行会扣掉两次运行的钱数总和

2:无效数据,跳过continue后的代码,直接进入下一次循环。

试验任务三

 

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

红绿灯

 试验任务四

#include<stdio.h>
int main(){
    double price, total=0, max=0,min=20000;
    printf("输入一天内的若干笔开销,输入-1时终止程序");
    while(1){
        scanf("%lf",&price);
        if(price==-1){
            break;
        }
        if(price<=1||price>=20000){
            printf("不合规\n"); 
            continue;
        }
        total+=price;
        if(price>max){
            max=price;
        }
        if(price<min){
            min=price;
        }
        
    
    }
    printf("今日累计消费总额:%.1lf\n", total);
    printf("今日最高一笔开销:%.1lf\n", max);
    printf("今日最低一笔开销:%.1lf\n", min);
    return 0;
    
} 

支出

实验任务五

#include<stdio.h>
#include<math.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");
            break;
        }
        else{
            if(a==b&&b==c) {
                printf("等边三角形\n");
            }
            else if(a==b||b==c||a==c){
                printf("等腰三角形\n");
            }
            else if(a*a==b*b+c*c||b*b==a*a+c*c||c*c==a*a+b*b){
                printf("直角三角形\n");
            }
            else{
                printf("普通三角形\n");
            }
            
        }
    }
    return 0;
} 

三角形

实验任务六

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main(){
    int luckyday, guess;
    int chance=3;
    srand((unsigned int)time(NULL));
    luckyday=rand()%30+1;
    printf("猜猜2025年11月哪一天是你的luckyday\n");
    printf("开始喽,你有三次机会,猜吧(1~30): ");
    while(chance>0){
        scanf("%d",&guess);
        if(guess<1||guess>30){
            printf("输入的数字不在1~30范围内,请重新输入: ");
            continue;
        } 
        if(guess==luckyday){
            printf("恭喜答对\n");
            return 0; 
        }
        else if(guess>luckyday){
            printf("你猜的日期晚了,你的lucky day在前面哦\n");
        }
        else{
            printf("你猜的日期早了,你的lucky day还没到呢\n");
        }
        chance--;
        if (chance > 0){
            printf("再猜(1~30): "); 
        } 
        
    } 
    printf("次数用光啦。偷偷告诉你,11月你的luckyday是%d号\n", luckyday);
    return 0;
} 

luckyday

 

posted @ 2025-10-14 16:27  孙棒棒  阅读(6)  评论(1)    收藏  举报