实验三

实验任务1

//一元二次方程求解
//重复执行,直到按Ctrl+Z结束
//
#include <math.h>
#include <stdio.h>

int main(){
    float a, b, c, x1, x2;
    float delta, real, imag;
    
    printf("Enter a, b, c:");
    
    while(scanf("%f%f%f", &a, &b, &c)!=EOF){
        if(a==0)
        printf("not quadratic equation.\n\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\n", x1, x2);
            
                }
            else {
                real = -b/(2*a);
                imag = sqrt(-delta)/(2*a);
                printf("x1 = %.2f + %.2fi, x2 = %.2fi\n\n",real,imag,real,imag);
                
            }
                }
                printf("Enter a, b, c:");
                
    } 
           return 0;
       }

实验任务2

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

int main(){
    int x,n;
    
    srand(time(0));
    n = 0;
    do{
        n++;
        x = rand()%10;
        printf("%3d",x);
    }while(n<N);
    
    printf("\n");
    
    return 0;
}

运行图

实验任务3

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(){
    int m,n;
    int i=0;
    
    for(m=101;m<=200;m++)
    {
    for(n=2;n++;n<=m)
    {
        if(m%n==0)break;}
        
    
    if(n>=m)
        {
        printf("%5d",m);
        i++;
        if(i%5==0)printf("\n");
    }
 

}
printf("\n");
printf(" 101~200之间有%d个素数\n",i);
return 0;
}

运行图示

实验任务4

算法思路:

输入整型数字除10取出,取出后与2取余判断是否为奇数。

排序按照先后顺序乘10的几次方,第n次取得的数乘n-1次方,以此类推。

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

运行图示

实验任务5

#include<stdio.h>
#include<math.h>
int main(){
    int m,n;
    float x;
    printf("Enter n(1~10):");
    while (scanf("%d",&n)!=EOF){
        float x=0,y=0;
        for(m=1;m<=n;m++){
            y=y*m;
            x=x+pow(-1,m-1)*(1/y);
        }
        printf("n=%d,x=%f\n",n,x);
        printf("Enter n(1~10):");
    }
    return 0;
}

运行图示

实验任务6

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main(){
    int m,n,x=1,y=0;
    int ret=0;
    srand(time(0));
    n=rand()%30+1;
    
    printf("猜猜2020年12月哪一天会是your luck day!\n");
    printf("开始喽,你有三次机会,来吧(1~31):");
    scanf("%d",&m);
    while(x<=3){
        if(m==n){
            printf("你猜的日期是正确的!\n");
            ret=1;
            break;
        }else if(m>n){
            printf("你猜的日期晚了,luck day悄悄溜到前面啦!\n");
            printf("再猜!");
            scanf("%d",&m);
            x++; 
        }else{
            printf("你猜的日期早了,luck day还没到呢!\n");
            
            printf("再猜(1~31):");
            scanf("%d",&m);
            x++;
        }
    }
    if(ret=0)printf("次数用完了,偷偷告诉你:12月,你的luck day是:%d",n);
  return 0;
}
    

运行图示

 

posted on 2020-11-20 11:38  helianthus-JUN  阅读(136)  评论(0编辑  收藏  举报

导航