• 实验三


    任务一

    #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.
    
    ");
            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
    
    ", x1, x2);
                }
                else{
                    real = -b / (2*a);
                    imag = sqrt(-delta) / (2*a);
                    printf("x1 = %.2f + %.2fi, x2 = %.2f - %.2fi
    
    ", real, imag, real, imag);
                }
            }
            
            printf("Enter a, b, c:  ");
        }
        
        return 0;
    }

    任务二

    #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("
    "); 
        
        return 0;
    }

    任务三

    #include<stdio.h>
    #include<math.h>
    
    int main()
    {
        int N, n;
        int count = 0; 
        
        for(N = 101; N <= 200; N++)
        {
            for(n = 2; n <= sqrt(N); n++)
            {
                if(N%n == 0) 
                    break;        
            }
            if(n > sqrt(N)) 
            {
                printf("%4d", N);
                
                count++;//统计素数的个数 
                
                if(count%5 == 0)// 每行输出五个数字 
                    printf("
    ");
            }
        }
        printf("
    ");
        printf("101~200之间有%d个素数
    ", count);
        
        return 0;
    } 

    任务四

    #include<stdio.h>
    #include<math.h> 
    
    int main() 
    {    
        long int s;
        
        printf("Enter a number:");
        
        while(scanf("%ld", &s) != EOF) 
        {         
            int i = 1;
            long int k = 0;
            int m = 0;
            
            printf("new number is:");
            
            while(s)  //当s不为0时,表达式为真,执行循环 
            {
                i = s%10;  //取s最后一位 
                s /= 10; 
                if(i%2 == 0) 
                    continue;    //跳过偶数 
                k += pow(10,m)*i;
                m++; //通过计算循环的次数来确定奇数的位置 
            } 
            printf("%ld", k);
            printf("
    
    "); 
            printf("Enter a number:");
        }
    
        return 0;
    }

    任务五

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

    任务六

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

  • 相关阅读:
    Python字符串的切片
    Python字符串的劈分
    Python函数的参数
    接口幂等性
    Kafka 中的设计思想
    Stream特性
    API接口的安全设计验证:ticket,签名,时间戳
    linux命令
    Java注解和反射
    su 和 sudo
  • 原文地址:https://www.cnblogs.com/wjxing/p/13997579.html
Copyright © 2020-2023  润新知