实验3

#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; // 生成一个0~99之间的随机整数
printf("%3d", x);
}
printf("\n");
return 0;
}

实验3

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main(){
    int x,n,ans;
    srand(time(0));
    x=rand()%30+1;
    printf("guessing which day in May is your lucky day. you have 3 chances:\n");
    for(n=1;n<=3;n++){
       scanf("%d",&ans);
       if(ans==x){
       printf("get it!");
       break;}
        
       else if(ans<x){
           printf("too early,");
       }
           else if(ans>x){
               printf("too late,");
           }
        if(n<=2){
               printf("\ntry again:");
           }  
    }
    if(n==4){
        printf("\nchances out, your lucky day is:%d",x);
    }
    
    return 0;
}

实验3

任务3做不起来T-T

#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;
}
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);
}
}
}

实验3

只有判别式等于0的时候,两解为同一值能返回给主函数

#define _CRT_SECURE_NO_DEPRECATE
#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) 
{
    if(n==1)
        return 1;
    else if(n==2) 
        return 0.5;
    else
        return fun(n-1)-(fun(n-1))/n+(fun(n-2))/n;    
}

实验3

#include<stdio.h>
#include<math.h>

int isPrime(int m);

int main(){
    
    int m,p=0,l=0;
    
    for(m=100;m<=200;m++){
    

    if(isPrime(m)){
    printf("%4d",m);
    l=l+1;
    p++;
        }
    
    if(p==5)
    {printf("\n");
    p=0;
    }
    
    }
    
    printf("\n100~200之间素数个数为:%d",l);
    
    return 0;
}

int isPrime(int m){
        int i,n,q;
        n=sqrt(m);
        for(i=2;i<=n;i++){
        q=m%i;
        if(q==0)
        {
        return 0;
        }
}
}

实验3

上一篇:实验三


下一篇:Delta Lake 第一篇论文发布了