Vampire Numbers(HDU-4235)

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.
 

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
 
代码如下:

#include<bits/stdc++.h>
using namespace std;
int m[800],cnt=0;
int main()
{
    set<int>vn;
    for(int i=2;i<=1010;i++)
        for(int j=i;i*j<=1000500;j++)
        {
            int vis[10]={0},x=i,y=j,s=i*j,flag=1;
            while(x){vis[x%10]++;x=x/10;};
            while(y){vis[y%10]++;y=y/10;};
            while(s){vis[s%10]--;s=s/10;};
            for(int k=0;k<=9;k++)
                if(vis[k])
                {
                    flag=0;
                    break;
                }
            if(flag) m[cnt++]=i*j;
        }
        sort(m,m+cnt);
    int n;
    while(cin>>n&&n!=0)
    {
        cout<<m[lower_bound(m,m+cnt,n)-m]<<endl;
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值