Vampire Numbers
Time Limit: 5000/1500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 437 Accepted Submission(s): 210
Problem Description
The number 1827 is an interesting number, because 1827=21*87, and all of the same digits appear on both sides of the ‘=’. The number 136948 has the same property: 136948=146*938.
Such numbers are called Vampire Numbers. More precisely, a number v is a Vampire Number if it has a pair of factors, a and b, where a*b=v, and together, a and b have exactly the same digits, in exactly the same quantities, as v. None of the numbers v, a or b can have leading zeros. The mathematical definition says that v should have an even number of digits and that a and b should have the same number of digits, but for the purposes of this problem, we’ll relax that requirement, and allow a and b to have differing numbers of digits, and v to have any number of digits. Here are some more examples:
126 = 6 * 21
10251 = 51 * 201
702189 = 9 * 78021
29632 = 32 * 926
Given a number X, find the smallest Vampire Number which is greater than or equal to X.
Such numbers are called Vampire Numbers. More precisely, a number v is a Vampire Number if it has a pair of factors, a and b, where a*b=v, and together, a and b have exactly the same digits, in exactly the same quantities, as v. None of the numbers v, a or b can have leading zeros. The mathematical definition says that v should have an even number of digits and that a and b should have the same number of digits, but for the purposes of this problem, we’ll relax that requirement, and allow a and b to have differing numbers of digits, and v to have any number of digits. Here are some more examples:
126 = 6 * 21
10251 = 51 * 201
702189 = 9 * 78021
29632 = 32 * 926
Given a number X, find the smallest Vampire Number which is greater than or equal to X.
Input
There will be several test cases in the input. Each test case will consist of a single line containing a single integer X (10 ≤ X ≤ 1,000,000). The input will end with a line with a single 0.
Output
For each test case, output a single integer on its own line, which is the smallest Vampire Number which is greater than or equal to X. Output no extra spaces, and do not separate answers with blank lines.
Sample Input
10 126 127 5000 0
Sample Output
126 126 153 6880
Source
C语言实现
//题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4235
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int cmp(const void *a, const void *b)
{ return *(int *)a - *(int *)b;}
int main (void)
{
int ans[1000];
int temp[10];
int i , j, k, s, n, count ,flag;
int temp_s, temp_i, temp_j;
count = 0;
for(i = 1; i <= 1010; i++) //前一个约数(小的)
for(j = i ; j < 1000500 / i; j++) //后一个约数(大的)
{
s = i * j;
temp_s = s;
temp_i = i;
temp_j = j;
memset(temp,0,sizeof(temp));
while(temp_s){temp[temp_s%10]++; temp_s /= 10;}
while(temp_i){temp[temp_i%10]--; temp_i /= 10;}
while(temp_j){temp[temp_j%10]--; temp_j /= 10;}
flag = 1;
for(k = 0; k < 10; k++)
if(temp[k])
{flag = 0;break;}
if(flag)
ans[count++] = s;
}
qsort(ans,count,sizeof(ans[0]),cmp);
while(scanf("%d", &n)!=EOF)
{
if(!n) return 0;
for(i = 0;i <= 712; i++)
if(n <= ans[i])
{
printf("%d\n", ans[i]);
break;
}
}
return 0;
}
C++ 实现
#include<cstdio>
#include<string>
#include<set>
#include <numeric>
#include <algorithm>
using namespace std;
int main(void)
{
int i, j, n, s;
int a, b, c, num[10];
set<int> vampire;
for(i = 1; i < 1010; i++) //较小约数
{
for(j = i; j < 1001000 / i ; j++) //另一个约数
{
s = i * j; //可能成为Vampire Numbers的数
a = s; b = i; c = j;
memset(num, 0, sizeof(num));
while(a) { num[a % 10]++; a /= 10; }
while(b) { num[b % 10]--; b /= 10; }
while(c) { num[c % 10]--; c /= 10; }
if(find_if(num, num + 10, bind2nd(not_equal_to<int>(), 0)) == num + 10) //如果是Vampire Numbers则保存
vampire.insert(s);
}
}
while(scanf("%d", &n) && n){
printf("%d\n", *vampire.lower_bound(n));
}
return 0;
}