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. android 多线程下载图片

    很多时候我们需要在Android设备上下载远程服务器上的图片进行显示,今天Android123整理出两种比较好的方法来实现远程图片的下载.   方法一.直接通过Android提供的Http类访问远程服 ...

  2. google-http-java-client(android学习篇)

    package com.example.android; import java.io.IOException; import java.util.HashMap; import android.ap ...

  3. 附录三 嵌入式C程序的编译与调试

    课程回顾 C语言库的特性和发展 C语言库的常用库函数 标准库函数的特色应用 git@github.com:Kevin-Dfg/Data-Structures-and-Algorithm-Analysi ...

  4. 越狱Season 1-Episode 6: Riots, Drills and the Devil: Part 1

    Season 1, Episode 6: Riots, Drills and the Devil: Part 1 - Diamond: Just a few more rides. 就再多玩几次吧 O ...

  5. C#的157个建议--读书笔记(一)

    操作符的重载 ----实现类对象可以像值类型那样进行操作符的操作 实现比较器(IComparable)---实现对象的排序方式 协变:让返回值类型返回比声明的类型派生程度更大的类型:协变支持的两种方式 ...

  6. RecyclerView使用完全解析

    http://blog.csdn.net/a396901990/article/details/40153759 Android RecyclerView 使用完全解析 体验艺术般的控件 通过这个控件 ...

  7. C++ 实用的小程序

    1. 打开test_ids.txt 将里面的东西添加"1_",然后另存为test_ids_repaired.txt #include <iostream> #inclu ...

  8. maven&&gradle

    https://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html http://maven.apache. ...

  9. mysql+mybatis 插入可递增字段库表操作

    mysql本身类型介绍: BIGINT 8 字节 (-9 233 372 036 854 775 808,9 223 372 036 854 775 807) (0,18 446 744 073 70 ...

  10. 【转】Asp.net中时间格式化的6种方法详细总结

    1. 数据控件绑定时格式化日期方法: 代码如下: <asp:BoundColumn DataField="AddTime" HeaderText="添加时间&quo ...