首页 > 其他分享 >实验2

实验2

时间:2024-10-10 22:32:31浏览次数:1  
标签:%. int else num 实验 printf include

试验任务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("20248329%04d\n",random_no);
    }
    
    cnt++;
    }
    return 0;
}

问题一:随机获得N1到N2之间的一个三位数

问题二:随机生成1到21之间的一个整数

问题三:在11班,12班,奇安信班所有学号中随机生成一个学号

实验任务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 = 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;
}

 

实验任务3

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

 

实验任务4

 1 #include<stdio.h>
 2 int main()
 3 {
 4     float sum=0;
 5     float num;
 6     float min=20000;
 7     float max=0;
 8     
 9     printf("ÊäÈë½ñÈÕ¿ªÏú£¬Ö±µ½-1ÖÕÖ¹\n"); 
10      
11         while(scanf("%f",&num)!=EOF){
12         
13         
14         if (num!=-1)
15         {
16         
17         sum+=num;
18         
19         if(num>max)
20         {
21         max=num;
22         }
23         else
24         {
25         max=max;
26         }
27         if(num<min)
28         {
29         min=num;    
30         }
31         else
32         {
33         min=min;
34         }
35           continue;
36         }
37         
38         else
39         {
40         break;
41         }
42         }
43         printf("½ñÈÕÀÛ¼ÆÏû·Ñ£º%.1f\n",sum); 
44             printf("½ñÈÕ×î¸ß¿ªÏú£º%.1f\n",max);
45             printf("½ñÈÕ×îµÍ¿ªÏú£º%.1f\n",min);
46         
47     return 0;
48     
49 }

 

实验任务5

 1 #include<stdio.h>
 2 int main()
 3 {   
 4     int a,b,c;
 5     while(scanf("%d%d%d",&a,&b,&c)!=EOF){
 6         if(a+b<=c||b+c<=a||a+c<=b)
 7         {
 8             printf("²»Äܹ¹³ÉÈý½ÇÐÎ"); 
 9         }
10         else if(a==b&&b==c)
11         {
12             printf("µÈ±ßÈý½ÇÐÎ"); 
13         }
14         
15         
16         
17         else if(a==b||a==c||b==c)    
18         {
19             printf("µÈÑüÈý½ÇÐÎ"); 
20         }
21         
22         else if (a*a+b*b==c*c||a*a==b*b+c*c||b*b==a*a+c*c)
23         {
24             printf("Ö±½ÇÈý½ÇÐÎ"); 
25         }
26         
27         
28         
29         else
30         printf("ÆÕͨÈý½ÇÐÎ"); 
31         
32     }
33     
34     return 0;
35         
36         
37     
38 }

 

 

 

实验任务6

 1 int date;
 2     #define N1 30
 3     int guess;
 4     int cnt;
 5     srand(time(NULL));
 6     
 7     date=rand()%30+1;
 8     printf("²Â²Â2024Äê11ÔÂÄÄÒ»Ìì»áÊÇÄãµÄlucky day\n");
 9     printf("¿ªÊ¼¿©£¬ÄãÓÐÈý´Î»ú»á£¬²Â°É(1~30):\n");
10     cnt=0;
11     
12     while(cnt<3){
13         
14         scanf("%d",&guess);
15         cnt++;
16         
17         if(guess<date)
18         {
19             printf("Äã²ÂµÄÈÕÆÚÔçÁË£¬ÄãµÄlucky day»¹Ã»µ½ÄØ\n");
20             printf("Ôٲ£¨1~30£©£º\n");
21             continue;
22             
23         }
24         
25         else if(guess>date)
26         {
27             printf("Äã²ÂµÄÈÕÆÚÍíÁË£¬ÄãµÄlucky dayÔÚÇ°ÃæÄØ\n");
28             printf("Ôٲ£¨1~30£©£º\n");
29             continue;
30         }
31         
32         else if(guess==date)
33         {
34             printf("ÍÛ£¬²ÂÖÐÁË\n");    
35             break;
36         }
37     
38             
39     }
40         while(cnt==3)
41         {
42             printf("´ÎÊýÓùâÀ²£¬ÍµÍµ¸æËßÄ㣬11ÔÂÄãµÄlucky dayÊÇ%d",date);
43             break;
44         }
45         return 0;
46 }

 

标签:%.,int,else,num,实验,printf,include
From: https://www.cnblogs.com/88888888whq/p/18455405

相关文章

  • 实验1 现代C++编程初体验
    1.实验任务1task1源代码:1#include<iostream>2#include<string>3#include<vector>4#include<algorithm>56usingnamespacestd;78//声明9//模板函数声明10template<typenameT>//*****表明这是一个模板参数,可以接受任意类型的参数......
  • 20222428 2024-2025-1 《网络与系统攻防技术》实验一实验报告
    1.实验内容1.1本周学习内容1.1.1安全漏洞简介定义:系统的缺陷或不足。作用:网络攻击和防御的关键点。网络攻击:利用系统漏洞进行攻击。防御:提前发现漏洞并修复。exploit:完整的代码攻击。shellcode:不完整的代码攻击。PoC:验证是否存在漏洞。1.1.2缓冲区溢出的定义和原因......
  • 实验1
    实验任务1:task1.cpp:1//现代C++标准库、算法库体验2//本例用到以下内容:3//1.字符串string,动态数组容器类vector、迭代器4//2.算法库:反转元素次序、旋转元素5//3.函数模板、const引用作为形参67#include<iostream>8#include<string>......
  • 20222304 2024-2025-1 《网络与系统攻防技术》实验一实验报告
    1.实验内容1.1本周学习内容1)反汇编反汇编是指将计算机程序的机器代码转换回其相应的汇编代码的过程。在计算机编程和逆向工程领域中,反汇编是一种常见的技术,用于理解和分析二进制程序的功能和内部结构。通常情况下,程序员编写的源代码会被编译器转换成机器码,这是计算机可以直接......
  • 20222321 2024-2025-1 《网络与系统攻防技术》实验一实验报告
    一.实验内容1实验目标本次实践的对象是一个名为pwn1的linux可执行文件。该程序正常执行流程是:main调用foo函数,foo函数会简单回显任何用户输入的字符串。该程序同时包含另一个代码片段,getShell,会返回一个可用Shell。正常情况下这个代码是不会被运行的。我们实践的目标就是想......
  • 实验1
    #include<algorithm>//#include<typeinfo>//#include<iomanip>//#include<map>#include<vector>#include<string>#include<iostream>usingnamespacestd;usingstd::string;//模板函数声明template<typenameT&g......
  • 20222411 2024-2025-1 《网络与系统攻防技术》实验一实验报告
    1.实验内容1.1基础知识1.1.1NOP,JNE,JE,JMP,CMP汇编指令的机器码(1)NOP:NOP指令即“空指令”。执行到NOP指令时,CPU什么也不做,仅仅当做一个指令执行过去并继续执行NOP后面的一条指令。(机器码:90)(2)JNE:条件转移指令,如果不相等则跳转。(机器码:75)(3)JE:条件转移指令,如果相等则跳转。(......
  • 实验2
    TASK11#include<stdio.h>2#include<stdlib.h>3#include<time.h>45#defineN56#defineN13977#defineN24768#defineN321910intmain(){11intcnt;12intrandom_major,random_no;1314srand(ti......
  • 实验1 现代C++基础编程
    任务1:源代码task1.cpp1#include<iostream>2#include<string>3#include<vector>4#include<algorithm>56usingnamespacestd;78template<typenameT>9voidoutput(constT&c);1011voidtest1();12void......
  • 实验1 现代c++编程初体验
    任务1:task1.cpp1//现代C++标准库、算法库体验2//本例用到以下内容:3//1.字符串string,动态数组容器类vector、迭代器4//2.算法库:反转元素次序、旋转元素5//3.函数模板、const引用作为形参67#include<iostream>8#include<string>......