设为首页 加入收藏

TOP

C语言编程练习题及答案分享
2018-04-10 06:06:40 】 浏览:134
Tags:语言编程 习题 答案 分享

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!时,就已经超出范围了。不解!


编程开发网
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇分别介绍C语言中的单引号和双引号 下一篇C语言按照索引插入并查找元素的实..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

array(4) { ["type"]=> int(8) ["message"]=> string(24) "Undefined variable: jobs" ["file"]=> string(32) "/mnt/wp/cppentry/do/bencandy.php" ["line"]=> int(214) }