王道机试指南练习(第二章)

本文介绍了五个编程题目,涉及整数操作、条件判断、循环结构和数学逻辑,包括数字组合、数字反转、鸡兔同笼问题等,通过实例展示了如何使用C++解决这些信息技术中的基本问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

2.1 枚举

例题2.1

#include <cstdio>
#include <iostream>
#include <stdlib.h>

using namespace std;

int main()
{
    for (int a = 0; a <= 9; ++a)
    {
        for (int b = 0; b <= 9; ++b)
        {
            for (int c = 0; c <= 9; ++c)
            {
                if (a * 100 + b * 110 + c * 12 == 532)
                {
                    printf("%d %d %d\n", a, b, c);
                }
            }
        }
    }
    system("pause");
    return 0;
}

例题2.2

#include <cstdio>
#include <iostream>
#include <stdlib.h>

using namespace std;

int Reverse(int x)
{
    int revx = 0;
    while (x != 0)
    {
        revx *= 10;
        revx += x % 10;
        x /= 10;
    }
    return revx;
}

int main()
{
    for (int i = 1000; i <= 9999; ++i)
    {
        if (i * 9 == Reverse(i))
        {
            printf("%d\n", i);
        }
    }
    system("pause");
    return 0;
}

例题2.3

#include <cstdio>
#include <iostream>
#include <stdlib.h>

using namespace std;

int Reverse(int x)
{
    int revx = 0;
    while (x != 0)
    {
        revx *= 10;
        revx += x % 10;
        x /= 10;
    }
    return revx;
}

int main()
{
    for (int i = 0; i <= 256; ++i)
    {
        if (i * i == Reverse(i * i))
        {
            printf("%d\n", i);
        }
    }
    system("pause");
    return 0;
}

习题2.1

#include <cstdio>
#include <iostream>
#include <stdlib.h>

using namespace std;

int main()
{
    int s = 0;
    int x;
    int y;
    int z;
    for (int i; i < 100; i++)
    {
        x = i % 7;
        y = i / 10;
        z = i % 10;
        if (x != 0 && y != 7 && z != 7)
            s += i * i;
    }
    printf("%d\n", s);
    system("pause");
    return 0;
}

习题2.2

#include <cstdio>
#include <iostream>
#include <stdlib.h>

using namespace std;

int main()
{
    int n;
    printf("请输入持有金额:");
    scanf("%d", &n);
    int cock, hen, chicken;
    for (cock = 0; cock <= n / 5; cock++)
    {
        for (hen = 0; hen <= n / 3; hen++)
        {
            for (chicken = 0; chicken <= 3 * n; chicken++)
            {
                if (cock * 5 + hen * 3 + chicken / 3 == n && cock + hen + chicken == n)
                {
                    printf("买大鸡%d只,买小鸡%d只,买小小鸡%d只\n", cock, hen, chicken);
                }
            }
        }
    }
    system("pause");
    return 0;
}

习题2.3

#include <cstdio>
#include <iostream>
#include <stdlib.h>

using namespace std;

int main()
{
    int n, x, y, z, i, j;
    bool flag;
    printf("请输入火鸡数量:");
    while (scanf("%d", &n))
    {
        printf("请输入账单的中间三位数字:");
        scanf("%d", &x);
        scanf("%d", &y);
        scanf("%d", &z);
        int p = x * 1000 + y * 100 + z * 10; //part of number
        flag = true;
        for (i = 9; i >= 1; --i)
        {
            for (j = 9; j >= 0; --j)
            {
                int num = i * 10000 + p + j;
                if (num % n == 0)
                {
                    printf("火鸡总价格为%d%d%d%d%d,火鸡单价为%d\n", i, x, y, z, j, num / n);
                    flag = false;
                    break;
                }
            }
            if (!flag)
                break;
        }
        if (flag)
        {
            printf("账单不存在\n");
        }
    }
    return 0;
}

例题2.4

#include <cstdio>
#include <iostream>
#include <stdlib.h>

using namespace std;
int main()
{
    printf("请输入梯形高度:");
    int h;
    while (scanf("%d", &h) != EOF)
    {
        int hang = h;
        int lie = h + 2 * (h - 1);
        for (int i = 0; i < hang; i++)
        {
            for (int j = 0; j < lie; j++)
            {
                if (j < h + 2 * i)
                {
                    printf("*");
                }
                else
                {
                    printf(" ");
                }
            }
            printf("\n");
        }
    }
    return 0;
}

例题2.5

#include <cstdio>
#include <iostream>
#include <stdlib.h>

using namespace std;

char matrix[80][80];
int main()
{
    int n;
    char a, b;
    bool firstcase = true;
    while (scanf("%d %c %c", &n, &a, &b) == 3)
    {
        if (firstcase == true)
        {
            firstcase = false;
        }
        else
        {
            printf("\n");
        }
        for (int i = 0; i <= n / 2; i++) //(i,i)是每圈左上角坐标
        {
            int j = n - 1 - i; //(j,j)是每圈右下角坐标
            int width = n - 2 * i;
            char c;
            if ((n / 2 - i) % 2 == 0) //判定当前圈为中心花色还是外筐花色
            {
                c = a;
            }
            else
            {
                c = b;
            }
            for (int k = 0; k < width; k++) //为当前圈赋值
            {
                matrix[i][i + k] = c;
                matrix[i + k][i] = c;
                matrix[j][j - k] = c;
                matrix[j - k][j] = c;
            }
        }
        if (n != 1)
        {
            matrix[0][0] = ' ';
            matrix[0][n - 1] = ' ';
            matrix[n - 1][0] = ' ';
            matrix[n - 1][n - 1] = ' ';
        }
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < n; j++)
            {
                printf("%c", matrix[i][j]);
            }
            printf("\n");
        }
    }
    return 0;
}

例题2.6

#include <cstdio>
#include <iostream>
#include <stdlib.h>

using namespace std;

int month_day[2][13] = {
    {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
    {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}};
bool leapyear(int year)
{
    return (year % 100 != 0 && year % 4 == 0) || (year % 400 == 0);
}

int main()
{
    int year, month, day;
    while (scanf("%d%d%d", &year, &month, &day) != EOF)
    {
        int number = 0;
        int tab = leapyear(year);
        for (int i = 0; i < month; i++)
        {
            number += month_day[tab][i];
        }
        number += day;
        printf("%d年%d月%d日是%d年的第%d天。\n", year, month, day, year, number);
    }
    return 0;
}

例题2.7

#include <cstdio>
#include <iostream>
#include <stdlib.h>

using namespace std;

int month_day[2][13] = {
    {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
    {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}};
bool leapyear(int year)
{
    return (year % 100 != 0 && year % 4 == 0) || (year % 400 == 0);
}

int main()
{
    int year, month, day, number;
    while (scanf("%d%d", &year, &number) != EOF)
    {
        int tab = leapyear(year);
        month = 0;
        while (number > month_day[tab][month])
        {
            number -= month_day[tab][month];
            month++;
        }
        day = number;
        printf("%04d-%02d-%02d\n", year, month, day);
    }
    return 0;
}

例题2.8

#include <cstdio>
#include <iostream>
#include <stdlib.h>

using namespace std;

int month_day[2][13] = {
    {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
    {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}};

bool leapyear(int year) //确定是否为闰年
{
    return (year % 100 != 0 && year % 4 == 0) || (year % 400 == 0);
}

int dayofyear(int year) //确定本年总天数
{
    if (leapyear(year))
    {
        return 366;
    }
    else
    {
        return 365;
    }
}

int main()
{
    int year, month, day, number, casenumber;
    scanf("%d", &casenumber);
    while (casenumber--)
    {
        scanf("%d%d%d%d", &year, &month, &day, &number);
        int tab = leapyear(year);
        for (int i = 0; i < month; i++)
        {
            number += month_day[tab][i];
        }
        number += day; //计算为本年第几天
        while (number > dayofyear(year))
        {
            number -= dayofyear(year);
            year++; //判断是否跨年,确定年
        }
        month = 0;
        tab = leapyear(year);
        while (number > month_day[tab][month])
        {
            number -= month_day[tab][month];
            month++; //确定月
        }
        day = number; //确定日
        printf("%04d-%02d-%02d\n", year, month, day);
    }
    system("pause");
    return 0;
}

例题2.9

#include <cstdio>
#include <iostream>
#include <stdlib.h>

using namespace std;

bool tree[10001];

int main()
{
    int L, M;
    while (scanf("%d%d", &L, &M) != EOF)
    {
        for (int i = 0; i <= L; i++)
        {
            tree[i] = true;
        }
        int number = L + 1;
        while (M--)
        {
            int left, right;
            scanf("%d%d", &left, &right);
            for (int i = left; i <= right; i++)
            {
                if (tree[i])
                {
                    tree[i] = false;
                    number--;
                }
            }
        }
        printf("%d", number);
    }
    return 0;
}

例题2.11

#include <cstdio>
#include <iostream>
#include <stdlib.h>

using namespace std;

int main()
{
    int n;
    while (scanf("%d", &n) != EOF)
    {
        if (n == 0)
        {
            break;
        }
        int step = 0;
        while (n != 1)
        {
            if (n % 2 == 0)
            {
                n = n / 2;
            }
            else
            {
                n = (3 * n + 1) / 2;
            }
            step++;
        }
        printf("%d\n", step);
    }
    return 0;
}

代码均正常运行,输入输出符合题目所给样例。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值