uva 725 Division(暴力模拟)
Division
紫书入门级别的暴力,可我还是写了好长时间 = =
【题目链接】uva 725
【题目类型】化简暴力
&题解:
首先要看懂题意,他的意思也就是0~9都只出现一遍,在这2个5位数中。
接着,你要知道:枚举一个5位数就够了,不用2个5位数都枚举,因为你可以通过n知道第2个5位数。
最后set维护出现的次数,ok判断是否可行,pri输出。
【时间复杂度】O(1e5)
&代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f;
#define cle(a,val) memset(a,(val),sizeof(a))
#define SI(N) scanf("%d",&(N))
#define SII(N,M) scanf("%d %d",&(N),&(M))
#define SIII(N,M,K) scanf("%d %d %d",&(N),&(M),&(K))
#define rep(i,b) for(int i=0;i<(b);i++)
#define rez(i,a,b) for(int i=(a);i<=(b);i++)
#define red(i,a,b) for(int i=(a);i>=(b);i--)
const ll LINF = 0x3f3f3f3f3f3f3f3f;
#define PU(x) puts(#x);
int n;
set<int> sei, se2;
vector<pair<int, int> > vep;
bool ok(int d) {
se2 = sei;
int t = d / n;
if (t * n != d) {
return false;
}
int u = 0;
while (t) {
u++;
sei.insert(t % 10);
t /= 10;
}
if (u > 5) {
return false;
}
if (sei.size() == 9 && !sei.count(0) && u == 4) {
return true;
}
if (sei.size() == 10) {
return true;
}
return false;
}
void pri() {
if (vep.empty()) {
printf("There are no solutions for %d.\n", n);
return;
}
int t = vep.size();
rep(i, t)
printf("%05d / %05d = %d\n", vep[i].first, vep[i].second, n);
}
void Solve() {
int uu = 0;
while (~SI(n), n) {
if (uu) PU()
uu = 1;
sei.clear() ;
vep.clear();
rez(i1, 0, 9) {
sei.insert(i1);
rez(i2, 0, 9) {
if (sei.count(i2)) continue;
sei.insert(i2);
rez(i3, 0, 9) {
if (sei.count(i3)) continue;
sei.insert(i3);
rez(i4, 0, 9) {
if (sei.count(i4)) continue;
sei.insert(i4);
rez(i5, 0, 9) {
if (sei.count(i5)) continue;
sei.insert(i5);
int d = i1 * 1e4 + i2 * 1e3 + i3 * 1e2 + i4 * 1e1 + i5;
if (ok(d)) {
pair<int, int> p;
p.first = d;
p.second = d / n;
vep.push_back(p);
}
sei = se2;
sei.erase(i5);
}
sei.erase(i4);
}
sei.erase(i3);
}
sei.erase(i2);
}
sei.erase(i1);
}
pri();
}
}
int main() {
Solve();
return 0;
}
上面是按位搜索的暴力,代码较长,下面是直接枚举的暴力,枚举枚举范围1234~98765,之后再判断,这样写代码就较短了。我枚举的是第一个5位数,当然,也可以枚举第二个,你自己可以试下。
&代码2:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f;
#define cle(a,val) memset(a,(val),sizeof(a))
#define SI(N) scanf("%d",&(N))
#define SII(N,M) scanf("%d %d",&(N),&(M))
#define SIII(N,M,K) scanf("%d %d %d",&(N),&(M),&(K))
#define rep(i,b) for(ll i=0;i<(b);i++)
#define rez(i,a,b) for(ll i=(a);i<=(b);i++)
#define red(i,a,b) for(ll i=(a);i>=(b);i--)
const ll LINF = 0x3f3f3f3f3f3f3f3f;
#define PU(x) puts(#x);
#define PI(A) cout<<(A)<<endl;
#define DG(x) cout<<#x<<"="<<(x)<<endl;
#define DGG(x,y) cout<<#x<<"="<<(x)<<" "<<#y<<"="<<(y)<<endl;
#define DGGG(x,y,z) cout<<#x<<"="<<(x)<<" "<<#y<<"="<<(y)<<" "<<#z<<"="<<(z)<<endl;
#define PIar(a,n) rep(i,n)cout<<a[i]<<" ";cout<<endl;
#define PIarr(a,n,m) rep(aa,n){rep(bb, m)cout<<a[aa][bb]<<" ";cout<<endl;}
const double EPS = 1e-9 ;
/* //////////////////////// C o d i n g S p a c e //////////////////////// */
const int MAXN = 1000 + 5 ;
int n;
bool used[10];
bool ok(int x, int y) {
if (y*n!=x) return false;
cle(used, 0);
//这是1e4 不是1e5
if (x < 10000) used[0] = 1;
if (y < 10000) used[0] = 1;
int u1 = 0, u2 = 0;
while (x) {
used[x % 10] = 1;
x /= 10;
u1++;
}
while (y) {
used[y % 10] = 1;
y /= 10;
u2++;
}
int c = 0;
if (u1 < 6 && u2 < 6)
rep(i, 10) if (used[i]) c++;
return (c == 10);
}
void Solve() {
int kg = 0;
while (~SI(n), n) {
if (kg)puts("");
kg = -1;
for (int i = 1234; i <= 98765; i++)
if (ok(i, i / n)) {
printf("%05d / %05d = %d\n", i, i / n, n);
kg = 1;
}
if (kg == -1) printf("There are no solutions for %d.\n", n);
}
}
int main() {
Solve();
return 0;
}
uva 725 Division(暴力模拟)的更多相关文章
- UVA.725 Division (暴力)
UVA.725 Division (暴力) 题意分析 找出abcdefghij分别是0-9(不得有重复),使得式子abcde/fghij = n. 如果分别枚举每个数字,就会有10^10,肯定爆炸,由 ...
- uva 725 DIVISION (暴力枚举)
我的56MS #include <cstdio> #include <iostream> #include <string> #include <cstrin ...
- 暴力枚举 UVA 725 Division
题目传送门 /* 暴力:对于每一个数都判断,是否数字全都使用过一遍 */ #include <cstdio> #include <iostream> #include < ...
- uva 725 Division(除法)暴力法!
uva 725 Division(除法) A - 暴力求解 Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & ...
- UVA 725 division【暴力枚举】
[题意]:输入正整数n,用0~9这10个数字不重复组成两个五位数abcde和fghij,使得abcde/fghij的商为n,按顺序输出所有结果.如果没有找到则输出“There are no solut ...
- UVa 725 Division (枚举)
题意 : 输入正整数n,按从小到大的顺序输出所有形如abcde/fghij = n的表达式,其中a-j恰好为数字0-9的一个排列(可以有前导0),2≤n≤79. 分析 : 最暴力的方法莫过于采用数组存 ...
- Uva 725 Division
0.不要傻傻的用递归去构造出一个五位数来,直接for循环最小到最大就好,可以稍微剪枝一丢丢,因为最小的数是01234 从1234开始,因为倍数n最小为2 而分子是一个最多五位数,所以分母应该小于五万. ...
- uva 725 division(水题)——yhx
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABVMAAAOHCAIAAAClwESxAAAgAElEQVR4nOydybGturJFcQEPfgQu4A
- UVA 725 – Division
Description Write a program that finds and displays all pairs of 5-digit numbers that between them ...
随机推荐
- CDH hadoop的安装
1 先拷贝tar包到目录底下(tar 包解压 tar zxvf) 2 : 1.使用课程提供的hadoop-2.5.0-cdh5.3.6.tar.gz,上传到虚拟机的/usr/local目录下.(htt ...
- Android项目——传感器的使用
public class MainActivity extends Activity { // 定义 方向传感器 和 重力传感器 private TextView tvOrientation, tvA ...
- 黑马程序员——JAVA基础之Map集合
------- android培训.java培训.期待与您交流! ---------- Map集合: 该集合存储键值对.一对一对往里存.而且要保证键的唯一性. 和Set很像,其实Set底层就是使用了M ...
- bootstrap源码里的function加上了+号
一般看JQuery插件里的写法是这样的 (function($) { //... })(jQuery); 今天看到bootstrap的javascript组件是这样写的 !function( $ ){ ...
- /usr/include/gnu/stubs.h:7:27: error: gnu/stubs-32.h:No such file or directory的解决办法
在编译32位HDecode时出现如题所示的错误,原因时没有安装32位glibc库导致的: ubuntu: sudo apt-get install libc6-dev-i386 CentOS:yum ...
- WaitAny, WaitAll 和 SignalAndWait
除了Set 和 WaitOne方法外,在类WaitHandle中还有一些用来创建复杂的同步过程的静态方法. WaitAny, WaitAll 和 SignalAndWait使跨多个可能为不同类型的等待 ...
- 市委组织部考核项目——利用EasyUi中可编辑的DataGrid控件对多行数据进行编辑并提交
http://blog.csdn.net/cjr15233661143/article/details/19041165 市委组织部考核项目中需要录入原始数据,拿开发区的数据录入举例说明,见下图,需要 ...
- ManifoldJS
微软推出开源工具ManifoldJS,可以自动将Web App转换成各种平台的App(安卓.iOS.Chrome OS.Windows). ManifoldJS通过获取网站meta-data信息,即可 ...
- Java Dns Cache Manipulator
通过代码直接设置Java的DNS(实际上设置的是DNS Cache),支持JDK 6+. 功能 设置/重置DNS(不会再去Lookup DNS) 可以设置单条 或是通过Properties文件批量设 ...
- [JAVA] java程序性能优化
一.避免在循环条件中使用复杂表达式 在不做编译优化的情况下,在循环中,循环条件会被反复计算,如果不使用复杂表达式,而使循环条件值不变的话,程序将会运行的更快. 例子: import java.util ...