1. 程式人生 > >UVA 725 Division(除法)

UVA 725 Division(除法)

輸入正整數n,按從小到大的順序輸出所有形如abcde/fghij=n的表示式,其中a~j恰好為數字0~9的一個排列,2\leqslantn\leqslant79。

題解:暴力破解列舉fghij。

#include<iostream>
#include <string>
#include <string.h>
#include<vector>
#include<stack>
#include<queue>
#include<stdio.h>
#include<stdlib.h>
#include<iomanip>
using namespace std;
const int maxn = 50000;
int main()
{
int kase=0;
	int a[5], b[5];
	int x = 0; while (cin >> x && x) {
                 if(kase)cout<<endl;
                 kase++;
		int c = 99999 / x; bool iscout = false;
		for (int i = 1234; i <= c; i++) {
			int k = i;
			for (int z = 0; z < 5; z++) {
				a[z] = k % 10; k /= 10;
			}
			bool isequal = false;
			for (int z = 0; z < 5; z++) {
				for (int j = z + 1; j < 5; j++) {
					if (a[z] == a[j]) {
						isequal = true; break;
					}
				}
			}
			if (isequal)continue;
			int to = i * x;
			if (to >= 100000)break;
			int tx = to;
			for (int z = 0; z < 5; z++) {
				b[z] = tx % 10; tx /= 10;
			}
			isequal = false;
			for (int z = 0; z < 5; z++) {
				for (int j = z + 1; j < 5; j++) {
					for (int r = 0; r < 5; r++)if (b[z] == a[r] || b[j] == a[r]) {
						isequal = true; break;
					}
					if (b[z] == b[j]) {
						isequal = true; break;
					}
				}
			}
			if (isequal)continue;
			/*cout << to << " / " << right << setw(5) << setfill('0') << i << " = " << x << endl;*/
			printf("%05d / %05d = %d\n", to, i, x);
			iscout = true;
		}
		if (!iscout)printf("There are no solutions for %d.\n", x);
	}

	//system("pause");
	return 0;
}