实验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;
} 

 

 实验二

// 生成N个0~99之间的随机整数,并打印输出 
#include <stdio.h>
#include <stdlib.h>
#include <time.h>


int main() {
    int x,a,i; 
    printf("猜猜5月哪一天是你的幸运日\n");
    printf("开始喽,猜吧"); 
    srand(time(0));  
    
        x = rand() % 30+1;
    
          for(i=1;i<4;i++) {
              if(i>1)
              printf("再猜");
            scanf("%d",&a);
            if(a!=x)
            {
              if(a<x){ 
              printf("你猜的日期早了\n"); 
              } 
              else {
        
              printf("你猜的日期晚了\n"); 
              }
            }
            else{
            printf("猜对了!");break;    
            }
        }
        if(i=4&&a!=x){
            printf("次数用完了,偷偷告诉你,lucky day是%d号",x); 
        }
    return 0;
} 

 

 

实验三

#include<stdio.h>
int main()
{
    unsigned long a,b;
    printf("输入数字:");
    while(scanf("%ld",&a)!=EOF)
    {
        unsigned long d=1;
        unsigned long c=0;
        while(a!=0)
        {
            if((a%10)%2!=0)
            {
                b=a%10;
                c=c+b*d;
                d=d*10;
            }
            a=a/10; 
         }
         printf("新数字是%ld\n",c);
         printf("输入数字:"); 
         
        
          
        
    }
    
    return 0;
    
}

 

 实验四

 

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

 

不能,函数只能有一个返回值。

 

实验五

 

 

#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) {
    int a,c;
    double sum=0;
    double b;
    b=1;
    c=2;
    for(a=1;a<=n;a++){
        sum=sum+1/b;
        b=(-1)*b*(c);
        c=c+1;
    }

    return sum;
}

 

 

 

 

 

实验六

#include<stdio.h>
#include<math.h>
int isPrime(int n) ;
int main(){
    int i,a,b;
    a=0;
    for(i=101;i<=200;i++){

        if(isPrime(i)){
        printf("%5d",i);
        a=a+1;
        if(a%5==0)
            printf("\n");
        }
    }
    printf("\n共有%d个",a); 
    return 0;
    
    
}

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

 

posted @ 2021-04-15 00:19  gch1203  阅读(50)  评论(1)    收藏  举报