实验3

//一元二次方程求解
//重复执行,直到按Ctrl+Z结束
//
#include<stdio.h>
#include<math.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;
} 

//生成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;
    {
        n++;
        x = rand()%10; //生成0~9之间的随机整数
        printf("%3d",x);
     } while(n<N);
     
     printf("\n");
     
     return 0;
}

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

#include<stdio.h>
#include<math.h>
int main() {
    long a;

    printf("Enter a number: ");
    while (scanf("%ld", &a) != EOF) {
        long b=0;
        int c, i = 0;
        while (a) {
            c = a % 10;
            
            if (c % 2){
                b += c * pow(10,i);
                
                i++;
            }a = a / 10;
            
        }
        printf("new number is:%ld\n\n", b);
        printf("Enter a number: ");
    }
    return 0;
}

#include<stdio.h>
#include<math.h>
int main() {
    int n;
    printf("Enter n(1~10): ");
    while (scanf("%d", &n) != EOF) {
        float x = 1.0;
        double s = 0.0;
        int m = 0;
        int i;
        for (i = 1; i <= n; i++) {
            x *= i;
            s += (1 / x) * pow(-1, m);
            m++;

        }
        printf("n=%d,s=%lf\n\n", n, s);
        printf("Enter n(1~10):");

    }
    return 0;
}

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

int main(){
    int x,date,i,a=0;
    srand(time(0));
    date=rand()%31;
    printf("猜猜2020年12月哪一天会是你的lucky day\n开始喽,你有三次机会,猜吧(1~31):"); 
    for(i=1;i<=3;i++){
        scanf("%d",&x);
        if(x>date)printf("\n你猜的日期晚了,lucky day悄悄溜到前面啦");
        else if(x<date) printf("\n你猜的日期早了,lucky day还没到呢");
        else {
            a=1;
            printf("\n你猜中了!");
            break;
        };
         
        if(i!=3)printf("\n再猜(1~31): "); 
        
    }
    if(a==0)printf("\n\n次数用完啦。偷偷告诉你:12月,你的lucky day是%d号",date);
    
    return 0;
}

posted @ 2020-11-19 00:08  华征  阅读(65)  评论(2)    收藏  举报