第二次作业

任务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;

line 21功能:随机数范围在(396,478)

line25功能:随机数范围在(0,22)

程序的功能:随机在两个班级生成五个同学的学号

图片

任务2

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int main()
{
    double a,b,c,delta,x1,x2,p,q;
    scanf("%lf %lf %lf",&a,&b,&c);
    if(a==0)
        printf("不是一元二次方程!\n");
    else
    {delta=b*b-4*a*c;
    if(delta==0)
    {
        printf("方程有两个相等的实数根!\n");
        x1=-b/(2*a);
        x2=x1;
        printf("%.2lf,    %.2f\n",x1,x2);
    }
    else if(delta>0)
    {
        printf("方程有两个不相等的实数根!\n");
            x1=-b/(2*a)+sqrt(delta)/(2*a);
            x2=-b/(2*a)-sqrt(delta)/(2*a);
            printf("%.2lf,    %.2lf\n",x1,x2);
    }
    else
    {
        printf("方程有两个不相等的复数根!\n");
        p=-b/(2*a);
        q=sqrt(-delta)/(2*a);
        printf("%.2lf+ %.2lfi\n",p,q);
        printf("%.2lf- %.2lfi\n",p,q);
    }
    }
        system("pause");
    return 0;
      
   
}

图片

任务3

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

return 0;
}

图片

任务4

#include <stdio.h>

int main() {
    double expense, maxExpense = 0.0, minExpense = 20000.0, totalExpense = 0.0;
    int done = 0;

    printf("输入今日开销,直到输入-1终止:\n");

    while (!done) {
        scanf("%lf", &expense);

        if (expense == -1) {
            done = 1;
        } else if (expense > 0 && expense <= 20000) {
       
            if (expense > maxExpense) {
                maxExpense = expense;
            }
            if (expense < minExpense) {
                minExpense = expense;
            }
            
            totalExpense += expense;
        } 
    }

    // 输出结果
    printf("今日累计消费总额: %.1f\n", totalExpense);
    printf("今日最高一笔开销: %.1f\n", maxExpense);
    printf("今日最低一笔开销: %.1f\n", minExpense);

    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||b==c) 
            if(a==b&&b==c)
               printf("等边三角形\n");
            else 
               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;        
}

图片

任务6

#include<stdio.h>
#include<time.h>
#include<stdlib.h>
int main()
{
    int luckyday,i,day;
    srand(time(NULL));
    luckyday=rand()%30+1;
    printf("猜猜11月哪一天会是你的lucky day\n");
    printf("开始喽,你有三次机会,猜吧(1~30):");
    
    for(i=1;i<4;i++){
        scanf("%d",&day);
        if(luckyday==day)
           { 
            printf("哇,猜中了:)");
            break; }
        else if(luckyday<day)
            {
            printf("你猜的日期晚了,你的lucky day在前面哦\n");
            printf("再猜(1~30):");}
        else
            {
            printf("你猜的日期早了,你的lucky day在后面哦\n"); 
            printf("再猜(1~30):");}
    }
    if(luckyday!=day)
      printf("次数用光啦,偷偷告诉你,11月你的lucky day是%d号",luckyday);
      return 0;
}

猜测成功的图片

猜测失败的图片

 

posted @ 2024-10-09 19:35  下北泽的小孤独  阅读(4)  评论(0编辑  收藏  举报