随手练——洛谷-P1008 / P1618 三连击(暴力搜索)
1.普通版
第一眼看到这个题,我脑海里就是,“我们是不是在哪里见过~”,去年大一刚学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; }
假设这三个数是 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; }
2.升级版
就是把原来的比例从1:2:3,变成了A:B:C。
思路还是差不多,中间continue判断,写的好像有点乱,但是确实不能少。
#include <iostream> using namespace std; bool IsRepeat(int x) { int a[10] = { 0 }; while (x) { int t = a[x % 10]++; if (t == 2 || a[0] == 1) return true; x /= 10; } return false; } bool IsRepeat3(int x, int y, int z) { int a[10] = { 0 }; while (x) { a[x % 10]++; a[y % 10]++; a[z % 10]++; if (a[0] > 0) return true; x /= 10; y /= 10; z /= 10; } for (int i = 1; i < 10; i++) if (a[i] > 1) return true; return false; } int main() { int A, B, C, flag = 0; cin >> A >> B >> C; for (int i = 123; i < 987; i++) { int j, k; if (IsRepeat(i))continue; if (i * B % A == 0) j = i * B / A; else continue; if (j > 987)continue; if (j*C%B == 0) { k = j * C / B; if (k > 987 || IsRepeat3(i, j, k))continue; flag = 1; cout << i << " " << j << " " << k << endl; } } if (!flag)cout << "No!!!" << endl; return 0; }