实验三

Part1:

#include <math.h>
#include <stdio.h>
#include <stdlib.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)) {
        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 = %f, x2 = %f\n", x1, x2);
            }
            else {
                real = -b/(2*a);
                imag = sqrt(-delta) / (2*a);
                printf("x1 = %f + %fi, x2 = %f - %fi\n", real, imag, real, imag);
            }
        }
        
        printf("Enter a, b, c:\n");
    }
    system("pause");
    return 0;
} 

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main() {
    int guessNumber;
    int ans;     
    
    srand((time(0)));  
    guessNumber = 1 + rand()%100;   
    
    do {
        printf("your guess number is(1~100): ");
        scanf("%d", &ans);
        if(ans < guessNumber)
            printf("%d is lower than the number.\n", ans);
        else if(ans > guessNumber) 
            printf("%d higher then the number.\n", ans); 
    }while(ans != guessNumber);
    
    printf("Congratulations. you hit it~\n");    
    
    system("pause"); 
     
    return 0;
} 

 

Part2:

#include <stdio.h>
#include <stdlib.h>
int main() {
    int number, max, min, n;
    
    n=1;
    printf("输入第%d个数: ", n);
    scanf("%d", &number);
    max = number;
    min = number;
    
    while(n<5) {
        n++;
        printf("输入第%d个数: ", n);
        scanf("%d", &number);    

        if(number>=max)
            max = number;
        else if(number<=min)
            min = number;
    }
    
    printf("最大数为: %d\n", max);
    printf("最小数为: %d\n", min);
    
    system("pause");
    
    return 0;
} 

 

Part3:

#include<stdio.h>
#include<math.h>
int isprime(int n);
int main(){
    int i,m=0;
    for(i=101;i<=200;i++)
    {
        if(isprime(i))
            {
             printf("%6d",i);
             m=m+1;
             if(m%5==0)
                 printf("\n");
             }
    }
    printf("\n");
    printf("101~200之间共有%d个素数",m);
    return 0;
}
int isprime(int n)
{
    int k;
    for(k=2;k<=sqrt(n);k++)
        if(n%k==0)
            return 0;
        return 1;
}

#include<stdio.h>
int main(){
    printf("Enter a number:");
    int k,i=1;
    unsigned long m,a=0;
    scanf("%lu",&m);
    while(m!=0){
        k=m%10;
        m=m/10;
        if(k%2==1){
            a=a+k*i;
            i=10*i;
        }
    }
    printf("new number is:%lu",a);
    return 0;
} 

#include<stdio.h>
int main(){
    int n,a;
    double s=0; 
    printf("Enter n and a:");
    scanf("%d %d",&n,&a);
    int i;
    double y=1,b=0,c;
    for(i=1;i<=n;i++){
        b=b+a*y;
        c=i/b;
        s=s+c;
        y*=10;    
    }
    printf("s=%f",s);
    return 0;
} 

 

posted @ 2019-11-15 17:38  PuppyPiggy  阅读(154)  评论(4)    收藏  举报