<NOIP> 7 . P1008 三连击

题解:这是洛谷的第8道题目,考察的是循环(while、do while、for)的使用。

解决办法:

1 . 直接暴力,从100至999,作为可能要输出的第一个数;

2 . 第二个数字、第三个数字分别是第一个数字的2、3倍,直接可以生成后两个数字;

3 . 筛选(由于三个数的每一个位上的数字都是不同的,并且这三个数字都是三位数)

源代码:

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

using namespace std;

bool check(int *first, int *second)
{
    if (first[0] != second[0] && first[0] != second[1] && first[0] != second[2] &&
        first[1] != second[0] && first[1] != second[1] && first[1] != second[2] &&
        first[2] != second[0] && first[2] != second[1] && first[2] != second[2] &&
        first[0]!=first[1] && first[0]!=first[2]&&first[1]!=first[2] &&
        second[0]!= second[1] && second[0]!= second[2] && second[1]!= second[2] &&
        first[1]!=0 && first[2]!=0 && second[1]!=0&&second[2]!=0)
        return true;
    else
        return false;
}

int main()
{
    for (size_t i = 100; i < 1000; i++)
    {
        int a = i * 1;
        int b = i * 2;
        int c = i * 3;

        int d = a;
        int e = b;
        int f = c;

        if (b < 1000 && c < 1000)
        {
            // 提取位数;
            int aArray[3],
                bArray[3],
                cArray[3];

            for (size_t i = 0; i < 3; i++)
            {
                aArray[i] = a % 10; a /= 10;
                bArray[i] = b % 10; b /= 10;
                cArray[i] = c % 10; c /= 10;
            }

            if (check(aArray, bArray) && check(aArray, cArray) && check(bArray, cArray))
                cout << d << " " << e << " " << f << endl;
        }
    }

    system("pause");
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值