实验三

//任务1
#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+%.2fi,x2=%.2f-%.2fi\n\n",x1,x2);
            }
            else{
                real=-b/(2*a);
                imag=sqrt(-delta)/(2*a);
                printf("x1=%.2f+%.2fi,x2=%.2f+%.2fi\n\n",real,imag,real,imag);
            }
        }
        printf("Enter a,b,c:");
    }
    return 0;
} 
//ex2 #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; }
//ex3 #include <stdio.h> #include <math.h> int main(){ int a,b,n; n=0; for(a=101;a<=200;a++){ for(b=2;b<=sqrt(a);b++){ if(a%b==0) break; else if(b>sqrt((double)a)-1){ printf("%d,",a); n++; if(n%5 ==0){ printf("\n"); } } else continue; } } printf("\n101~200之间一共有%d个素数",n); return 0; }
//Ex4 #include <stdio.h> #include <math.h> int main(){ long n; printf("Enter a number:"); while(scanf("%ld",&n)!=EOF){ long a=0; int b=0; while (n!=0){ if(n%2!=0){ a=a+pow(10,b)*(n%10); b++; } n=n/10; } printf("new number is:%ld\n\n",a); printf("Enter a number:"); } return 0; }
//Ex5 #include <stdio.h> #include<math.h> int main(){ int n; printf("Enter n(1~9):"); while(scanf("%d",&n)!=EOF){ double s=0.0; double k=1.0; for(int a=1;a<=n;a++){ k=k*a; s+=pow(-1,a-1)*(1.0/k); } printf("n=%d,s=%lf\n\n",n,s); printf("Enter n(1~9):"); } return 0; }
//Ex6 #include <stdio.h> #include <time.h> #include <stdlib.h> int main(){ int day,n; srand(time(0)); printf("猜猜12月哪一天是你的lucky day:"); n=rand()%31+1; int a=0; while(scanf("%d",&day)!=EOF){ a++; if(day==n){ printf("Yes!"); break; } if(day<n){ printf("Too early!\n\n"); printf("Try again:"); } if(day>n){ printf("Too late!\n\n"); printf("Try again:"); } if(a==3){ printf("Your lucky day is%d",n); break; } } return 0; }

 

posted @ 2020-11-18 14:10  包包ovo  阅读(153)  评论(2)    收藏  举报