UVA725 - 725 - Division
2015-07-15 19:52
357 查看
Division
Time limit: 3.000 seconds
Write a program that finds and displays all pairs of 5-digit numbers that between them use the digits 0through 9 once each, such that the first number
divided by the second is equal to an integer N, where
. 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.
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 forN.". Separate the output for two different values of N by
a blank line.
Sample Input
Time limit: 3.000 seconds
Write a program that finds and displays all pairs of 5-digit numbers that between them use the digits 0through 9 once each, such that the first number
divided by the second is equal to an integer N, where
. 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 forN.". Separate the output for two different values of N by
a blank line.
Sample Input
61620Sample Output
There are no solutions for 61. 79546 / 01283 = 6294736 / 01528 = 62 一个简单的枚举题目,枚举0~9的所有排列时没有必要的!而且也是不可实现的!只需枚举题目中的 fghij 就可以算出 abcde, 然后再判断所有数字都不相同即可!#include <stdio.h> #include <iostream> #include <string.h> #include <algorithm> using namespace std; int solve(int a, int b) { int bj[10], num[10], aa = a, bb = b; /// bj数组用来记录每一位出现的次数,num数组用来记录是否出现 memset(bj,0,sizeof(bj)); memset(num,0,sizeof(num)); if(b > 98765) return 0; if(a < 10000) {num[0] = 1;bj[0] = 1;}///小于10000,存在一个前导0 while(a) { num[a%10] = 1; bj[aa%10]++; a /= 10; aa /= 10; } while(b) { num[b%10] = 1; bj[bb%10]++; b /= 10; bb /= 10; } int sum = 0; for(int j = 0; j < 10; j++) if(bj[j] == 1) sum += num[j]; return (sum == 10); } int main() { int n, t = 0; while(scanf("%d",&n)&&n!=0) { if(t++) printf("\n"); int cnt = 0; for(int i = 1234; i < 100000; i++) { if(solve(i,i*n)) { printf("%05d / %05d = %d\n",i*n,i,n); cnt++; } } if(!cnt) printf("There are no solutions for %d.\n",n); } return 0; }
相关文章推荐
- 机器学习的数学基础(1)--Dirichlet分布
- 查看 SELinux状态及关闭SELinux
- android studio中文件内容查找相当于Eclipse中Ctrl+H & 查找项目中所有中文
- DataList和Repeater
- POJ 3026 (BFS+最小生成树)
- B. Case of Fake Numbers
- 如何防止头文件被重复包含、嵌套包含
- 【Python】容器类
- java之多线程
- MyEclipse Servers视窗出现“Could not create the view: An unexpected exception was thrown”错误解决办法
- 嵌入式系统bootloader详解
- 机房收费系统之功能实现
- springmvc+mongodb+maven 项目搭建配置
- poj 2528 线段覆盖 求能看到的线段有几段 (线段切割) poj 3277 (矩形切割)
- 【独立开发者er Cocos2d-x实战 009】Cocos2dx 菜单项CCMenu使用
- ajax批量上传图片
- android产生8位16进制的随机数
- JDBC代码规范化
- redis之sentinel
- hdu 3304 Interesting Yang Yui Triangle