shiyan3

#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 = %.2f - %.2fi\n\n" , real, imag, real, imag);
                 }
          }
          
          printf("Enter a, b, c: ");
      }
      
      return 0;
  }

 

 

//
#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;
}

 

 

#include<stdio.h> 
#include<math.h>
int main(){
 int x,y,z,a=0;
    for(x=101;x<=200;x++) 
    {
 
 y=(int)sqrt((double)x);
    for(z=2;z<=y;z++)

    
 
    {
 if(x%z==0)
    break;}
    if(z>y){
 
    printf("%5d",x);
    a++;
    if(a%5==0){
    printf("\n");}}
}
printf("\n") ;
printf("101~200之间共有%d个素数",a); 
    return 0;
    }

 

 实验3.3:

1)对x进行取余运算得到末位数字后通过整除10去掉该位数字,用if语句判断取余后的得数是否为奇数

2)因为第一个while循环所得的数值与想要获得的结果相反,所以再通过while语句循环将后输出的高位数字进行倒序处理

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

 

 可以运行但显示有bug 还不太清楚原因o.o

 

 

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

 

 

# include<stdio.h>
# include<stdlib.h>
# include<time.h>
int main(){
    int date,answer,count;
    srand((unsigned)time(NULL));
    date= rand()% 31;
    printf("猜一猜2020年12月哪一天会是你的lucky day\n");
    printf("你有三次机会哦!猜吧(1~31):");
    
    while (count<3){
        scanf("%d",&answer);
        if(date>answer){
          printf("太小了!luckday还没到\n");}
        else  if(date==answer){
          printf("恭喜你猜对了!\n");break;}
        else{
          printf("太大了!luckyday已经过了\n");}
          count ++;
          
    }
    if(count>=3)
    {
 printf("\n"); 
    printf("GAME OVER!\n");
    printf("实际上你的lucky day是%d号",date);} 
    return 0; 
    }

 

 总结:对while,for,if等运用更熟悉,了解到一些运算方法,有些想不到的运算还是需要同学提醒,每次写代码之前要先在纸上打草稿,令人头秃。。。

posted @ 2020-11-20 13:01  shokuhou04  阅读(100)  评论(0)    收藏  举报