实验三

实验任务一

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define N 5

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

实验三

实验任务二

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

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

实验三

实验任务三

#include<stdio.h>

int main() {
    unsigned long x,t,p;
    while(printf("Enter a number:\n"), scanf("%u",&x)!=EOF) {
        t=0,p=1;
        while(x!=0){
        if((x%10)%2!=0)
        {
           t=t+(x%10)*p;
           p=p*10;    
        }
        x=x/10;
    }
    printf("New number is:%u\n",t);
    
    }
    
    return 0;
} 

实验三

实验任务四

#include<stdio.h>
#include<math.h>
void solve(double a, double b, double c);

int main() {
    double a,b,c;
    
    printf("Enter a,b,c:");
    while(scanf("%lf%lf%lf",&a, &b, &c)!=EOF){
        solve(a,b,c);
        printf("Enter a,b,c:");
    }
    
    return 0;
} 
void solve(double a, double b, double c) {
    double x1, x2;
    double delta, real, imag;
    
    if(a==0)
       printf("not quadratic equation.\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", x1, x2);
        }
        else{
            real = -b/(2*a);
            imag = sqrt(-delta)/(2*a);
            printf("x1 = %.2f+%.2fi, x2 = %.2f-%.2fi\n", real, imag, real, imag);
        }
    }   
}

实验三

不能以返回值的形式返回给主调函数,因为函数每次只能有一个返回值,无法将solve函数中 x1,x2均返回至主函数

实验任务五

#include<stdio.h>
double fun(int n);

int main(){
    int n;
    double s;

    while(printf("Enter n(1~10):"),scanf("%d", &n) != EOF){
        s = fun(n);
        printf("n = %d, s = %f\n\n", n, s);
    }

    return 0;
}

double fun (int n){
    int i,z,f=1,design=1;
    double b,s;
    for(i=1,s=0;i<=n;i++,f=1)
    {
    for(z=i;z>0;--z){
        f=f*z;
    }    
        b=design*1.0/f; 
        s=s+b;
        design=-design;
    }
    return s;
}

实验三

实验任务六

#include<stdio.h>
#include<math.h>
int isPrime(int n);

int main(){
    int N, n;
    for(n = 101,N = 1;n<200;n++){

        if (isPrime(n))
        {
        printf("%d ", n);
        N++;
         if(N%6 == 0)
         {
         printf("\n");
         N = 1;
         }
        }
    }
    return 0;
}

int isPrime(int n){
    int k;
    for(k=2;k<=sqrt(n);k++)
        if(n%k==0)
           return 0;
        return 1;   
    }
   

实验三

上一篇:实变函数例题(4)


下一篇:为Anaconda python3安装gi模块