实验3

//一元二次方程求解
//重复执行, 直到按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 eduation .\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;
} 

//生成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;
} 

//输出101~200之间所有素数,并输出这一区间内素数个数
#include <stdio.h>
#include <math.h>
int main() {
    int a, b, x, y;
    x = 0;
    
    for(a = 101; a <= 200; a++) 
    {
        b = sqrt(a);
        for(y = 2; y <= b; y++)
            if(a % y == 0)break;
            
        if(y > b)
        {
            printf("%5d", a);
            x++;
            if(x % 5 == 0)
            printf("\n");
        }
        
    }
    printf("\n");
    printf("100~200之间有%d个素数。", x);
    return 0;
}

//将一个长整型数s的每一数位上的奇数依次取出来,构成一个新的数,高位仍在高位,地位仍在低位
#include <stdio.h>
int main() {
    long long s, t, n, m;
    printf("Enter a number:");
    
    while(scanf("%d", &s) != EOF) {
    
    for(n = 1; s / n != 0; n *= 10);
    n /= 10;
    
    for(n >= 1; n /= 10;) {
        t = s / n % 10;
        if(t % 2 == 1)
        m = m * 10 + t;
    
    }   
    printf("New number is:%d", m);
}
    return 0;
}

//编写实验,实现从键盘上输入n(1<=n<=10),根据以下计算式计算s的值,并输出。
#include <stdio.h>
#include <math.h>
int main() {
    int n, i;
    double s = 0, b = 1;
    
    printf("Enter n(1~10):");
    
    while(scanf("%d", &n) != EOF) {
        for(i = 1; i <= n; i++, b *= i) {
            s = s + pow(-1, i + 1)/b;
        }
        printf("n = %d, s = %f", n, s);
    }
    return 0;
} 

//编写一个简单的猜日期程序
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
    int a, b, c;
    srand(time(0));
    a = rand() % 31 + 1;
    
    printf("猜猜2020年12月哪一天会是你的lucky day?\n");
    printf("开始罗,你一共有三次机会,在1~31里猜吧:");
    
    scanf("%d", &b);
    for(c = 1; c <= 3; c++) {
    if(c <= 2) {
        if(b < a) {
            printf("嘿嘿嘿你猜的日期早了哦,你的lucky day还没到呢\n");
            printf("再猜:");
            scanf("%d", &b);
    }
        else if(b > a) {
            printf("哎呀你猜晚啦,你的lucky day已经过了\n");
            printf("再猜:");
            scanf("%d", &b) ;
        }
        else
            printf("恭喜你猜对啦!"); 
    }
    else if(c = 3) {
        if(b < a) {
            printf("嘿嘿嘿你猜的日期早了哦,你的lucky day还没到呢\n");
            printf("次数用完啦,告诉你吧,你的lucky day是:%d", a);
        }
        else if(b > a) {
               printf("哎呀你猜晚啦,你的lucyk day已经过了\n");
            printf("次数用完啦,告诉你吧,你的lucky day是:%d", a);
        }
        else
            printf("恭喜你猜对啦!"); 
        }
    
    
    }
        
    return 0;
}

posted @ 2020-11-20 01:59  林静耶  阅读(126)  评论(0)    收藏  举报