首页 > 其他分享 >实验二

实验二

时间:2024-10-14 08:54:03浏览次数:1  
标签:%. int random else 实验 printf include

task_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;//随机生成从397到476的一个值 
            printf("20248329%04d\n", random_no); 
        }
        else {
            random_no = rand() % N3 + 1;
            printf("20248395%04d\n", random_no);//随机生成1到21的一个值 
        }

        cnt++;
    }

    return 0;
}

第二十一行的作用是随机生成397到476的一个值

第二十五行的作用是随机生成1到21的一个值

整个程序的作用是随机抽取名同学的学号

 

task_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=o,不是一元二次方程");
                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;
 } 

task_3

#include<stdio.h>

int main()
{
    char x;
    
    printf("请输入信号灯的颜色,Y、y代表黄灯,R、r代表红灯,G,g代表绿灯\n");
    
    while(scanf(" %c",&x)!=EOF)
    {
        if(x == 'r'||x == 'R')
        {
            printf("stop\n");
        }
        else if(x == 'y'||x == 'Y')
        {
            printf("wait a minute\n");
        }
        else if(x == 'g'||x == 'G')
        {
            printf("go\n");
        }
        else
        {
            printf("wrong color\n");
        } 
    }
    return 0;
} 

task_4

 #include <stdio.h>
 
 int main()
 {
     double shuru, sum = 0.0, max = 0.0, min = 20001.0;
 
     printf("输入今日开销,直到输入-1终止:\n");
 
     while (1)
    {
         scanf("%lf", &shuru);
        if (shuru == -1)
            break;
        else
        {
            sum += shuru;
            max = max < shuru ? shuru : max;
            min = min < shuru ? min : shuru;
        }
    }

    printf("今日累计消费总额:%.1lf\n今日最高一笔开销:%.1lf\n今日最低一笔开销:%.1lf", sum, max, min);

    return 0;
}

 

 

 

task_5

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

int main()
{    
    int i;//循环变量
    int a, b, c;//三角形的三边

    printf_s("输入三角形的三边长\n");
    for (i = 0;i <= i + 1; ++i)
    {
        scanf_s("%d %d %d", &a, &b, &c);

        if (a + b <= c || b + c <= a || c + a <= b)
        {
            printf_s("不构成三角形\n");
        }
        else if (a == b && b == c && c == a)
        {
                printf_s("等边三角形\n");
        }
        else if (a == b && a != c || b == c && b != a || c == a && c != b)
        {
            printf("等腰三角形\n");
        }
        else if (pow(a, 2) + pow(b, 2) == pow(c, 2) || pow(b, 2) + pow(c, 2) == pow(a, 2) || pow(c, 2) + pow(a, 2) == pow(b, 2))
        {
            printf_s("直角三角形\n");
        }
        else
        {
            printf_s("普通三角形\n");
        }
    }

    return 0;
}

 task_6

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
 
  int main()
  {
       srand(time(NULL));
  
       int lucky_day = rand() % 30 + 1;
       int a = 0; 
int i; while (a < 3)
{ scanf("%d", &i); if (i == lucky_day) { printf("猜中了\n"); a = 3; return 0; } else if (i > lucky_day) { printf(" 猜晚了\n"); ++a; } else { printf("猜早了\n"); ++a; } } printf("%d\n", lucky_day); return 0; }

 

标签:%.,int,random,else,实验,printf,include
From: https://www.cnblogs.com/jiaoaodedihuazhixiu/p/18459176

相关文章

  • 操作系统:实验二:进程调度
    1.实验目的  1.了解三级调度的基本概念;  2.通过对先来先服务算法、时间片轮转算法和短作业优先算法的模拟实现掌握它们的工作原理。2.三级调度的基本概念  现在的操作系统大多是多道程序系统,在同一时段内,允许用户同时执行多个作业进(或进程)。一个作业从提......
  • 操作系统:实验三:存储器管理
    1.实验目的  1.理解固定式分区及可变式分区两种存储管理模式,知道各自的优缺点;  2.验证型实验需理解可变式分区方式的三种算法最先适应算法、最佳适应算法和最坏适应算法的工作原理,理解内存释放的具体实现过程;  3.提高型实验需在提供的代码框架下根据提示自主......
  • 实验1 现代c++编程初体验
    任务1:task1.cpp//现代C++标准库、算法库体验//本例用到以下内容://1.字符串string,动态数组容器类vector、迭代器//2.算法库:反转元素次序、旋转元素//3.函数模板、const引用作为形参#include<iostream>#include<string>#include<vector>#include<algorithm>......
  • 实验1
    实验任务1代码:1//现代C++标准库、算法库体验2//本例用到以下内容:3//1.字符串string,动态数组容器类vector、迭代器4//2.算法库:反转元素次序、旋转元素5//3.函数模板、const引用作为形参67#include<iostream>8#include<string>9#......
  • 面向对象程序设计-实验1
    任务一: #include<iostream>#include<string>#include<vector>#include<algorithm>usingnamespacestd;template<typenameT>voidoutput(constT&c);voidtest1();voidtest2();voidtest3();intmain(){cout&l......
  • 实验1 现代c++编程初体验
    实验任务一task1.cpp1//现代C++标准库、算法库体验2//本例用到以下内容:3//1.字符串string,动态数组容器类vector、迭代器4//2.算法库:反转元素次序、旋转元素5//3.函数模板、const引用作为形参6#include<iostream>7#include<string>8#includ......
  • openssl实验截图记录
                                    ......
  • 实验1 C++
    task1:代码:1//现代C++标准库、算法库体验2//本例用到以下内容:3//1.字符串string,动态数组容器类vector、迭代器4//2.算法库:反转元素次序、旋转元素5//3.函数模板、const引用作为形参67#include<iostream>8#include<string>9#......
  • 实验1
    实验任务1:1#include<iostream>2#include<string>3#include<vector>4#include<algorithm>56usingnamespacestd;78template<typenameT>9voidoutput(constT&c);1011voidtest1();12voidtest2();13voi......
  • c++实验1
    实验1://现代C++标准库、算法库体验//本例用到以下内容://1.字符串string,动态数组容器类vector、迭代器//2.算法库:反转元素次序、旋转元素//3.函数模板、const引用作为形参#include<iostream>#include<string>#include<vector>#include<algorithm>usin......