# 題目: UVa 725 - Division
# 題目說明
輸入一個範圍 2 ~ 79 的整數 N ,找到 abcde / fghij = Nabcdefghij 為 0 ~ 9 不重複數字
INPUT:
每筆測資輸入一個整數 N
當 N 為 0 時結束
OUTPUT:
輸出所有符合條件的 abcde / fghij = N ,以升冪排序
如果都不符合條件,輸出 There are no solutions for N
# 解題方法
採取暴力破解法
先將條件置換成 abcde / N = fghijabcde 的範圍為 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;  | |
} |