频道栏目
首页 > 资讯 > C语言 > 正文

C语言编程练习题及答案分享

18-04-08        来源:[db:作者]  
收藏   我要投稿

1. 题目:写一个程序,判断两个浮点数是否足够精确。

/*  File name:ApproximatelyEqual  
    Function: ensure the accurcy of two numbers.|x-y|/min(|x|,|y|)<e   
    Time: 2018.04.07  
    edited by QJX     
*/  
  
#include <stdio.h>  
#include <math.h>  
double Minfabsnum(double x,double y);   //Calculate the min of fabs(x),fabs(y)  
void GiveInstruction(void); //Give instruction and reference to users  
#define e 0.0001    //The accurcy   
  
double main()  
{  
     double x,y,c;  
     GiveInstruction();  
     printf("Please input 2 numers:");  
     scanf("%lf,%lf",&x,&y);  
     c=Minfabsnum(x,y);  
     if(fabs(x-y)/c<e)  
        printf("Two numers satisfy the accurcy!");  
    else   
        printf("Two numers not satisfy the accurcy!");  
}   
  
double Minfabsnum(double x,double y)    //Calculate the min of fabs(x),fabs(y)  
{  
    double a,b;  
    a=fabs(x);  
    b=fabs(y);  
    if(a<=b)  
    return a;  
    else  
    return b;  
}  
  
void GiveInstruction(void)  //Give instruction and reference to users  
{  
    printf("This program can ensure the accurcy of two double numbers!\n");  
    printf("And the accurcy is 0.0001\n\n");  
}  

2. 题目:打印杨辉三角前八行

/*  File name:Pascal triangle or YangHui triangle 
    Founction: display the 8 raws of YangHui triangle 
    Time:2018.04.07 
    edited by qjx 
*/  
  
#include <stdio.h>  
#define N 8  
int Factorial(int n);  
int Combinations(int n,int k);  
void GiveInstruction(void);  
  
main()  
{  
    int raw,i,j,k;  
    GiveInstruction();  
    printf("Please input the raws of YangHui triangle:");  
    scanf("%d",&raw);  
    for(i=1;i<=raw;i++)  
    {  
        for(j=0;j<raw-i;j++) //print the spacebar  
        {  
            printf(" ");  
        }  
        for(k=0;k<=i-1;k++)  //print the YangHui triangle  
        {  
            printf("%4d ",Combinations(i-1,k));   
        //  printf(" ");  
        }  
        printf("\n");  
    }  
}  
  
int Factorial(int n)   //calculate n!  
{  
    int i,product=1;  
    for(i=1;i<=n;i++)  
    {  
        product*=i;  
    }  
    return product;   
}  
  
int Combinations(int n,int k)    //calculate C(n,k)=n!/(k!*(n-k)!)  
{  
    int a,b,c;  
    a=Factorial(n);  
    b=Factorial(k);  
    c=Factorial(n-k);  
    return a/(b*c);  
}  
  
void GiveInstruction(void)  
{  
    printf("This program can display the YangHui triangle!\n");  
    printf("For example:\n");  
    printf("  1  \n");  
    printf(" 1 1 \n");  
    printf("1 2 1\n");  
}  

当用更大的数据去测试时,发现最多只能到13,到需要输出14行杨辉三角时就会出现错误。

本来以为是超出了int型的范围,但是手动计算之后并没有超出啊,而且如果超出的话,在计算12!时,就已经超出范围了。不解!

相关TAG标签
上一篇:获取SourceTree许可证,完成注册
下一篇:robot framework常用关键字
相关文章
图文推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站