实验三

#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() % 32; printf("%3d", x); } printf("\n"); return 0; }

实验2

 

#include<stdio.h>
#include <stdlib.h>
#include <time.h> 
int main(){
    int n,x,i;
    printf("猜猜2021年5月哪一天会是你的luck day\n");
    printf("开始了,你有三次机会,猜吧(1`31):");
    srand(time(0));
    x=rand()%32;
    i=1;
while(i<=3){
    scanf("%d",&n);
    if(n!=x){
        if(n>x){
            printf("你猜的日期晚了,luck day溜到前面了\n"); 
        }
        else
        printf("你猜的日期早了,luck day还没到\n");
        if(i!=3)
        printf("再猜:");
        i++; 
    }
    else {printf("right");
    break;
    }
}
printf("次数用完了,5月你的lucy days是%d号",x);
return 0;

}

实验3

#include<stdio.h>
#include<math.h> 
int main(){
    long  n,x,s,b,c;
    while(printf("enter a number:"),scanf("%ld",&n)!=EOF){
        b=0;
        s=0;
        x=n;
        while(x!=0){
            c=x%10;
            x=x/10;
            if(c%2!=0)
            {
            s=s+pow(10,b)*c;
            b++;
            }
        }
        printf("new number:%ld\n",s);
        
    }
return 0;
    
}

实验4

// 一元二次方程求解(函数实现方式)
// 重复执行, 直到按下Ctrl+Z结束 

#include <math.h>
#include <stdio.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;
}

// 函数定义
// 功能:求解一元二次方程,打印输出结果
// 形式参数:a,b,c为一元二次方程系数 
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函数后添加return(表达式),从而在主函数中输出。

实验5

#include <stdio.h>
double fun(int n); 
 
int main() {
    int n;
    double s;
    
    printf("Enter n(1~10): ");
    while(scanf("%d", &n) != EOF) {
        s = fun(n);  
        printf("n = %d, s= %f\n\n", n, s);
        printf("Enter n(1~10): ");
    }
    
    return 0;
}


double fun(int n) {
    double p,s;
    int k;
    k=1;
    p=-1;
    s=0;
    while(k<=n){
        p=-1*p/k;
        s=s+p;
        k++;
    }
    return(s);
    
    
    
}

实验6

#include<stdio.h>
#include<math.h>
int isprime(int a);
int main(){
    int p,b;
    p=101;
    b=0;
    while(p<=200){
        if(isprime(p)==1){
            printf("%4d",p);
            b=b+1;
            if(b%5==0){
            printf("\n");
        }
        
        }
        p=p+1;
        
    }
    printf("\n100~200素数个数为:%d",b);
}

int isprime(int a){
    int i;
    double m;
    m=sqrt(a);
    for(i=2;i<=m;i++){
        if(a%i!=0){
        }
        else if(i>m){
            return 1;
        }
        else return 0;
    
        }
        return 1;
    }

posted on 2021-04-15 21:07  郑豪斌  阅读(69)  评论(1)    收藏  举报