任务1

// 生成N个0~99之间的随机整数,并打印输出 
#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>
#define N 5

int main() {
    int x, n; 
    
    srand(time(0));  // 以当前系统时间作为随机种子 
    
    for(n=1; n<=N; n++) {
        x = rand() % 31+1;//生成1~31间的随机数 
        printf("%3d",x);
    }
    
    
    return 0;
} 

任务2

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

int main() {
    int x,y,n; 
    
    srand(time(0));  // 以当前系统时间作为随机种子 
    x = rand() % 31+1;  // 生成一个0~99之间的随机整数
    printf("猜猜2021年5月哪一天会是你的 luck day");
    printf("你有三次机会哦,快来是试一试(1~31):");
    
       for(n=1;n<=4;n++)
{
if (n<4){

scanf("%d",&y); 
       if(x!=y)
       {
           if(x<y)
       {
       printf("你猜的幸运日晚了,lucky day悄悄溜到前面啦\n"); 
        }
       else 
        printf("你猜的幸运日早了,lucky day悄悄溜到后面啦\n");
       }
       else
       {
       printf ("猜对啦!!") ;break;
       }
}

       else
        {n--; 
        n--; 
        printf("你的机会用完啦,偷偷告诉你你的lucky day 是,%d",x);
        }
}
    return 0;
} 

task3

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

int main(){
long x,y,z,n;



while( printf("Enter a number:"),scanf("%ld",&x)!=EOF)
{
z=0;
n=0;
while(x!=0){
y=x%10;
x=x/10;
if (y%2!=0){
n=n+y*pow(10,z);
z++;
}
}
printf("new number is:%ld\n\n",n);
}

return 0;
}

 

task4

// 一元二次方程求解(函数实现方式)
// 重复执行, 直到按下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);
        }
    }    
}

task5

#include <stdio.h>
#include <math.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 m,q,i;
    double p;
    m=1;
    p=0;
    i=1;
    while(m<=n)
    {
    for(q=1;q<=m;q++){
    i=i*q;
    }
    p=p+(1.0/i)*pow(-1,m+1);
    m++;
     }
     return p;
}// 补足代码
    // ××× 

task6

#include <stdio.h>
#include<math.h>
int isprime(int x);
int main (){
    int x,m;
    m=0;
    for(x=100;x<=200;x++){
    if (isprime(x)){
     printf("%4d",x);
     m++;
     if(m%5==0){
     printf("\n");
}
}
}
printf("\n\n");
printf("100~200之间素数个数为:%d",m);
return 0;
}

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

实验总结:更加深刻地掌握了函数的调用、定义等等,对算法逻辑的理解也更深了。

不足:依然对知识运用不够熟练,此外小错误太多,对作业效率造成了很大困扰。

posted on 2021-04-15 23:41  花语吖  阅读(34)  评论(1编辑  收藏  举报