首页 > 编程语言 >实验2_C语言分支与循环基础应用编程

实验2_C语言分支与循环基础应用编程

时间:2024-10-09 21:02:51浏览次数:1  
标签:random int 编程 C语言 else %. 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;
}

 

line21 生成N2-N1+1~N1之间的随机数

line25生成N3~1间的随机数

该程序生成随机学号

实验二

#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 i;
    while(scanf("%c",&i)!=EOF){
        if(i=='r'){
            printf("stop!\n");
        }else if (i=='g'){
            printf("go go go\n");
        }else if(i=='y'){
            printf("wait a minute\n");    
        }else{
            printf("something must be wrong...\n");
        } 
        getchar();  
    }   
    return 0;
}

实验四

#include<stdio.h>
int main()
{
    int t,max,min;
    int sum=0;
    max=0;
    min=20000;
    do
    {
        scanf("%d",&t);
        if (t>=0&&t<=20000)
        {
            sum+=t;
        }
        if (t>max){
            max=t;
        }
        if (t<min){
            min=t;
        }
    }while(t!=-1);
    printf("今日累计消费总额:%d\n今日最高一笔开销:%d\n今日最低一笔开销:%d\n",sum,max,min);
    return 0;
}

实验五

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

实验六

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

 

标签:random,int,编程,C语言,else,%.,printf,include,分支
From: https://www.cnblogs.com/water202483290431/p/18455128

相关文章

  • 实验2 C语言分支与与循环基础应用编程——1
    一、实验目的1.能正确使用if语句实现分支结构2.能正确使用while语句、do...while语句实现循环结构3.能在具体问题场景中正确区分、使用continue和break4.能灵活、组合使用c语句编程解决简单应用问题 二、实验准备1.分支语句if和循环语句while、do...while的用法......
  • 实验1 现代C++编程初体验
    task1:1//现代C++标准库、算法库体验2//本例用到以下内容:3//1.字符串string,动态数组容器类vector、迭代器4//2.算法库:反转元素次序、旋转元素5//3.函数模板、const引用作为形参67#include<iostream>8#include<string>9#includ......
  • 实验1 现代C++编程初体验
    任务一源代码1#include<iostream>2#include<string>3#include<vector>4#include<algorithm>56usingnamespacestd;78template<typenameT>9voidoutput(constT&c);1011voidtest1();12voidtest2();13v......
  • 实验1 现代C++编程初体验
    1.实验任务11#include<iostream>2#include<string>3#include<vector>4#include<algorithm>56usingnamespacestd;78//声明9//模板函数声明10template<typenameT>11voidoutput(constT&c);1213//普通函数声明......
  • 实验二 C语言分支与循环基础应用编程
    实验二C语言分支与循环基础应用编程实验任务1——抽学号#include<stdio.h>#include<stdlib.h>#include<time.h>#defineN5#defineN1397#defineN2476#defineN321intmain(){ intcnt; intrandom_major,random_no; srand(time(NULL));//以当前系统......
  • C#联合Visionpro编程学习记录(判断相机硬件是否掉线的方法)
    1,在实际使用过程中,Visionpro没有提供用于直接判断相机硬件是否依然在线的方法,有一个方法可以使用:1///<summary>2///使用获取相机时间戳计时器频率的方式来判断相机是否仍然在线,3///如果相机掉线获取相机TimeStampFrequency属性将报错,以此判断相机......
  • C#联合Visionpro编程学习记录(将指定颜色的十字线图形添加到CogRecordDisplay上)
    1///<summary>2///将指定颜色的十字线图形添加到CogRecordDisplay上3///</summary>4///<paramname="icogimage"></param>5///<returns></returns>6publicstaticstringAddCrossCurveRecord2CogRecordDisplay(I......
  • C语言结构体
    1.结构体的定义、初始化、打印输出#include<stdio.h>#include<string.h>structStudent{//定义结构......
  • 【蓝桥杯】“萌新首秀”全国高校新生编程排位赛3
    一、下一次生日题目下一次生日 题目分析闰年,四年一次,今年是闰年,那下一个闰年就是四年后代码#includeusingnamespacestd;intmain(){cout<<"2028";return0;}二、遗失的数字题目遗失的数字  题目分析用一个数组来记录数组A[N]出现的数字,如果......
  • C++消灭星星游戏编程【目录】
    欢迎来到zhooyu的专栏。主页:【zhooyu】专栏:【C++消灭星星游戏编程】特色:【保姆级教程,含每一课程源码】致力于用最简洁的语言,最简单的方式,最易懂的知识,带大家享受编程的快乐。消灭星星游戏编程演示效果消灭星星游戏编程演示效果本专栏内容:消灭星星的小游戏保姆......