实验3

实验结论!

1.

 1 #include <math.h>
 2 #include <stdio.h>
 3 
 4 int main() {
 5     
 6     double a, b, c, x1, x2;
 7     double delta, real, imag;
 8     
 9     printf("Enter a, b, c: ");
10     
11     while(scanf("%lf%lf%lf", &a, &b, &c) != EOF) {
12         if(a == 0)
13             
14             printf("not quadratic equation.\n\n");
15         
16         else {
17             delta = b*b - 4*a*c;
18             if(delta >= 0) {
19                 x1 = (-b + sqrt(delta)) / (2*a);
20                 x2 = (-b - sqrt(delta)) / (2*a);
21                 printf("x1 = %.2f, x2 = %.2f\n\n", x1, x2);
22             }else {
23                 real = -b/(2*a);
24                 imag = sqrt(-delta) / (2*a);
25                 printf("x1 = %.2f + %.2fi, x2 = %.2f - %.2fi\n\n", real, imag, real, imag);
26             }
27         }
28         printf("Enter a, b, c: ");
29     }
30     
31     return 0;
32     
33 }

2.

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <time.h>
 4 #define N 5
 5 
 6 int main() {
 7     int x, n;
 8     srand(time(0)); // 以当前系统时间作为随机种子
 9     n = 0;
10     do {
11         n++;
12         x = rand()%10; // 生成一个0~9之间的随机整数
13         printf("%3d", x);
14     }while(n<N);
15     
16     printf("\n");
17     return 0;
18     
19 }

3.

 1 #include<stdio.h>
 2 int main(){
 3     int n=0;
 4     for(int i=101;i<201;i++){
 5         for(int j=2;j<i;j++){
 6             if(i%j==0)break;
 7             if(j==i-1){
 8                 printf("%5d",i);
 9                 n++;
10             }
11         }
12     }
13     printf("\n101到200之间共有21个素数\n");
14     return 0;
15 }

4.

 1 #include <stdio.h>
 2 #include<math.h>
 3 
 4 int main(int argc, const char * argv[]) {
 5     long a;
 6     printf("Enter a number:");
 7     while(scanf("%ld",&a)!=EOF){//令a代表输入的数
 8         
 9         long new=0;//构造中间数new
10         int n=0;//输出数的位数(从零开始)
11         while(a!=0){//如果a还不是0,则
12             if(a%2!=0){//如果个位是奇数
13                 new=new+pow(10,n)*(a%10);//那么将a的个位变成new的最高位
14                 n++;
15             }
16             a/=10;//砍去a的个位,进行下一轮循环
17         }//(总的来说,while执行的是:a由低到高依次削去各位数,与此同时将被削去的奇数由右向左排列,构成new)
18       
19         printf("new number is:%ld\n\n",new);//new横空出世
20         printf("Enter a number:");//下一回合
21     }
22     return 0;
23 }

算法思路见代码注释(括弧笑

5.

 1 #include <stdio.h>
 2 
 3 int factorial(int n);
 4 int main(int argc, const char * argv[]) {
 5     int n;
 6     printf("Enter n(1~10):");
 7     while(scanf("%d",&n)!=EOF){
 8         float sign=1;
 9         double sum=0;
10         for(int i=1;i<=n;i++){
11             sum+=sign/factorial(i);     //递增
12             sign=sign*(-1);             //改变系数符号
13         }
14     printf("n = %d, s = %lf\n\n",n,sum);
15     printf("Enter n(1~10):");
16         
17     }
18     return 0;
19 }
20 
21 
22 
23 int factorial(int n){                   //构造阶乘函数
24     int sum=1;
25     for(int i=1;i<=n;i++){
26         sum*=i;
27     }
28     return sum;
29 }

6.

 1 #include <stdio.h>
 2 #include<stdlib.h>
 3 #include<time.h>
 4 
 5 
 6 int main(int argc, const char * argv[]) {
 7     
 8     int day;
 9     srand(time(0));
10     printf("猜猜2020年12月哪一天会是你的忌日\n");
11     printf("开始咯,你有三次机会,猜吧(1~31):");
12     int n=rand()%31+1;
13     int life=3;
14 this:
15     scanf("%d",&day);
16     if(day>=1&&day<=31){                //输入正常
17         if(day==n){
18             printf("真是个小机灵鬼!你的忌日就在%d号哟\n",n);
19             return 0;
20         }
21         life--;
22         if(life==0){
23             printf("阿咧啊咧,次数用完啦。偷偷告诉你:12月,你的忌日是%d号\n",n);
24             return 0;
25         }
26         if(day>n) printf("你猜的日期晚了,忌日在这之前哟\n");
27         if(day<n) printf("你猜的日期也忒早了,忌日在这之后\n");
28         printf("再猜(1~31):");
29         
30         goto this;
31         
32     }else{                              //输入异常
33         printf("您老看不懂题目???重来!:");
34         goto this;
35     }
36     
37     return 0;
38 }

实验总结

好玩儿

了解了随机函数及其种子

更加熟悉了!=EOF

熟悉了ctrl+Z或D

更加熟悉pow()

学会了goto

posted @ 2020-11-15 10:59  是逸仙呀  阅读(124)  评论(3编辑  收藏  举报