您的位置:首页 > 其它

uva 10396 - Vampire Numbers(暴力)

2014-01-08 14:25 357 查看
题目链接:uva 10396 - Vampire Numbers

题目大意:输入n,找出为数为n的吸血鬼数字。吸血鬼数字即偶数,并且可以拆分成两个不同为10的倍数的数,相乘为它本身。

解题思路:枚举拆分后的两个数,然后判断。

#include <stdio.h>
#include <string.h>
#include <set>
#include <algorithm>

using namespace std;

const int N = 6;
const int tmp
= {0, 1, 10, 100, 1000, 10000};
int n, rec
;
set<int> v
;

bool judge(int a, int b) {
int c[10], s = a * b;
memset(c, 0, sizeof(c));
while (a) {
c[a%10]++; a /= 10;
}
while (b) {
c[b%10]++; b /= 10;
}
while (s) {
c[s%10]--; s /= 10;
}
for (int i = 0; i < 10; i++) if (c[i] != 0) return false;
return true;
}

void solve() {
int k = n / 2;
if (rec[k] == 0) {
for (int i = tmp[k]; i < tmp[k+1]; i++) {
for (int j = i; j < tmp[k+1]; j++) {
if (i % 10 == 0 && j % 10 == 0) continue;
if (i % 2 && j % 2) continue;
if (judge(i, j)) v[k].insert(i * j);
}
}
}
rec[k] = 1;
for (set<int>::iterator i = v[k].begin(); i != v[k].end(); i++)
printf("%d\n", *i);
printf("\n");
}

int main() {
memset(rec, 0, sizeof(rec));
while (scanf("%d", &n) == 1) {
solve();
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: