Vampire Numbers - STL - set应用实例

9 篇文章 0 订阅

set总结

insert(key_value);将key_value插入到set中,返回值是pair< set< int>::iterator,bool>,bool标志着插入是否成功,而iterator代表插入的位置,若key_value已经在set中,则iterator表示的key_value在set中的位置。
lower_bound(key_value),返回第一个大于等于key_value的定位器
upper_bound(key_value),返回最后一个大于等于key_value的定位器
count(),用来查找set中某个某个键值出现的次数。这个函数在set并不是很实用,因为一个键值在set只可能出现0或1次,这样就变成了判断某一键值是否在set出现过了。
clear(),删除set容器中的所有的元素
empty(),判断set容器是否为空
max_size(),返回set容器可能包含的元素最大个数
size(),返回当前set容器中的元素个数
STL-set用法参考网址http://blog.csdn.net/yimeng2013/article/details/8882216

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

题意

一个数x,能把它拆分为a,b,如果a*b=s,则它是Vampire数。
要求输入n,求出大于等于数n的最小Vampire数。

代码

#include<iostream>
#include<set>
#include<algorithm>
using namespace std;

//int ans[1000];
set<int> Vampire;
void prepare()
{
    for(int a=1;a<=1010;a++)//较小约数
    {
        for(int b=1;b<1000500/a;b++)//另一个约数
        {
            int temp[10]={0};
            int x=a*b;//可能成为Vampire Numbers的数
            int temp_x=x;
            int temp_a=a;
            int temp_b=b;
            bool flag=true;
            while(temp_x){temp[temp_x%10]++;temp_x/=10;}
            while(temp_a){temp[temp_a%10]--;temp_a/=10;}
            while(temp_b){temp[temp_b%10]--;temp_b/=10;}
            for(int k=0;k<10;k++)
            {
                if(temp[k]){
                    flag=false;
                }
            }
            if(flag){
                Vampire.insert(x);//如果是Vampire Numbers则插入
            }
        }
    }
}
int main()
{
    prepare();//591个Vampire
    /*for(set<int>::iterator it=Vampire.begin();it!=Vampire.end();it++)
    {
        cout<<*it<<",";
    }*/
    int n;
    while(cin>>n &&n!=0)
    {
        cout<<*Vampire.lower_bound(n)<<endl;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值