实验3

1.实验任务1

//一元二次方程求解
//重复执行,直到按Ctrl+Z结束
//
#include <math.h>
#include <stdio.h>

int main() {
    float a, b, c, x1, x2;
    float delta, real, imag;
    
    printf("Enter a, b, c:  ");
    
    while(scanf("%f%f%f", &a, &b, &c) != EOF) {
        if(a == 0)
            printf("not quadratic equation.\n\n");
        else{
            delta = b*b - 4*a*c;
            
            if(delta >= 0) {
                x1 = (-b + sqrt(delta)) / (2*a);
                x2 = (-b - sqrt(delta)) / (2*a);
                printf("x1 = %.2f, x2 = %.2f\n\n", x1, x2);
            }
            else{
                real = -b/(2*a);
                imag = sqrt(-delta) / (2*a);
                printf("x1 = %.2f + %.2fi, x2 = %.2f - %.2fi\n\n", real, imag, real, imag);
            }
        }
        printf("Enter a, b, c: ");
    }
    
    return 0;
}

2.实验任务2

// 生成N个0~9之间的随机整数,并打印输出
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define N 5

int main() {
    int x, n;
    
    srand(time(0));
    n = 0;
    do {
        n++;
        x = rand()%10;
        printf("%3d", x);
    }while(n<N);
    
    printf("\n");
    
    return 0; 
}

3.实验任务3

#include <stdio.h>
#include <math.h>
int main() {
    int x, y, z;
    for (x = 101; x <= 200; x++){
        for(y = 2; y <= sqrt(x); y++)
        if(x % y == 0)
        break;
        if(y > sqrt(x)){
            z++;
            printf("%5d", x);
            if(z % 5 == 0)
            printf("\n");
        }
    }
    printf("\n101~200之间共有%d个素数。", z);
    return 0;
}

4.实验任务4

 

#include <stdio.h>
int main(){
    long int s, p = 1, t = 0;
    printf("Enter a number: ");
    while(scanf("%ld", &s)!=EOF)
    {
        if((s % 10) % 2 != 0)
        {
            t = t + (s % 10) * p;
            p = p * 10;
            s = s / 10;
        }
        printf("new number is: %ld\n", t);
        printf("Enter a number: ");
    }
    return 0;
}

 

SOS  只能显示出最后一位的奇数怎么办?

搞了好久了

 

5.实验任务5

#include <stdio.h>
int main(){
    int n;
    printf("Enter n(1~10): ");
    while(scanf("%d", &n)!=EOF){
        int t, f, T;
        f = 1; T = 1;
        double s = 0.0;
        for (t = 1;t <= n; t++){
            T = t * T;
            s = s + f * (1.0 / T);
            f = f * (-1);
        }
        printf("n = %d, s = %f\n\n", n, s);
        printf("Enter n (1~10): ");
    }
}

6.实验任务6

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
    printf("猜猜那一天会是你的lucky day\n\n开始咯,你有三次机会,猜吧:\n"); 
    int a,b,i;
    srand(time(0));
    a = rand() % 31;
    
    for(i=1;i<=3;i++)
    {
        if(i >= 2)
        {
            printf("再猜一次吧:");
        }
        scanf("%d", &b);
        if(b < a)
        {
            printf("你猜的日期早了,lucky day还没到呢:)\n\n");
        }
        if(b > a)
        {
            printf("你猜的日期晚了,lucky day悄悄溜到前面去啦:)\n\n");
        }
        if(b == a)
        {
            printf("Bingo~");
            break;
        }
        if(i == 3)
        {
            printf("次数用完啦,悄悄告诉你,你的lucky day是%d号",a);
        }
    }
    return 0;
}

 

 

(实验四格式一直出错,暂时先不提交。)

(交上了交上了  但还是错……)

posted @ 2020-11-18 15:07  李青宇  阅读(80)  评论(2)    收藏  举报