首页 > 其他分享 >实验2

实验2

时间:2024-10-13 17:01:00浏览次数:1  
标签:%. int money random 实验 printf include

任务一

源代码

#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之间的数并赋值给random_no

问题2:随机生成一个1-21之间的数并赋值给random_no

问题3:生成五个随机学号

任务二

#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;
}

任务三

#include<stdio.h>

int main()
{
    char color;
    while ((color = getchar()) != EOF) {
        if (color == 'r') {
            printf("stop!\n");
        }
        else if (color == 'y') {
            printf("wait a minute\n");
        }
        else if (color == 'g') {
            printf("go go go\n");
        }
        else {
            printf("something must be wrong...\n");
        }
        getchar();
    }

    return 0;
}

任务四

源代码

#include <stdio.h>

int main() {
    double money, max = 0, min = 1000000, total = 0;

    printf("输入今日开销,直到输入-1终止:\n");
    while (1) {
        scanf("%lf", &money);
        if (money == -1) {
            break;
        }
        if (money > max) {
            max = money;
        }
        if (money < min) {
            min = money;
        }
        total += money;
    }

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

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

任务六

源代码

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

 

标签:%.,int,money,random,实验,printf,include
From: https://www.cnblogs.com/shi1naii/p/18462199

相关文章

  • 实验1 现代C++编程初体验
    task1:代码:1//现代C++标准库、算法库体验2//本例用到以下内容:3//1.字符串string,动态数组容器类vector、迭代器4//2.算法库:反转元素次序、旋转元素5//3.函数模板、const引用作为形参67#include<iostream>8#include<string>9#inc......
  • 软件工程实验:结对编程与Git实战
    Lab1实验报告实验要求1.读入文本并生成有向图:将文本数据转换为有向图结构,各单词作为节点,有向边表示单词在文本中的相邻关系及其出现次数。2.展示有向图:图形化展示生成的有向图,并可保存为图形文件。3.查询桥接词:查询两个单词之间的桥接词,即图中存在两条边word1→word3和......
  • 实验1 现代C++编程初体验
    实验一:1//现代C++标准库、算法库体验2//本例用到以下内容:3//1.字符串string,动态数组容器类vector、迭代器4//2.算法库:反转元素次序、旋转元素5//3.函数模板、const引用作为形参6#include<iostream>7#include<string>8#include<vector>9......
  • 实验2
    任务1:1#include<stdio.h>2#include<stdlib.h>3#include<time.h>45#defineN56#defineN13977#defineN24768#defineN321910intmain(){11intcnt;12intrandom_major,random_no;1314srand(time......
  • 实验2 C语言分支与循环基础应用编程
    #include<stdio.h>#include<stdlib.h>#include<time.h>#defineN5#defineN1397#defineN2476#defineN321intmain(){intcnt;intrandom_major,random_no;srand(time(NULL));//以当前系统时间作为随机种子cnt=0;wh......
  • 实验2
    任务11#include<stdio.h>2#include<stdlib.h>3#include<time.h>45#defineN56#defineN13977#defineN24768#defineN321910intmain(){11intcnt;12intrandom_major,random_no;1314srand(time(......
  • 实验2
    任务1:源代码:1#include<stdio.h>2#include<stdlib.h>34#defineN55#defineN13976#defineN24767#defineN32189intmain()10{11intcnt;12intrandom_major,random_no;1314srand(time(NULL));//seed......
  • 实验1 现代C++编程初体验
    Task1code1.cpp1//现代C++标准库、算法库体验2//本例用到以下内容:3//1.字符串string,动态数组容器类vector、迭代器4//2.算法库:反转元素次序、旋转元素5//3.函数模板、const引用作为形参67#include<iostream>8#include<string>......
  • 【大数据技术基础 | 实验二】Linux基础:常用基本命令和文件操作
    文章目录一、实验目的二、实验要求三、实验环境四、常用基本命令1、验证cd和pwd命令2、验证ls命令3、验证mkdir命令4、验证cp、mv和rm命令五、Linux文件操作1、验证touch命令2、验证cat命令3、验证more命令六、实验心得一、实验目的学会linux常用命令(cd,ls,pwd......
  • 实验2 c语言分支与循环基础应用编程-1
    实验任务1task1.c1#include<stdio.h>2#include<stdlib.h>3#include<time.h>45#defineN56#defineN13977#defineN24768#defineN321910intmain(){11intcnt;12intrandom_major,random_no;1314s......