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

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

时间:2023-11-08 11:07:24浏览次数:35  
标签:练习题 公历 年份 闰年 int 31 C语言 year 参考答案

/*  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;}


标签:练习题,公历,年份,闰年,int,31,C语言,year,参考答案
From: https://blog.51cto.com/emanlee/8246533

相关文章

  • C语言程序设计 练习题参考答案 第五章 (2) 递归函数
    /*5.10编写函数,求Fibonacci数列的第n项*/#include"stdio.h"intfibonacci(intn);voidmain(){intn;printf("求Fibonacci数列的第n项,请输入n\n");scanf("%d",&n);/*VC6中n要小于?*/printf("Fibonacci数列的第%d项为%d",n,......
  • C语言程序设计 课程实施细则
    C语言程序设计-课程实施细则课程学时:36+20教材:《C语言程序设计教程》  西安交通大学出版社  张毅坤等编著实验指导书:《C语言程序设计教程学习指南与实验指导》西安交通大学出版社  张毅坤等编著金花校区东门口书店有售。 第1章  概述            ......
  • C语言程序设计 练习题参考答案 第三章 (3) 循环结构
    /*3.9求派的值*/#include"stdio.h"voidmain(){intn;doublesum=0;for(n=1;n<=10000;n++){sum=sum+1.0/(4*n-3)-1.0/(4*n-1);}printf("pi的值为%lf\n",4*sum);}/*3.9求派的值*/#include<stdio.......
  • C语言程序设计 练习题参考答案 第六章 (1) 结构体 综合练习
    /*6.910个学生,每个学生3门课程成绩,求平均分及前五名*/#include"stdio.h"#include"conio.h"#defineN6structstudent/*定义结构体数据类型*/{intnum;charname[10];intscore[3];/*不能使用float*/floataverage;};voidsort(structstudentstu......
  • C语言程序设计 要求掌握的例题和习题
    以下的例题和习题要求掌握。 第1章  概述              2学时 第2章基本数据类型、运算符及表达式        2学时习题:2.7, 2.8, 2.11, 2.12, 2.13, 2.14, 2.15第3章 基本结构程序设计                       8学时例题: ......
  • C语言程序设计 随机函数的使用-随机点名做习题
    /*---------------------------------------随机点名做习题Author:emanleeDate:2008-04-24---------------------------------------*/#include"stdio.h"#include"conio.h"#include"time.h"voidmain(){intcount=4......
  • C语言程序设计 选择排序简介
    选择排序选择排序是通过每一趟排序过程中从待排序记录中选择出关键字最小(大)的记录,将其依次放在数据表的最前或最后端的方法来实现整个数据表的有序排列。本节将介绍选择排序方法中最简单且最常用的简单选择排序。选择排序基本思想 第一趟排序在所有待排序的n个记录中选出关键字最......
  • C语言程序设计 冒泡排序简介
    冒泡排序基本思想将n个记录看作按纵向排列,每趟排序时自下至上对每对相邻记录进行比较,若次序不符合要求(逆序)就交换。每趟排序结束时都能使排序范围内关键字最小的记录象一个气泡一样升到表上端的对应位置,整个排序过程共进行n-1趟,依次将关键字最小、次小、第三小…的各个记录“冒到......
  • C语言程序设计 练习题参考答案 第三章 (2) 选择结构
    /*3.6求3个数中最大值。类似于例1.2*/#include<stdio.h>voidmain(){inta,b,c,max;printf("\n请输入3个整数,整数以空格分隔:\n");scanf("%d%d%d",&a,&b,&c);if(a>b)max=a;elsemax=b;if(max......
  • C语言程序设计 求阶乘递归函数调用示例
    ......