Division

紫书入门级别的暴力,可我还是写了好长时间 = =

【题目链接】uva 725

【题目类型】化简暴力

&题解:

首先要看懂题意,他的意思也就是0~9都只出现一遍,在这2个5位数中。

接着,你要知道:枚举一个5位数就够了,不用2个5位数都枚举,因为你可以通过n知道第2个5位数。

最后set维护出现的次数,ok判断是否可行,pri输出。

【时间复杂度】O(1e5)

&代码:

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll;
  4. const int INF = 0x3f3f3f3f;
  5. #define cle(a,val) memset(a,(val),sizeof(a))
  6. #define SI(N) scanf("%d",&(N))
  7. #define SII(N,M) scanf("%d %d",&(N),&(M))
  8. #define SIII(N,M,K) scanf("%d %d %d",&(N),&(M),&(K))
  9. #define rep(i,b) for(int i=0;i<(b);i++)
  10. #define rez(i,a,b) for(int i=(a);i<=(b);i++)
  11. #define red(i,a,b) for(int i=(a);i>=(b);i--)
  12. const ll LINF = 0x3f3f3f3f3f3f3f3f;
  13. #define PU(x) puts(#x);
  14. int n;
  15. set<int> sei, se2;
  16. vector<pair<int, int> > vep;
  17. bool ok(int d) {
  18. se2 = sei;
  19. int t = d / n;
  20. if (t * n != d) {
  21. return false;
  22. }
  23. int u = 0;
  24. while (t) {
  25. u++;
  26. sei.insert(t % 10);
  27. t /= 10;
  28. }
  29. if (u > 5) {
  30. return false;
  31. }
  32. if (sei.size() == 9 && !sei.count(0) && u == 4) {
  33. return true;
  34. }
  35. if (sei.size() == 10) {
  36. return true;
  37. }
  38. return false;
  39. }
  40. void pri() {
  41. if (vep.empty()) {
  42. printf("There are no solutions for %d.\n", n);
  43. return;
  44. }
  45. int t = vep.size();
  46. rep(i, t)
  47. printf("%05d / %05d = %d\n", vep[i].first, vep[i].second, n);
  48. }
  49. void Solve() {
  50. int uu = 0;
  51. while (~SI(n), n) {
  52. if (uu) PU()
  53. uu = 1;
  54. sei.clear() ;
  55. vep.clear();
  56. rez(i1, 0, 9) {
  57. sei.insert(i1);
  58. rez(i2, 0, 9) {
  59. if (sei.count(i2)) continue;
  60. sei.insert(i2);
  61. rez(i3, 0, 9) {
  62. if (sei.count(i3)) continue;
  63. sei.insert(i3);
  64. rez(i4, 0, 9) {
  65. if (sei.count(i4)) continue;
  66. sei.insert(i4);
  67. rez(i5, 0, 9) {
  68. if (sei.count(i5)) continue;
  69. sei.insert(i5);
  70. int d = i1 * 1e4 + i2 * 1e3 + i3 * 1e2 + i4 * 1e1 + i5;
  71. if (ok(d)) {
  72. pair<int, int> p;
  73. p.first = d;
  74. p.second = d / n;
  75. vep.push_back(p);
  76. }
  77. sei = se2;
  78. sei.erase(i5);
  79. }
  80. sei.erase(i4);
  81. }
  82. sei.erase(i3);
  83. }
  84. sei.erase(i2);
  85. }
  86. sei.erase(i1);
  87. }
  88. pri();
  89. }
  90. }
  91. int main() {
  92. Solve();
  93. return 0;
  94. }

上面是按位搜索的暴力,代码较长,下面是直接枚举的暴力,枚举枚举范围1234~98765,之后再判断,这样写代码就较短了。我枚举的是第一个5位数,当然,也可以枚举第二个,你自己可以试下。

&代码2:

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll;
  4. const int INF = 0x3f3f3f3f;
  5. #define cle(a,val) memset(a,(val),sizeof(a))
  6. #define SI(N) scanf("%d",&(N))
  7. #define SII(N,M) scanf("%d %d",&(N),&(M))
  8. #define SIII(N,M,K) scanf("%d %d %d",&(N),&(M),&(K))
  9. #define rep(i,b) for(ll i=0;i<(b);i++)
  10. #define rez(i,a,b) for(ll i=(a);i<=(b);i++)
  11. #define red(i,a,b) for(ll i=(a);i>=(b);i--)
  12. const ll LINF = 0x3f3f3f3f3f3f3f3f;
  13. #define PU(x) puts(#x);
  14. #define PI(A) cout<<(A)<<endl;
  15. #define DG(x) cout<<#x<<"="<<(x)<<endl;
  16. #define DGG(x,y) cout<<#x<<"="<<(x)<<" "<<#y<<"="<<(y)<<endl;
  17. #define DGGG(x,y,z) cout<<#x<<"="<<(x)<<" "<<#y<<"="<<(y)<<" "<<#z<<"="<<(z)<<endl;
  18. #define PIar(a,n) rep(i,n)cout<<a[i]<<" ";cout<<endl;
  19. #define PIarr(a,n,m) rep(aa,n){rep(bb, m)cout<<a[aa][bb]<<" ";cout<<endl;}
  20. const double EPS = 1e-9 ;
  21. /* //////////////////////// C o d i n g S p a c e //////////////////////// */
  22. const int MAXN = 1000 + 5 ;
  23. int n;
  24. bool used[10];
  25. bool ok(int x, int y) {
  26. if (y*n!=x) return false;
  27. cle(used, 0);
  28. //这是1e4 不是1e5
  29. if (x < 10000) used[0] = 1;
  30. if (y < 10000) used[0] = 1;
  31. int u1 = 0, u2 = 0;
  32. while (x) {
  33. used[x % 10] = 1;
  34. x /= 10;
  35. u1++;
  36. }
  37. while (y) {
  38. used[y % 10] = 1;
  39. y /= 10;
  40. u2++;
  41. }
  42. int c = 0;
  43. if (u1 < 6 && u2 < 6)
  44. rep(i, 10) if (used[i]) c++;
  45. return (c == 10);
  46. }
  47. void Solve() {
  48. int kg = 0;
  49. while (~SI(n), n) {
  50. if (kg)puts("");
  51. kg = -1;
  52. for (int i = 1234; i <= 98765; i++)
  53. if (ok(i, i / n)) {
  54. printf("%05d / %05d = %d\n", i, i / n, n);
  55. kg = 1;
  56. }
  57. if (kg == -1) printf("There are no solutions for %d.\n", n);
  58. }
  59. }
  60. int main() {
  61. Solve();
  62. return 0;
  63. }

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. Dwarves (有向图判环)

    Dwarves 时间限制: 1 Sec  内存限制: 64 MB提交: 14  解决: 4[提交][状态][讨论版] 题目描述 Once upon a time, there arose a huge ...

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

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

  3. poj1511 最短路

    题意:与poj3268一样,所有人需要从各点到一点再从一点到各点,求最短路总和. 与poj3268一样,先正向建图跑 dijkstra ,得到该点到其他所有各点的最短路,即这些人回去的最短路,再用反向 ...

  4. FS拓展设置

    一.集群测试说明: 1.该测试的主要目的是:让两个注册在不同FS Server上的账号彼此双方通话. 2.测试工具:eyeBeam .LinPhone 3.FS架构图: 上图中两台FS的分机状况如下: ...

  5. MySQL – optimizer_search_depth

    Working on customer case today I ran into interesting problem – query joining about 20 tables (thank ...

  6. Java C# 加密解密类库

    Bouncy Castle 是一种用于 Java 平台的开放源码的轻量级密码术包.它支持大量的密码术算法,并提供 JCE 1.2.1 的实现.因为 Bouncy Castle 被设计成轻量级的,所以从 ...

  7. vs2010 clickone 工程安装后的路径 win7

    C:\Users\xuan\AppData\Local\Apps\2.0\DX16T5JV.MLO\1H1ZAND1.1ZY\test..tion_f74974f651f2573b_0001.0000 ...

  8. Appium查找元素

    记录一些需要记忆的查找元素的内容: 1. driver.findElement(By.name("DELETE");   //We can use the DELETE text ...

  9. form表单元素类型

    <form> <input type="text"> <input type="password"> <input t ...

  10. hibernate 双向一对多关系(Annotation mappedBy注解理解)

    1.@mappedBy 属性简单理解为设定为主表(OneToMany方)(这只是我个人理解,上面文章中也有提到过) 所以另一端(ManyToOne)则需要设置外键@JoinColumn(name=&q ...