C语言程序设计 练习题参考答案 第五章 (1) 函数定义调用

 /*  5.6  编写函数,输出所有水仙花数 */

#include  "stdio.h"
int isdaffodil( int  n ) ;  /* isdaffodil函数原型声明 */

void main()
{
   int i;
   for( i=100; i<=999; i++)
      if( isdaffodil( i ) )
         printf("%5d",i);
}

int isdaffodil( int n ) /* isdaffodil函数 ,判断数n是不是水仙花数 */
{
 int units, tens, hundreds;
 if(n>999 || n<100)
    return 0;  /* it is not a daffodil */
 units=n%10;
 tens=n/10%10;
 hundreds=n/100;
 if(n==units*units*units+ tens*tens*tens+hundreds*hundreds*hundreds)
     return 1;   /* it is a daffodil ,返回1*/
 else
     return 0;   /* it is not a daffodil,返回0 */
}

/*  5.7 编写函数,求最大公约数和最小公倍数 */

#include "stdio.h"
#include "conio.h"
int CommonDivisor(int  m, int  n) ;
int LowestCommonMultiple(int  m, int  n);
void main()
{
    int m, n;
    printf("求最大公约数和最小公倍数 ,请输入m和n\n");
    scanf("%d%d", &m, &n );
    printf("最大公约数为%d, 最小公倍数为%d",CommonDivisor(m,n),LowestCommonMultiple(m, n)) ;
    getch();
}

int CommonDivisor(int  m, int  n)
{
    int  remainder, temp;
    if(n<m)
    { temp=m; m=n; n=temp; }
    remainder=m%n;
    while( remainder != 0 )
    {
     m=n;
     n=remainder;
     remainder=m%n;
    }
   return n;

}

int LowestCommonMultiple(int m, int n)
{
   return m*n/CommonDivisor(m,n);
}

/*  5.8  编写函数,重复打印给定字符n次*/

#include "stdio.h"
void printchar(char  ch, int  count); /* 函数原型声明 */

void main()
{
   char  c;
   int  n;
   printf("请输入要重复打印的字符\n");
   c=getchar( );
   printf("请输入重复次数\n");
   scanf("%d", &n);
   printf("重复输出给定字符%c共计%d次\n", c, n);
   printchar(c, n);
}

void printchar(char  ch, int  count)  /* 重复打印字符ch,count表示次数 */
{
  int  i;
  for(i=1; i<=count; i++)
    putchar(ch);

}

闰年是为了弥补因人为历法规定造成的年度天数与地球实际公转周期的时间差而设立的。补上时间差的年份,即有闰日的年份为闰年
         公历闰年判定遵循的规律为:  四年一闰,百年不闰,四百年再闰.
         公历闰年的简单计算方法(符合以下条件之一的年份即为闰年)
        1。能被4整除而不能被100整除。
        2。能被100整除也能被400整除。
详情如下:
       闰年leap year),指在公历(格里历)或夏历中有闰日的年份,以及在中国旧历农历中有闰月的年份。
       地球绕太阳运行周期为365天5小时48分46秒(合365.24219天),即一回归年tropical year)。公历的平年只有365日,比回归年短约0.2422 日,每四年累积约一天,把这一天加于2月末(2月29日),使当年的历年长度为366日,这一年就为闰年。 按照每四年一个闰年计算,平均每年就要多算出0.0078天,经过四百年就会多出大约3天来,因此,每四百年中要减少三个闰年。所以规定公历年份是整百数的,必须是400的倍数才是闰年,不是400的倍数的就是平年。比如,1700年、1800年和1900年为平年,2000年为闰年。闰年的计算,归结起来就是通常说的:四年一闰,百年不闰,四百年再闰.
 /*  5.9 编写函数,给出年月日,计算该日是本年的第几天 */

#include "stdio.h"
int isleapyear(int  y); /*函数原型声明*/
int dayofyear(int year, int month, int day);/*函数原型声明*/
void main()
{
   int y,m,d;
   printf("请输入年月日,数字间以空格隔开\n");
   scanf("%d%d%d",&y, &m, &d);
   printf("%d年%d月%d日是该年的第%d天", y, m, d, dayofyear(y, m, d));
}

int isleapyear(int  y) /* 判断某年y是不是闰年*/
{
 if (( y%4==0 && y%100!=0 ) || y%400==0 )
     return 1;   /* it is a leap year ,返回 1*/
 else
     return 0;   /* it is not a leap year ,返回 0*/
}

int dayofyear(int  year, int  month, int  day) /* 计算某日是该年第几天 */
{
    int days[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
    int i, daycount=0;
    if( isleapyear( year ) )
       days[2]=29;
    for(i=1; i<month; i++)
       daycount=daycount + days[i];
    daycount=daycount + day;
    return daycount;

}

posted @ 2008-04-19 18:05  emanlee  阅读(5852)  评论(0编辑  收藏  举报