# 題目: UVa 725 - Division
# 題目說明
輸入一個範圍2 ~ 79的整數N,找到 abcde / fghij = N
abcdefghij為0 ~ 9不重複數字
INPUT:
每筆測資輸入一個整數N
當N為0時結束
OUTPUT:
輸出所有符合條件的abcde / fghij = N,以升冪排序
如果都不符合條件,輸出There are no solutions for N
# 解題方法
採取暴力破解法
先將條件置換成abcde / N = fghij
abcde的範圍為01234 ~ 98765,設一個變數i除以N
若i % N = 0則進行下一步判斷
設一個變數num = i / N
若num與i中擁有0123456789所有元素,則符合條件,存入priority queue
最後再依序輸出
# 參考程式碼
#include <iostream>
#include <sstream>
#include <algorithm>
#include <queue>
#define p pair<string, string>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
cout.tie(nullptr);
cin.tie(nullptr);
int N, cnt = 0;
string s = "0123456789";
while (cin >> N, N)
{
priority_queue<p, vector<p>, greater<p>> ans;
for (int i = 98765; i >= 1234; --i) if (i % N == 0)
{
int num = i / N;
string str;
string t1, t2;
stringstream ss1, ss2;
ss1 << i, ss1 >> t1;
ss2 << num, ss2 >> t2;
str = t1 + t2;
if (str.size() == 9) str += '0';
sort(str.begin(), str.end());
if (str == s) ans.push({ t1, t2});
}
if (cnt++) cout << "\n";
if (ans.empty()) cout << "There are no solutions for " << N << ".\n";
else
{
while (!ans.empty())
{
auto& [i, j] = ans.top();
cout << i << " / " << (j.size() == 4 ? "0" : "") << j << " = " << N << "\n";
ans.pop();
}
}
}
return 0;
}