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

UVA - 725

时间:2014-11-26 14:08:05      阅读:157      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   ar   color   os   sp   for   

  Division 

Write a program that finds and displays all pairs of 5-digit numbers that between them use the digits 0 through 9 once each, such that the first number divided by the second is equal to an integer N, where bubuko.com,布布扣. That is,

 


abcde / fghij = N

where each letter represents a different digit. The first digit of one of the numerals is allowed to be zero.

 

Input 

Each line of the input file consists of a valid integer N. An input of zero is to terminate the program.

 

Output 

Your program have to display ALL qualifying pairs of numerals, sorted by increasing numerator (and, of course, denominator).

Your output should be in the following general form:

 


xxxxx / xxxxx = N

xxxxx / xxxxx = N

.

.

 


In case there are no pairs of numerals satisfying the condition, you must write ``There are no solutions for N.". Separate the output for two different values of N by a blank line.

 

Sample Input 

61
62
0

 

Sample Output 

There are no solutions for 61.

79546 / 01283 = 62
94736 / 01528 = 62

 

 

bubuko.com,布布扣
 1 #include <iostream>
 2 #include <cstring>
 3 #include <cstdio>
 4 #include <set>
 5 
 6 using namespace std;
 7 
 8 bool check(int x,int y) {
 9     char ch1[5];
10     char ch2[5];
11     sprintf(ch1,"%05d",x);
12     sprintf(ch2,"%05d",y);
13     set<int>s;
14     for (int i = 0;i < 5;i++) {
15         s.insert(ch1[i]);
16         s.insert(ch2[i]);
17     }
18     if (s.size() == 10) return 1;
19     else return 0;
20 }
21 
22 int main () {
23     int n;
24     int pos = 0;
25     while (cin >> n,n) {
26         int ok = 0;
27         if (pos++) cout << endl;
28         for (int i = 1234;i <= 98765;i++) {
29             if (i * n > 100000) break;
30             if (check(i,i * n)) {
31                 printf("%05d / %05d = %d\n",i * n, i , n);
32                 ok = 1;
33             }
34         }
35         if (!ok) printf("There are no solutions for %d.\n",n);
36     }
37 }
View Code

 

UVA - 725

标签:style   blog   http   io   ar   color   os   sp   for   

原文地址:http://www.cnblogs.com/xiaoshanshan/p/4122974.html

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