ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

实验三

2021-04-13 19:34:52  阅读:84  来源: 互联网

标签:int double printf 实验 delta x2 include


实验任务一

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define N 5

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

实验任务二

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

int main() {
    int x,n,a;
    
    srand(time(0));
    x =1+ rand() % 31;
    printf("猜猜2021年5月哪一天是你的lucky day\n开始喽,你有三次机会,猜吧(1^31):");
    for(n=1;n<=3;n++)
    {
        scanf("%d",&a);
        
        if(a==x){
           printf("恭喜你呀,猜对啦!\n");
           break;
        }
        else if(a<x){
            if(n<=2)
            printf("你猜的日期早了,lucky day还没到呢\n再猜:");
            else
            printf("你猜的日期早了,lucky day还没到呢\n");
        }
        else{
            if(n<=2)
            printf("你猜的日期晚了,lucky day悄悄溜到前面啦\n再猜:"); 
            else
            printf("你猜的日期晚了,lucky day悄悄溜到前面啦\n"); 
        }
    }
    while(n>3){
        printf("次数用完啦,偷偷告诉你:5月,你的lucky day是%d号\n",x)    ;
        break;
        
    }
         
         
    
        
    return 0;
} 

实验任务三

#include<stdio.h>

int main() {
    unsigned long x,t,p;
    while(printf("Enter a number:\n"), scanf("%u",&x)!=EOF) {
        t=0,p=1;
        while(x!=0){
        if((x%10)%2!=0)
        {
           t=t+(x%10)*p;
           p=p*10;    
        }
        x=x/10;
    }
    printf("New number is:%u\n",t);
    
    }
    
    return 0;
} 

实验任务四

#include<stdio.h>
#include<math.h>
void solve(double a, double b, double c);

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

不能以返回值的形式返回给主调函数,因为函数每次只能有一个返回值,无法将solve函数中 x1,x2均返回至主函数

实验任务五

#include<stdio.h>
double fun(int n);

int main(){
    int n;
    double s;

    while(printf("Enter n(1~10):"),scanf("%d", &n) != EOF){
        s = fun(n);
        printf("n = %d, s = %f\n\n", n, s);
    }

    return 0;
}

double fun (int n){
    int i,z,f=1,design=1;
    double b,s;
    for(i=1,s=0;i<=n;i++,f=1)
    {
    for(z=i;z>0;--z){
        f=f*z;
    }    
        b=design*1.0/f; 
        s=s+b;
        design=-design;
    }
    return s;
}

实验任务六

#include<stdio.h>
#include<math.h>
int isPrime(int n);

int main(){
    int N, n;
    for(n = 101,N = 1;n<200;n++){

        if (isPrime(n))
        {
        printf("%d ", n);
        N++;
         if(N%6 == 0)
         {
         printf("\n");
         N = 1;
         }
        }
    }
    return 0;
}

int isPrime(int n){
    int k;
    for(k=2;k<=sqrt(n);k++)
        if(n%k==0)
           return 0;
        return 1;   
    }
   

标签:int,double,printf,实验,delta,x2,include
来源: https://www.cnblogs.com/rickyhh/p/14654864.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有