码迷,mamicode.com
首页 > 其他好文 > 详细

UVA - 471 Magic Numbers

时间:2014-07-10 20:10:06      阅读:238      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   color   strong   

Description

bubuko.com,布布扣


 Magic Numbers 

Write a program that finds and displays all pairs ofintegers bubuko.com,布布扣 and bubuko.com,布布扣 such that:

  1. neither bubuko.com,布布扣 nor bubuko.com,布布扣 have any digits repeated; and
  2. bubuko.com,布布扣 , where N is a given integer;

Input and Output

The input file consist a integer at the beginning indicating the number of testcase followed by  a blank line. Each test case consists of one line of input containing N. Twoinput are separated by a blank line.

For each input the output consists of a sequence of zero or more lines each containing bubuko.com,布布扣 / bubuko.com,布布扣 = N, where bubuko.com,布布扣 and N are the integers described above. When there are two or more solutions, sort them by increasing numerator values.Two consecutive output set will separated by a blank line. 

Sample Input

1

1234567890

Sample Output

1234567890 / 1 = 1234567890
2469135780 / 2 = 1234567890
4938271560 / 4 = 1234567890
6172839450 / 5 = 1234567890
8641975230 / 7 = 1234567890
9876543120 / 8 = 1234567890

题意:求除数和被除数都没有重复的数字,结果为N 的解

思路:首先回溯把所有可能的组成数字储存起来,然后判断去重就行了

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <cstring>
#include <set>
using namespace std;
typedef long long ll;

vector<ll> sum;
char vis[11];

void cal(ll cnt) {
	sum.push_back(cnt);
	for (ll i = 0; i <= 9; i++) {
		if (vis[i] == '1')
			continue;
		vis[i] = '1';
		cal(cnt*10+i);;
		vis[i] = '0';
	}
}

int check(ll num) {
	int tmp[10];
	memset(tmp, 0, sizeof(tmp));
	while (num) {
		int t = num%10;
		if (tmp[t])
			return 0;
		tmp[t] = 1;
		num /= 10;
	}
	return 1;
}

int main() {
	for (ll i = 1; i <= 9; i++) {
		vis[i] = '1';
		cal(i);
		vis[i] = '0';
	}	
	int t;
	ll n;
	scanf("%d", &t);
	while (t--) {
		scanf("%lld", &n);
		set<ll> ans;
		for (ll i = 0; i < sum.size(); i++) {
			if (sum[i]%n == 0)
				if (check(sum[i]/n)) 
					ans.insert(sum[i]/n);
		}
		set<ll>::iterator it;
		for (it = ans.begin(); it != ans.end(); it++) {
			ll p = *it;
			printf("%lld / %lld = %lld\n", n*p, p, n);
		}
		if (t)
			printf("\n");
	}
	return 0;
}



UVA - 471 Magic Numbers,布布扣,bubuko.com

UVA - 471 Magic Numbers

标签:des   style   blog   http   color   strong   

原文地址:http://blog.csdn.net/u011345136/article/details/37593567

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!