exp3

实验任务1

//exp3-1
#include<stdio.h>
#include<math.h>


int main()
{
    double a, b, c, x1, x2;
    double delta, real, imag;
    printf("输入a,b,c : ");
    while (scanf_s("%lf%lf%lf",&a,&b,&c)!=EOF)
    {
        if (a==0)
        {
            printf("输入的数据无效");
        }
        else
        {
            delta = b * b - 4 * a * c;
            if (delta >= 0)
            {
                x1 = (-b + sqrt(delta)) / (2 * a);
                x2 = (-b - sqrt(delta)) / (2 * a);
                printf("x1 = %.2lf,x2 = %.2lf\n", x1, x2);
            }
            else
            {
                real = -b / (2 * a);
                imag = sqrt(-delta) / (2 * a);
                printf("x1 = %.2lf+%.2lfi\n", real, imag);
                printf("x2 = %.2lf-%.2lfi\n", real, imag);
            }

        }
    printf("输入a,b,c : ");
        
        

    }



    return 0;

}

 

 

实验任务2

 

//exp3-2
#include<stdlib.h>
#include<stdio.h>
#include<time.h>
#define N 5
int n,x;
int main()
{
    n = 0;
    do
    {
        n++; 
        srand(time(NULL));
        x = rand() % 10;
        printf("%3d",x);

    } while (n<N);
    system("pause");
    return 0;
}

 

 

 实验任务3

#include<stdio.h>
#include<math.h>
#include<stdlib.h>


int main()
{
    int a = 101, b = 1,c = 2,shu=1;
    while(b <=100)
    {
        while (c < a)
        {
            if (a % c != 0)
            {
                c++;
            }
            else
            {
                break;
            }
        }

        if (c == a)
        {
            if(shu<=5)
            {
                printf("\t%d", a);
                a++;
                b++;
                c = 2;
                shu++;
            }
            else
            {
                printf("\n\t%d", a);
                a++;
                b++;
                c = 2;
                shu = 2;


            }
            
        }
        else
        {
            a++;
            b++;
            c = 2;
        }
    }
    system("pause");
    return 0;
}

 

实验任务4

 


#include<stdio.h>
#include<stdlib.h>

 


int main()
{
  int result = 0, n = 1,t = 1 ;
  long s ;
  printf("请输入一个长整型整数: ");

  while (scanf_s("%ld", &s) != EOF)
  {

    while (s / t >= 1)
    {
      if ((s / t % 10) % 2 != 0) //此处判断抽取出来的数是否为奇数
      {
        result += (s / t % 10) * n;
        n *= 10;
      }
      t *= 10;
    }
    printf("结果是: %ld\n", result);
    t = 1;
    result = 0;
    n = 1;
    printf("请输入一个长整型整数: ");
  }

  return 0;
}


算法思路:

要取出一个十进制多位数的整数上的奇数,首先要取出每一位上的数字再进行判断是否为奇数,取出的方法为:原整数除以要取出的位数的权重,再对10取余,所以我们定义了  t  作为权重,每次取出数字判断完后权重  t  乘 10 ,继续下一次的取数,若(原数/t)< 1  ,则说明数已经全部取完。

(数是从低位向高位取)

关于如何保证低位继续在低位,使用累加的方法可以达到理想效果:如代码所示,每次取到奇数时n*10,那么下次取到奇数时的数就在上一个数的前一位。

 

 

实验任务5

 

#include<stdio.h>
#include<math.h>
#include<stdlib.h>


int main()
{
    double s = 0 , x = -1, t = 1,sum = 1 ;
    int n;
    printf("请输入正整数n:");
        while(scanf_s("%d", &n)!= EOF)
        {
            if (1 <= n && n <= 10)
            {
                while (t <= n)
                {
                    s = s + pow(x, t - 1) * sum;
                    t++;
                    sum = sum * (1 / t);


                }
                printf("%lf\n", s);

            }

            printf("n的范围是:1<=n<=10\n");
            printf("请输入正整数n:");
        }


}

 

实验任务6

 

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<time.h>
 4 
 5 int x,t=3,a;
 6 int main()
 7 {
 8     srand(time(NULL));
 9     x = 1 + rand() % 31;
10     printf("猜猜2020年12月哪一天是你的幸运日?\n");
11     printf("你有三次机会,猜吧: ");
12     
13     while (scanf_s("%d", &a)<t)
14     {
15         if (x < a)
16         {
17             printf("你猜的日期晚了,lucky day跑到前面去了\n");
18             t--;
19             printf("再猜(1~31):");
20         }
21 
22         if (x > a)
23         {
24             printf("你猜的日期早了,lucky day跑到后面去了\n");
25             t--;
26             printf("再猜(1~31):");
27         }
28 
29         if (x == a)
30         {
31             break;
32         }
33     }
34     if (t == 1)
35     {
36         printf("次数用完了哦,你的幸运日其实是%d日啦。", x);
37         system("pause");
38     }
39     else
40     {
41         printf("你猜对啦,你的lucky day就是%d日!",x);
42 
43         system("pause");
44     }
45 
46     return 0;
47 }

 

 

posted @ 2020-11-15 22:01  kagiii  阅读(163)  评论(2编辑  收藏  举报