C++12!配对

题目内容:找出输入数据中所有两两相乘的积为12!的对数。

输入描述:输入数据中含有一些整数n(1<=n<232)

输出描述:输出所有两两相乘的积为12!的对数。

题目分析:对于输入的每个整数,首先判断该整数是否是12!的约数,若是,则从多重集合中查找该数对应的因子,对数加一,然后从多重集合中删除该因子;若不是,则先将该数插入到多重集合中,以供后面输入的数查找对应因子。

参考代码:

#include <fstream>
#include <iostream>
#include <set>

using namespace std;
int main(int argc,char * argv[])
{
    int num=0;
    int f12=479001600;
    multiset<unsigned int> s;
    int n;
    while(cin>>n)
    {
        if(f12%n==0)
        {
            multiset<unsigned int>::iterator it=s.find(f12/n);
            if(it!=s.end())
            {
                num++;
                s.erase(it);
            }
            else
                s.insert(n);
        }
        if(cin.get()=='
')
        {
            break;
        }
    }
    cout<<num<<endl;
    system("pause");
    return 0;
}

运行结果:

原文地址:https://www.cnblogs.com/cysolo/p/3381052.html