其他分享
首页 > 其他分享> > 随手练——洛谷-P1008 三连击(暴力亦需有头脑)

随手练——洛谷-P1008 三连击(暴力亦需有头脑)

作者:互联网

 

第一眼看到这个题,我脑海里就是,“我们是不是在哪里见过~”,大一刚学C语言的时候写过一个类似的题目,写了九重循环。。。。就像这样(在洛谷题解里看到一位兄台写的。。。。超长警告,慎重点开

#include <stdio.h>
#include <cstdlib>
int main()
{
    int i[9];
    for (i[0] = 1; i[0] <= 9; i[0]++)
    {
        for (i[1] = 1; i[1] <= 9; i[1]++)
        {
            int p1=0;
            if (i[1] == i[0]) p1 = 1;
            if (p1 != 1) {
                for (i[2] = 1; i[2] <= 9; i[2]++)
                {
                    int p2=0;
                    for (int j2 = 0; j2 < 2; j2++) if (i[2] == i[j2]) p2 = 2;
                    if (p2 != 2) {
                        for (i[3] = 1; i[3] <= 9; i[3]++)
                        {
                            int p3=0;
                            for (int j3 = 0; j3 < 3; j3++) if (i[3] == i[j3]) p3 = 3;
                            if (p3 != 3) {
                                for (i[4] = 1; i[4] <= 9; i[4]++)
                                {
                                    int p4=0;
                                    for (int j4 = 0; j4 < 4; j4++) if (i[4] == i[j4]) p4 = 4;
                                    if (p4 != 4) {
                                        for (i[5] = 1; i[5] <= 9; i[5]++)
                                        {
                                            int p5=0;
                                            for (int j5 = 0; j5 < 5; j5++) if (i[5] == i[j5]) p5 = 5;
                                            if (p5 != 5) {
                                                for (i[6] = 1; i[6] <= 9; i[6]++)
                                                {
                                                    int p6=0;
                                                    for (int j6 = 0; j6 < 6; j6++) if (i[6] == i[j6]) p6 = 6;
                                                    if (p6 != 6) {
                                                        for (i[7] = 1; i[7] <= 9; i[7]++)
                                                        {
                                                            int p7=0;
                                                            for (int j7 = 0; j7 < 7; j7++) if (i[7] == i[j7]) p7 = 7;
                                                            if (p7 != 7) {
                                                                for (i[8] = 1; i[8] <= 9; i[8]++)
                                                                {
                                                                    int p8=0;
                                                                    for (int j8 = 0; j8 < 8; j8++) if (i[8] == i[j8]) p8 = 8;
                                                                    if (p8 != 8) {
                                                                        //printf("%d %d %d %d %d %d %d %d %d\n", i[0], i[1], i[2], i[3], i[4], i[5], i[6], i[7], i[8]);
                                                                        int a = 100 * i[0] + 10 * i[1] + i[2];
                                                                        int b = 100 * i[3] + 10 * i[4] + i[5];
                                                                        int c = 100 * i[6] + 10 * i[7] + i[8];
                                                                        double d1 = double(a) / b;
                                                                        double d2 = double(c) / b;
                                                                        if (d1 == 0.5 && d2 == 1.5)
                                                                        {
                                                                            printf("%d %d %d\n", a, b, c);
                                                                            //system("pause");
                                                                        }
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    system("pause");
    return 0;
}
View Code

假设这三个数是 x : y : z  = 1 : 2 : 3,那么z最大可以为987,那么x最大到329(其实比329还要小几个数,因为数字重复了,懒得去抠了),那就好办了,x 从123循环到329,判重就完事了。

#include <iostream>
#include <string.h>
using namespace std;
int a[10];
//判重,且不能带0
int IsRepeat(int x) {
    memset(a, 0, sizeof(int) * 10);
    while (x) {
        int t = ++a[x % 10];
        if (t == 2 || a[0] == 1) 
            return 1;
        x /= 10;
    }
    return 0;
}
int IsRepeat2(int x,int y,int z) {
    memset(a, 0, sizeof(int) * 10);
    while (x) {
        a[x % 10]++; a[y % 10]++; a[z % 10]++;
        if (a[0] > 0) return 1;
        x /= 10; y /= 10; z /= 10;
    }
    for (int i = 1; i < 10; i++) 
        if (a[i] > 1) return 1;
    return 0;
}
int main() {
    //x,y,z 1:2:3,z最大可以为987,那么1最大到329
    for (int x = 123; x <= 329; x++) {
        if (IsRepeat(x)) {
            continue;
        }
        //如果x不重复,y,z 也不会重复
        int y = x * 2; int z = x * 3;
        if (!IsRepeat2(x, y, z)) {
            cout << x << " " << y << " " << z << endl;
        }
    }
    return 0;
}

 

标签:10,连击,洛谷,int,329,亦需,++,return,include
来源: https://www.cnblogs.com/czc1999/p/10360320.html