首页 > 其他分享 >实验2

实验2

时间:2023-10-22 11:58:01浏览次数:34  
标签:main int break 实验 printf N1 include

实验任务1

代码

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<time.h>
 4 
 5 #define N 5
 6 #define N1 374
 7 #define N2 465
 8 
 9 int main()
10 {
11     int number;
12     int i;
13     
14     srand(time(0));
15     
16     for(i = 0; i < N; ++i) {
17         number = rand()%(N2 - N1 + 1) + N1;
18         printf("202383290376%04d\n", number);
19     }
20     
21     return 0;
22  } 

 

运行结果

 问题1答案

rand()函数会生成一个0到(N2-N1+1)范围的随机整数,然后加上N1,这样就得到了在闭区间[N1,N2]内的随机整数

问题2答案

 生成5个随机符号,尾号范围在374到465之间

实验任务2

代码

 1 #include<stdio.h>
 2 
 3 int main()
 4 {
 5     char input;
 6     
 7     while(1){
 8         printf("请输入交通信号灯的颜色:");
 9         int result = scanf("%c",&input);
10         
11         if(result == EOF){
12             break;
13         }
14         else if (result == 1){
15             switch(input){
16                 case'r':
17                     printf("stop!\n");
18                     break;
19                 case'y':
20                     printf("wait a minute\n");
21                     break;
22                 case'g':
23                     printf("go go go\n");
24                     break;    
25                 default:
26                     printf("something must be wrong...\n");
27                     break;                
28             }
29         }
30             
31                 getchar();    
32     
33     }
34     
35     return 0;
36 }

 

运行结果

 

实验任务3

代码

 

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<time.h>
 4 
 5 int main(){
 6     int luckyday = 0;
 7     int guess;
 8     int chances = 3;
 9     
10     srand(time(0));
11     luckyday = rand() % 30 + 1;
12     
13     printf("猜猜2023年11月哪一天是你的lucky day\n");
14     printf("开始喽,你有三次机会,猜吧(1~30):");
15     
16     while(chances > 0){
17         scanf("%d",&guess);
18         
19         if (guess == luckyday){
20             printf("哇,猜中了:-)\n");
21             break;
22         }
23         else if (guess < luckyday){
24             printf("你猜的日期早了,你的lucky day还没到呢\n");
25         }
26         else{
27             printf("你猜的日期晚了,你的lucky day已经过啦\n");    
28         }
29         
30         chances--;    
31         
32         if(chances > 0){
33             printf("再猜(1~30):");
34         }
35         else{
36             printf("次数用完啦。偷偷告诉你:11月,你的lucky day是%d号\n",luckyday);
37         }
38     }
39         return 0;
40     }

 

运行结果

 

实验任务4

代码

 

 1 #include<stdio.h>
 2 
 3 int main(){
 4     int n,a,i,k;
 5     double s;
 6     while(scanf("%d%d",&n,&a)!=EOF){
 7         s = 0;
 8         k = 0;
 9         for(i = 1;i <= n;i++){
10             k = 10*k + a;
11             s += 1.0*i/k;
12         }
13         printf("n = %d,a = %d,s = %lf\n\n",n,a,s);
14    
15     }   
16     return 0;
17 }

 

运行结果

 

实验任务5

代码

 1 #include<stdio.h>
 2 int main(){
 3     int i,j;
 4     for(i = 1;i <= 9;i++){
 5         for(j = 1;j <= i;j++){
 6             printf("%d*%d = %d ",i,j,i*j);
 7         }
 8     printf("\n");
 9     } 
10     return 0;
11 }

运行结果

 实验任务6

代码

 1 #include<stdio.h>
 2 int main(){
 3     int n;
 4     int i,j,k,l,f;
 5     scanf("%d",&n);
 6     for(i = 1;i <= n;i++){
 7         for(f=1;f<=i-1;f++){
 8             printf("\t");
 9         }
10     for(j = 1;j <= 2*n-1-2*(i - 1);j++){
11             printf(" O \t");
12         }
13   
14     printf("\n");
15   
16     for(f=1;f<=i-1;f++){
17         printf("\t");
18         }
19     for(k = 1;k  <= 2*n-1-2*(i - 1);k++){
20         printf("<H>\t");    
21         }
22      
23     printf("\n");
24 
25     for(f=1;f<=i-1;f++){
26         printf("\t");
27         }
28     for(l = 1;l  <= 2*n-1-2*(i - 1);l++){
29         printf("I I\t");    
30         }
31     printf("\n");     
32     }     
33     return 0;
34 }

运行结果

 

标签:main,int,break,实验,printf,N1,include
From: https://www.cnblogs.com/chenziming114514/p/17767601.html

相关文章

  • 实验课二
    一、task1    1,源代码1#include<stdio.h>2#include<stdlib.h>3#include<time.h>45#defineN56#defineN13747#defineN246589intmain()10{11intnumber;12inti;13srand(time(0));1415for(i......
  • 实验2 类和对象
    实验任务3Complex.hpp#pragmaonce#include<iostream>#include<cmath>classComplex{public:Complex(doubler=0,doublei=0){real=r;imag=i;}Complex(constComplex&x){real=x.real......
  • 实验2
    实验任务1#include<stdio.h>#include<stdlib.h>#include<time.h>#defineN5#defineN1374#defineN2465intmain(){intnumber;inti;srand(time(0));for(i=0;i<N;++i){number=rand()%(N2......
  • 实验2 C语言分支与循环基础应用编程
    实验任务1#include<stdio.h>#include<stdlib.h>#include<time.h>#defineN5#defineN1374#defineN2465intmain(){intnumber;inti;srand(time(0));for(i=0;i<N;++i){number=rand()%(N2-N1+1)+N1;pr......
  • 实验2 C语言分支与循环基础应用编程
    实验任务1源代码1#include<stdio.h>2#include<stdlib.h>3#include<time.h>45#defineN56#defineN13747#defineN246589intmain()10{11intnumber;12inti;1314srand(time(0));1516......
  • 实验二
    实验任务3:1.complex.hpp1#pragmaonce2#include<iostream>3#include<cmath>4classComplex{public:5Complex(doubler=0,doublei=0){6real=r;imag=i;}7Complex(constComplex&x){8real=x.real;......
  • 实验二
    Task1: 1#include<stdio.h>2#include<stdlib.h>3#include<time.h>45#defineN56#defineN13747#defineN246589intmain()10{11intnumber;12inti;13srand(time(0));1415for(i=0;i<N;......
  • 实验二
    1.test1#include<stdio.h>#include<stdlib.h>#include<time.h>#defineN5#defineN1374#defineN2465intmain(){intnumber;inti;srand(time(0));for(i=0;i<N;++i){number=rand()%......
  • 实验2 C语言分支与循环基础应用编程
    1.实验任务1task1源代码1#include<stdio.h>2#include<stdlib.h>3#include<time.h>45#defineN56#defineN13747#defineN246589intmain()10{11intnumber;12inti;1314srand(time(0));//以当前......
  • 实验2
    实验任务1问题1:生成随机数字问题2:可随机生成学号实验任务2实验任务3 实验任务4实验任务5实验任务6 ......