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(暴力模拟)的更多相关文章

  1. UVA.725 Division (暴力)

    UVA.725 Division (暴力) 题意分析 找出abcdefghij分别是0-9(不得有重复),使得式子abcde/fghij = n. 如果分别枚举每个数字,就会有10^10,肯定爆炸,由 ...

  2. uva 725 DIVISION (暴力枚举)

    我的56MS #include <cstdio> #include <iostream> #include <string> #include <cstrin ...

  3. 暴力枚举 UVA 725 Division

    题目传送门 /* 暴力:对于每一个数都判断,是否数字全都使用过一遍 */ #include <cstdio> #include <iostream> #include < ...

  4. uva 725 Division(除法)暴力法!

    uva 725  Division(除法) A - 暴力求解 Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & ...

  5. UVA 725 division【暴力枚举】

    [题意]:输入正整数n,用0~9这10个数字不重复组成两个五位数abcde和fghij,使得abcde/fghij的商为n,按顺序输出所有结果.如果没有找到则输出“There are no solut ...

  6. UVa 725 Division (枚举)

    题意 : 输入正整数n,按从小到大的顺序输出所有形如abcde/fghij = n的表达式,其中a-j恰好为数字0-9的一个排列(可以有前导0),2≤n≤79. 分析 : 最暴力的方法莫过于采用数组存 ...

  7. Uva 725 Division

    0.不要傻傻的用递归去构造出一个五位数来,直接for循环最小到最大就好,可以稍微剪枝一丢丢,因为最小的数是01234 从1234开始,因为倍数n最小为2 而分子是一个最多五位数,所以分母应该小于五万. ...

  8. uva 725 division(水题)——yhx

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABVMAAAOHCAIAAAClwESxAAAgAElEQVR4nOydybGturJFcQEPfgQu4A

  9. UVA 725 – Division

    Description   Write a program that finds and displays all pairs of 5-digit numbers that between them ...

随机推荐

  1. HTML 5 canvas —— 基本语法

    目录 简述 canvas 基础 2D context API 基本线条 路径 插入图像 像素级操作 文字 阴影 颜色渐变 小节 简述 HTML 5 规范引进了很多新特性,其中最令人期待的之一就是 ca ...

  2. 《剑指Offer》之二维数组中的查找

    1.题目描述 在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数. 2.代码实现 pu ...

  3. Windows Kernel Security Training Courses

    http://www.codemachine.com/courses.html#kerdbg Windows Kernel Internals for Security Researchers Thi ...

  4. note: declarations in dependent base ‘std::basic_ios<char>’ are not found by unqualified lookup

    错误信息如题所示. 修改前: template< typename _CharT, typename _Traits = std::char_traits<_CharT> > ...

  5. Unity3D 几个基本动画(控制物体移动、旋转、缩放)

    Transform基本移动函数: 1.指定方向移动: //移动速度 float TranslateSpeed = 10f; //Vector3.forward 表示"向前" tra ...

  6. Comparison of B-Tree and Hash Indexes

    Understanding the B-tree and hash data structures can help predict how different queries perform on ...

  7. loadrunner---<二>---菜鸟对cookie的思考

    http://www.cnblogs.com/Pierre-de-Ronsard/archive/2012/11/19/2772630.html loadrunner---<二>---菜鸟 ...

  8. C# 中的EventHandler

    //这里定义了一个水箱类 public class 水箱 {     //这是水箱的放水操作     public void 放水() { }     //这是水箱的属性     public dou ...

  9. wikioi 1688 求逆序对

    /*=========================================================== wikioi 1688 求逆序对 时间限制: 1 s 空间限制: 12800 ...

  10. noip2012普及组——质因数分解

    [问题描述]已知正整数 n 是两个不同的质数的乘积,试求出较大的那个质数. [输入]输入文件名为 prime.in.输入只有一行,包含一个正整数 n. [输出]输出文件名为 prime.out.输出只 ...