实验三——张翼飞

实验任务一:

// 生成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~31之间的随机整数,并打印输出
#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); 
}
printf("\n");
return 0;
} 

   实验任务二:

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define N 3 
int main(){
    int x,n,a;
  srand(time(0));
    x = rand()%31+1;
   printf("猜猜2021年5月哪一天会是你的lucky day\n开始喽,你有三次机会,猜吧(1~31):");
for(n=1;n<=N;n++){
  scanf("%d",&a);
    if(a>x)
    {
 if(n<3)
 printf("你猜的日期晚了,lucky day悄悄溜到前面了\n再猜(1~31):");
else
 printf("你猜的日期晚了,lucky day已经过了\n");
    }
    else if(a<x)
    {
     if(n<3)
     printf("你猜的日期早了,lucky day悄悄溜到后面了\n再猜(1~31):");
     else
     printf("你猜的日期早了,lucky day还没到呢\n");
    } 
    else
    {printf("恭喜你猜对了!\n");break;}
}
    printf("次数已经用完了很遗憾你没有猜对。偷偷告诉你:5月,你的lucky day是%d号\n",x);  
return 0;
}

 实验任务三:

#include <math.h>
int main() {
    long a,b,n,x;
    printf("Enter a number:\n");
    scanf("%d",&a);//输入一个长整型数字 
    b=a,n=1;
    while(b/=10)n*=10;//判断s有多少位
    while(a){
        if((x=a/n)&1)//是奇数
        (b*=10)+=x;//将b乘以10后加到这位奇数上构成新的数字
        a%=n,n/=10; 
    } 
    printf("The new number is %d\n",b);
    return 0;
} 

 

 实验任务四:

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

 

 

 

 实验任务五:

#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 s=1,x=1;
    int i;
    if(n==1)
    return 1;
    else{
    for(i=2;i<=n;i++){
        x=-x*1/i;
        s=s+x;
        }
    return s;     
    }
}

 

 

 

 实验任务六:

#include<stdio.h>
#include<math.h>
int isPrime(int n) ;
int main(){
    int i,s,a=0;
    for(i=101;i<=200;i++){
        s=isPrime(i);
        if(s==i){
        printf("%5d",s);
        a=a+1;
    }
        if(s!=0&&a%5==0)
        printf("\n");
    }
    return 0;
    
    
}

int isPrime(int n){
    int i,m;
    m=sqrt(n);
    for(i=2;i<=m;i++)
        if(n%i==0)break;
    if(i>m&&n>1)
    return n;
    else
    return 0;    
    }

 

posted @ 2021-04-13 21:59  文雨zyf  阅读(71)  评论(2)    收藏  举报