作业3

 

 

// 一元二次方程求解
// 重复执行, 直到按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 = %.2f - %.2fi\n\n", real,
imag, real, imag);
}
}
printf("Enter a, b, c: ");
}
return 0;
}

 

 

// 生成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; // 生成一个0~9之间的随机整数
printf("%3d", x);
}while(n<N);
printf("\n");
return 0;
}

 

 

//100到200之间的
#include<stdio.h>
#include<math.h> 
int main(){
    int i,j;
    int n=0;
    for(i=100;i<=200;i++)
    {
        for(j=2;j<=sqrt(i);j++)
        if(i%j==0)
        break;
        if(j>sqrt(i))
        {
        printf("%4d",i);
        n++;
        if(n%5==0)
        printf("\n");
        }
        
    }printf("\n中有%d个素数",n);
return 0;
} 

 

 

//changzhengshu
#include<stdio.h>
#include<math.h>
int main()
{
    int n,a,b,i,s;
    printf("Enter a number:");
    scanf("%d",&n);
    while(n){
        i=0;
        s=0;
        while(n!=0){
            a=n%10;
            b=a%2;
            if(b!=0){
                s=s+a*pow(10,i);
                i++;}
            n/=10;}

        printf("new number is:%d\n",s)c;
 printf("Enter a number:");
        scanf("%d",&n);}
return 0;}

 

 

//不晓得取啥名,反正就是实验5 
#include<stdio.h>
#include<math.h> 
int main(){
    int n,m,i;
    double x,s;
    s=0;
    x=1;
    m=-1;
printf("Enter a number(1~10):%d\n",n);
    while(scanf("%d",&n))
    {
    for(i=1;i<=n;i++)
       { m=-m;
         x=x*i;
         s=s+m*(1/x);
    }
    printf("n=%d,s=%lf\n",n,s);
    printf("Enter a number(1~10):");
    } 
    return 0;
} 

 

 

//猜不对啊
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main(){
    int i,m,n=1;
    srand((unsigned)time(0));
    m=rand()%30+1;
    printf("猜猜2020年12月哪一天会是你的luck day\n");
    printf("开始喽,你有三次机会,猜吧(1~31):");
    while(n<=3)
    {
        scanf("%d",&i);
        if(i==m)
        {
            printf("猜对了哦!\n");
            break;
        }
        else
        if(m>i)
           {
               printf("你猜的日期早了,luck day还没到呢\n");
               printf("再猜(1~31):"); 
               n++;
           }
        else
        {
        printf("你猜的日期晚了,luck day已经悄悄溜走了\n");
        printf("再猜(1~31):");
        n++;
    }
    }
    if(n>3)
    printf("次数用完了,偷偷告诉你:12月,你的luck day是%d\n",m);

    return 0;}

 

posted on 2020-11-19 22:45  雨打花落又逢春  阅读(104)  评论(0)    收藏  举报

导航