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. Codeforces Round #135 (Div. 2)

    A. k-String 统计每个字母出现次数即可. B. Special Offer! Super Price 999 Bourles! 枚举末尾有几个9,注意不要爆掉\(long\ long\)的范 ...

  2. Windows && Linux 双系统

    使用软件 EasyBCD 添加新条目--->NeoGrub--->安装--->配置 在弹出的文本中输入如下东西: title Install Backbox root (hd0,0) ...

  3. Andoid 利用ndk-stack定位崩溃代码

    Android NDK自从版本R6开始, 提供了一个工具ndk-stack( 在目录{ndk_root}/中 ). 这个工具能自动分析dump下来的crash log, 将崩溃时的调用内存地址和c++ ...

  4. 继承:《原型和原型链(prototype 属性使您有能力向对象添加属性和方法。)》

    二. 原型对象   在JavaScript 中,每当定义一个对象(函数)时候,对象中都会包含一些预定义的属性.其中函数对象的一个属性就是原型对象 prototype.注:普通对象没有prototype ...

  5. 【转】 iOS 开发之静态库.a和动态库详解 -- 不错

    原文网址:http://blog.csdn.net/lxl_815520/article/details/52154331 一, 简单介绍 1.什么是库 库是程序代码的集合,是共享程序代码的一种方式 ...

  6. JVMInternals

    http://blog.jamesdbloom.com/JVMInternals.html http://blog.jamesdbloom.com/JavaCodeToByteCode_PartOne ...

  7. linux下单节点oracle数据库间ogg搭建

    环境说明:   linux为Linux 2.6.32-573.el6.x86_64     oracle为 11g Enterprise Edition Release 11.2.0.1.0 - 64 ...

  8. 简单实用的Android ORM框架TigerDB

    TigerDB是一个简单的Android ORM框架,它能让你一句话实现数据库的增删改查,同时支持实体对象的持久化和自动映射,同时你也不必关心表结构的变化,因为它会自动检测新增字段来更新你的表结构. ...

  9. MySQL中char(36)被认为是GUID导致的BUG及解决方案

    MySQL中char(36)被认为是GUID导致的BUG及解决方案 有时候在使用Toad或在程序中,偶尔会遇到如下的错误: System.FormatException GUID 应包含带 4 个短划 ...

  10. unity 合并skinnedMeshRenderer中遇到的一个大坑

    将多个skinnedMeshRenderer合并成一个skinnedMeshRenderer,主要涉及的mesh合并.骨骼列表合并.重定向顶点骨骼索引.其中重定向顶点骨骼索引只是通过加偏值即可完成,所 ...