你的位置:首页 > 信息动态 > 新闻中心
信息动态
联系我们

【C++】循环练习作业:打印图形+九九乘法表(附加:判断闰年问题+三个数排序输出问题)

2021/11/4 8:56:15

题目如下

题目01

在这里插入图片描述

题目02

在这里插入图片描述

题目03

在这里插入图片描述

题目04

在这里插入图片描述

题目05

在这里插入图片描述

题目06

ex04_10

编程求解问题,若一头小母牛,从出生起第四个年头开始每年生一头母牛,按此规律,第n年时有多少头母牛。

题目07

在这里插入图片描述

题目08

在这里插入图片描述

实现代码

#include <iostream>
#include <cmath>

using namespace std;

void test_ex04_08_01()
{

    for (int i = 9, j = 1; i >= 0; i--, j = j + 2)
    {
        for (int m = 0; m < i; m++)
            cout << "  ";
        for (int n = 0; n < j; n++)
            cout << "# ";
        cout << endl;
    }
}

void test_ex04_08_02()
{

    for (int i = 8, j = 17; i > 0; i--, j--)
    {
        for (int m = 0; m < 8 - i; m++)
            cout << " ";
        for (int n = 0; n < j; n++)
            cout << "# ";
        cout << endl;
    }
}

void test_ex04_09_01()
{

    cout << "*";
    for (int heng = 1; heng < 10; heng++)
    {
        cout << "\t" << heng;
    }
    cout << endl;
    cout << "---------------------------------------------------------------------------" << endl;
    for (int i = 1, head = 1; i <= 9; i++, head++)
    {
        cout << head << "\t";
        for (int j = 1; j <= 9; j++)
        {
            cout << head * j << '\t';
        }
        cout << endl;
    }
}

void test_ex04_09_02()
{

    cout << "*";
    for (int heng = 1; heng < 10; heng++)
    {
        cout << "\t" << heng;
    }
    cout << endl;
    cout << "---------------------------------------------------------------------------" << endl;
    for (int i = 1, head = 1; i <= 9; i++, head++)
    {
        cout << head << "\t";
        for (int j = 1; j <= i; j++)
            cout << i * j << '\t';

        cout << endl;
    }
}

void test_ex04_09_03()
{

    cout << "*";
    for (int heng = 1; heng < 10; heng++)
    {
        cout << "\t" << heng;
    }
    cout << endl;
    cout << "---------------------------------------------------------------------------" << endl;
    for (int i = 1, head = 1; i <= 9; i++, head++)
    {
        cout << head << "\t";
        for (int j = 1; j <= 9; j++)
        {
            if (j < i)
                cout << '\t';
            else
                cout << i * j << '\t';
        }
        cout << endl;
    }
}

int func_ex04_10(int n)
{
    if (n <= 3) //数据校验有待完善
        return 1;
    else
        return func_ex04_10(n - 1) + func_ex04_10(n - 3);
}

void test_ex04_10()
{
    cout << "请输入年份n,将计算求出第n年时有多少头母牛:" << endl;
    int n;
    cin >> n;
    cout << "第" << n << "年时有 " << func_ex04_10(n) << " 头母牛: " << endl;
}

int func_unit3_assign1(int year)
{
    if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
        return 1;
    return 0;
}

void test_unit3_assign1()
{
    cout << "Enter a year : ";
    int year;
    cin >> year;
    cout << endl;
    int leap = func_unit3_assign1(year);
    if (leap)
        cout << year << " is a leap year." << endl;
    else
        cout << year << " is not a leap year." << endl;
}

void test_unit3_assign2()
{
    cout << "请输入三个数字 : " << endl;
    int x, y, z;
    cin >> x >> y >> z;
    cout << endl;
    int total = x + y + z;
    int max_value = max(max(y, z), max(y, x));
    int min_value = min(min(y, z), min(y, x));
    cout << "max = " << max_value << endl;
    cout << "mid = " << total - max_value - min_value << endl;
    cout << "min = " << min_value << endl;
}




int main()
{

    cout << "-------------------begin--------------------------\n" << endl;

    //    test_ex04_08_01();
    //    test_ex04_08_02();
    //    test_ex04_09_01();
    //    test_ex04_09_02();
    //    test_ex04_09_03();
    //    test_ex04_10();
    //    test_unit3_assign1();
    //    test_unit3_assign2();


    cout << "\n--------------------end----------------------------" << endl;
    return 0;
}

实现效果

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述