除法(Division,UVa 725)
2015-11-08 15:41
447 查看
题意:输入正整数n,按从小到大的顺序输出所有形如abcde/fghij = n的表达式,其中a-j敲好位数字0-9的全排列(可以有前导0),2 <= n <= 79。
思路:枚举abcde,由n算出fghij,进而判断是否满足题意即可。
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<string>
#include<map>
#include<sstream>
#include<queue>
#include<cctype>
using namespace std;
#define MAX 17
typedef long long ll;
int p[11];
bool get(int x) {
int exp = 10000;
while(exp) {
int m = (x / exp) % 10;
exp /= 10;
if(p[m]) return false;
p[m]++;
}
return true;
}
int main() {
int n;
bool flag;
bool first = true;
while(~scanf("%d",&n) && n) {
if(first)
first = false;
else
printf("\n");
flag = true;
for(int i=1000;i<98766;i++) {
memset(p,0,sizeof(p));
if(get(i) && i % n == 0)
if(get(i/n)) {
flag =false;
printf("%05d / %05d = %d\n",i,i/n,n);
}
}
if(flag) {
printf("There are no solutions for %d.\n",n);
}
}
return 0;
}
思路:枚举abcde,由n算出fghij,进而判断是否满足题意即可。
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<string>
#include<map>
#include<sstream>
#include<queue>
#include<cctype>
using namespace std;
#define MAX 17
typedef long long ll;
int p[11];
bool get(int x) {
int exp = 10000;
while(exp) {
int m = (x / exp) % 10;
exp /= 10;
if(p[m]) return false;
p[m]++;
}
return true;
}
int main() {
int n;
bool flag;
bool first = true;
while(~scanf("%d",&n) && n) {
if(first)
first = false;
else
printf("\n");
flag = true;
for(int i=1000;i<98766;i++) {
memset(p,0,sizeof(p));
if(get(i) && i % n == 0)
if(get(i/n)) {
flag =false;
printf("%05d / %05d = %d\n",i,i/n,n);
}
}
if(flag) {
printf("There are no solutions for %d.\n",n);
}
}
return 0;
}
相关文章推荐
- latex公式、编号、对齐
- 2015上海马拉松线上跑感悟-人生如同一场马拉松
- 添加、设置tabBarItem属性
- 性能经验之谈【转】
- Linux中查看GNOME版本号
- 自学php-循环控制语句
- Android View的onTouchEvent和OnTouch区别
- Java实现服务器端对多个客户端请求响应
- linux find命令使用
- IntentService学习笔记
- Linux基础学习(一)
- 内存/硬盘/io关系
- cocos2dx 3.x UI之Button的初使用
- LINUX --grep 小论
- 降级至1.1.835教程
- Nginx反向代理的配置
- 57. mysqli 扩展库(4)
- JMS 企业开发流程实现
- 前端开发面试问题及答案整理
- Python中定义字符串和修改字符串的原理