uva 725 DIVISION (暴力枚举)
2015-12-04 17:39
417 查看
我的56MS
但要特别提一下这位仁兄写的,有狠多值得学习的地方
http://blog.csdn.net/mobius_strip/article/details/38735773
#include <cstdio> #include <iostream> #include <string> #include <cstring> #include <queue> #include <vector> #include <map> #include <cmath> using namespace std; #define MEM(a,v) memset (a,v,sizeof(a)) // a for address, v for value #define max(x,y) ((x)>(y)?(x):(y)) #define max(x,y) ((x)>(y)?(x):(y)) #define debug printf("!\n") int num[15]; bool visited[15]; bool check(int a,int b) { MEM(visited,false); int j; for(j = 5;j>=1;j--) { int tmp = a%10; num[j] = tmp; a/=10; } for(j = 10;j>=6;j--) { int tmp = b%10; num[j] = tmp; b/=10; } for(j = 1;j<=10;j++) { if(visited[num[j]]) return false; visited[num[j]] = true; } return true; } int main() { int i,n,j,k,T=0; while(~scanf("%d",&n) && n) { if(T++) printf("\n"); bool find = false; for(i = 1234;i<=98765;i++) { if(i%n==0) { int m = i/n; if(check(i,m)) { find =true; for(j = 1;j<=5;j++) printf("%d",num[j]); printf(" / "); for(j = 6;j<=10;j++) printf("%d",num[j]); printf(" = %d\n",n); } } } if(!find) printf("There are no solutions for %d.\n",n); } return 0; }
但要特别提一下这位仁兄写的,有狠多值得学习的地方
http://blog.csdn.net/mobius_strip/article/details/38735773
#include <iostream> #include <cstdlib> #include <cstdio> using namespace std; int used[10]; int judge( int a, int b ) { //对于判断是否有重复的数字,可以用一个used数组 //将用过的项置为1 if ( b > 98765 ) return 0; for ( int i = 0 ; i < 10 ; ++ i ) used[i] = 0; if ( a < 10000 ) used[0] = 1; while ( a ) { used[a%10] = 1; a /= 10; } while ( b ) { used[b%10] = 1; b /= 10; } int sum = 0; for ( int i = 0 ; i < 10 ; ++ i ) sum += used[i]; return (sum == 10); } int main() { int n, T = 0; while ( ~scanf("%d",&n) && n ) { if ( T ++ ) printf("\n"); int count = 0; for ( int i = 1234 ; i < 100000 ; ++ i ) { if ( judge( i, i*n ) ) { printf("%05d / %05d = %d\n",i*n,i,n); //可以用%05d填充,就不用数组来保存数字了 count ++; } } if ( !count ) printf("There are no solutions for %d.\n",n); } return 0; }
相关文章推荐
- HTML CSS之FORM
- 机器学习
- 实习整理(十七)
- 20151204 流水账
- JavaMail发送多联系人多附件邮件
- 使用JDBC在MySQL数据库中快速批量插入数据
- hadoop Job 运行错误 java.lang.OutOfMemoryError: Java heap space
- Android 点击事件 4种 写法
- java -访问者模式实现用户登录模块
- Android中5种Button点击事件的实现方法
- jqGrid 属性、事件全集
- php7 安装 2
- Codeforces Round #334 E. Lieges of Legendre(SG)
- 模糊Kmeans
- 关于分在 Linux windows 平台下对github上的文件进行克隆 上传
- 转 soap和wsdl 的理解
- 平衡二叉树(AVL)
- C语言指针笔记 by STP
- Java内存分配
- 最长公共子序列问题