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

发布时间 2023-10-22 18:36:08作者: mdlmdl

task1

 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 srand( time(0) ); // 以当前系统时间作为随机种子
14 for(i = 0; i < N; ++i) {
15 number = rand()%(N2 - N1 + 1) + N1;
16 printf("202383290376%04d\n", number);
17 }
18 system("pause");
19 return 0;
20 }

问题1:计算得到学号后四位随机数

问题2:随机点学号

task2

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 int main()
 4 {char a;
 5     while(scanf("%c",&a)!=EOF)
 6      
 7     {getchar();switch(a){
 8      
 9       case 'r':printf("stop!\n");break;
10       case 'g':printf("go go go\n");break;
11       case 'y':printf("wait a minute\n");break;
12       default:printf("something must be wrong...\n");
13        }
14       }
15 
16 system("pause");
17 return 0;
18 }

 task3


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

 

 

 task4

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 int main()
 4 {
 5     int a, n, i;
 6     double s;
 7     
 8     
 9     while (scanf_s("%d%d", &n, &a) != EOF)
10     {s = 0.0;
11         int m;
12         
13         for (i = 1,m = a; i <= n; i++)
14         {
15             s += double(i) / double(m);
16             m = m * 11;
17             
18         }printf("n=%d a=%d s=%f\n", n, a, s);
19     }
20     
21     return 0;
22 }

多组输入时s=0要放在while里面,防止下一组数值执行时s是上一次的结果

task5

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

 task6

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