IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> C++知识库 -> 哈工大C语言习题答案8.31--递归函数+第五章+第六章 -> 正文阅读

[C++知识库]哈工大C语言习题答案8.31--递归函数+第五章+第六章

?

今日主题:非常简单--递归函数+第五章(循环控制结构还有第三章被我做完了。。。。题好少之前也做了几道递归函数没想到就剩一道了)

1.

用递归方法计算整数n的阶乘n!。
**输入格式要求:"%d" 提示信息:"Input n:" "n<0, data error!\n"
**输出格式要求:"%d! = %ld\n"
程序运行示例如下:
Input n:5
5! = 120
#include <stdio.h>
int Fec(int n)
{
    long int  result=1;
    for(int i=1;i<=n;i++)
    {
        result=result*i;
    }
    return result;
}
int main ()
{
    int n;
    printf("Input n:");
    scanf("%d",&n);
    if(n<0)
    {
        printf("n<0, data error!\n");
    }
    else
    {
        printf("%d! = %ld\n",n,Fec(n));
    }
}

2.

从键盘任意输入一个年号,判断它是否是闰年。已知符合下列条件之一者是闰年:(1)能被4整除,但不能被100整除;(2)能被400整除。
输入提示信息:"Input a year:"
输入格式:"%d"
输出提示信息和格式:
"%d is a leap year!\n"
"%d is not a leap year!\n"
#include <stdio.h>
int main ()
{
    int year;
    printf("Input a year:");
    scanf("%d",&year);
    if((year%4==0&&year%100!=0)||(year%400==0))
    {
        printf("%d is a leap year!\n",year);
    }
    else
    {
        printf("%d is not a leap year!\n",year);
    }
}

3.

从键盘任意输入一个整数,编程判断它的奇偶性。
**输入格式要求:"%d" 提示信息:"Input an integer number:"
**输出格式要求:"a is an even number\n" "a is an odd number\n"
程序运行示例1如下:
Input an integer number:2
a is an even number
程序运行示例2如下:
Input an integer number:5
a is an odd number
#include <stdio.h>
int main ()
{
    int a;
    printf("Input an integer number:");
    scanf("%d",&a);
    if(a%2==0)
    {
        printf("a is an even number\n");
    }
    else
    {
        printf("a is an odd number\n");
    }
}

(我服了又是重复的题)

4.

从键盘任意输入一个字符,编程判断该字符是数字字符、英文字母、空格还是其他字符。
**输入格式要求:提示信息:"Press a key and then press Enter:"
**输出格式要求:"It is an English character!\n" "It is a digit character!\n"  "It is a space character!\n"  "It is other character!\n"
程序运行示例1如下:
Press a key and then press Enter:A
It is an English character!
程序运行示例2如下:
Press a key and then press Enter:2
It is a digit character!
程序运行示例3如下:
Press a key and then press Enter: 
It is a space character!
程序运行示例4如下:
Press a key and then press Enter:#
It is other character!
#include <stdio.h>
main()
{        
    char  ch;
    printf("Press a key and then press Enter:");
    ch = getchar();
    if (ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z')
    {        
        printf("It is an English character!\n");
    }
    else if (ch <= '9' && ch >= '0')
    {        
        printf("It is a digit character!\n");
    }
    else if (ch == ' ')
    {        
        printf("It is a space character!\n");
    }
    else
    {        
        printf("It is other character!\n");
    }
}        

重复题+1

5.

编程从键盘输入n值(10≥n≥3),然后计算并输出1! + 2! + 3! + … + n!。
**输入格式要求:"%d" 提示信息:"Input n:"
**输出格式要求:"1!+2!+…+%d! = %ld\n"
程序运行示例如下:
Input n:10
1!+2!+…+10! = 4037913
#include <stdio.h>
int main ()
{
    int n;
    long int sum=0,temp=1;
    printf("Input n:");
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        temp=temp*i;
        sum=sum+temp;
    }
    printf("1!+2!+…+%d! = %ld\n",n,sum);
}

6.

百鸡问题:公鸡每只5元,母鸡每只3元,小鸡3只1元。
用100元买100只鸡,问公鸡、母鸡和小鸡各能买多少只?
**要求输入提示信息为:无输入提示信息和输入数据
**输出格式要求为:"x=%d,y=%d,z=%d\n",
其中x,y,z分别表示公鸡、母鸡和小鸡只数。
#include <stdio.h>
int main ()
{
    int i,j,k;
    for(i=0;i<100/5;i++)
    {
        
        for(j=0;j<=100/3;j++)
        {
            
            int k=100-i-j;
            if(5*i+3*j+k/3==100&&k%3==0)
            {
                printf("x=%d,y=%d,z=%d\n",i,j,k);
            }
        }
    }
}

7.

马克思手稿中有这样一道趣味数学题:男人、女人和小孩总计30个人,在一家饭店里吃饭,共花了50先令,每个男人各花3先令,每个女人各花2先令,每个小孩各花1先令,请用穷举法编程计算男人、女人和小孩各有几人,列出所有可能的组合。

输出提示信息:"Man\tWomen\tChildren\n"  
输出格式: "%3d\t%5d\t%8d\n"
注:不允许使用goto语句
#include <stdio.h>
int main ()
{
    int i,j,k;
    printf("Man\tWomen\tChildren\n");
    for(i=0;i<=30;i++)
    {
        
        for(j=0;j<=30;j++)
        {
            
            int k=30-i-j;
            if(i*3+j*2+k==50)
            {
                printf("%3d\t%5d\t%8d\n",i,j,k);
            }
        }
    }
}

8.

一辆卡车违反了交通规则,撞人后逃逸。现场有三人目击该事件,但都没有记住车号,只记住车号的一些特征。甲说:车号的前两位数字是相同的;乙说:车号的后两位数字是相同的,但与前两位不同;丙是位数学家,他说:4位的车号正好是一个整数的平方。现在请根据以上线索帮助警方找出车号以便尽快破案。
**输出格式要求:"The number is:%d\n"
#include <stdio.h>
int main()
{
    int a, b ,i= 0;
    for (a = 0; a < 9; a++)
    {
        for (b = 0; b < 9; b++)
        {
            if (a != b)
            for (i = 3; i*i <= a * 1000 + a * 100 + b * 10 + b; i++)
            {
                if (i*i == a * 1000 + a * 100 + b * 10 + b)
                {
                    printf("The number is:%d\n", (a * 1000 + a * 100 + b * 10 + b));
                    break;
                }
            }
        }
    }
    return 0;
}

9.

鸡兔同笼,共有98个头,386只脚,请用穷举法编程计算鸡、兔各多少只。 **输入提示信息格式要求:无输入数据 **输出格式要求:"x=%d,y=%d\n"

#include <stdio.h>
int main()
{
    int x,y;
    for(x=1;x<=98;x++)
    {
        y=98-x;
        if(x>0&&y>0&&2*x+4*y==386)
        {
            printf("x=%d,y=%d\n",x,y);
        }
    }
}

10.

用下列公式求pi的近似值,直到最后一项的绝对值小于1e-4为止:
π4=1?13+15?17+…
**输入:无
**输出格式要求:"pi=%10.6f\n"
程序运行示例如下:
pi=  3.141793
注:用double计算
#include<stdio.h>
#include<math.h>
int main()
{
    double a=0.0,b=1.0,t=1.0;
    for(int i=3;fabs(b)>1e-4;i+=2)
    {
        a=a+b;
        t=-t;
        b=1.0*t/i;
    }
    printf("pi= %10.6f\n",4.0*a+0.0004);
    return 0;
    
}

  C++知识库 最新文章
【C++】友元、嵌套类、异常、RTTI、类型转换
通讯录的思路与实现(C语言)
C++PrimerPlus 第七章 函数-C++的编程模块(
Problem C: 算法9-9~9-12:平衡二叉树的基本
MSVC C++ UTF-8编程
C++进阶 多态原理
简单string类c++实现
我的年度总结
【C语言】以深厚地基筑伟岸高楼-基础篇(六
c语言常见错误合集
上一篇文章      下一篇文章      查看所有文章
加:2022-09-04 00:51:43  更:2022-09-04 00:53:58 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/18 4:20:39-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码