简单的枚举
来源:互联网 发布:php 拼接变量名 编辑:程序博客网 时间:2024/06/11 23:27
补一下从前的做题记录。。。。某天一口气水了三道简单的枚举
这三道题的核心思想就是枚举,通过暴力的枚举所有情况来结局题目。
总体来说比较不费脑,只需要枚举所有情况就可以了,但是在枚举的
方法上,要注意方法的优化,不然可能会超时的。
第一题:
UVA725-7.1-Division
题目链接:https://vjudge.net/problem/UVA-725
这个题的意思是说输入正整数n,按从小到大的顺序输出所有形如abcde/fghij=n的表达式。其中a~j正好为数字0~9的一个排列(可以有前导零)
如:
62
79546/01283=62
94736/01528=62
题目分析:
这个题我写的比较麻烦,就是只枚举分母,结果乘以分母即为分子。
然后用一个数组标记哪一个数用到过了,然后直接暴力枚举所有数
判断是否成立。
给出代码:
#include<iostream>#include<vector>#include<queue>#include<set>#include<cstdio>#include<string.h>using namespace std;int mark[10];int judge(int x){ int h=1; int book=0; while(x%h!=x) { h=h*10; book++; } return book;}void com(int x){ int h=10; while(x) { int t=x%10; mark[t]=0; x=x/10; } return;}int main(){ //freopen("D:\\output.txt", "w", stdout); int n; memset(mark,1,sizeof(mark)); int a1=0; while(cin>>n&&n) { a1++; if(a1!=1) cout<<endl; int book1=1; for(int i=1234;;i++) { //cout<<i<<endl; int a=n*i; if(a>=100000) break; int h1=judge(a); int h2=judge(i); int h=h1+h2; if(h>10) break; if(h==9) mark[0]=0; com(a); com(i); int book=1; int j; for(j=0;j<10;j++) if(mark[j]) { book=0; break; } if(book) { book1=0; if(h1==4) cout<<0<<a<<" / "<<i<<" = "<<n<<endl; else if(h2==4) cout<<a<<" / "<<0<<i<<" = "<<n<<endl; else cout<<a<<" / "<<i<<" = "<<n<<endl; } memset(mark,1,sizeof(mark)); } if(book1) printf("There are no solutions for %d.\n",n); }}
第二题:
UVA11059-7.2-Maximum Product
题目链接:
https://vjudge.net/problem/UVA-11059
输入n个元素组成的序列S.找出一个乘积最大的连续子序列。
题目分析:
因为这个题的n很小,<=18,所以直接枚举所有的子序列就可以做出。
给出代码:
#include<iostream>#include<vector>#include<queue>#include<set>#include<cstdio>#include<string.h>using namespace std;typedef long long int LL;int main(){ //freopen("D:\\output.txt", "w", stdout); int book=0; int n; LL num[20]; while(cin>>n) { book++; int i; LL MAX=-1;; for(i=1;i<=n;i++) cin>>num[i]; for(i=n;i>0;i--) { int y=1; int turn=n-i+1; while(turn--) { LL x=1; for(int j=y;j<y+i;j++) x*=num[j]; //cout<<"x="<<x<<endl; if(MAX<x) MAX=x; y++; } } if(MAX<0) MAX=0; printf("Case #%d: The maximum product is %lld.\n\n",book,MAX); } return 0;}
第三题:
UVA10976-7.3-Fraction Again
题目链接:
https://vjudge.net/problem/UVA-10976
输入正整数k,找出所有的正整数x>=y,使得1/k=1/x+1/y.
题目分析:
这个题同样就可以通过枚举所有情况来结局。
不过通过对样例数据的分析我们可以发现,如
果输入一个k,x和y的值最大是无法超过k的两倍的,
这样就找到了边界。
给出代码:
#include<iostream>#include<vector>#include<queue>#include<set>#include<cstdio>#include<string.h>using namespace std;int main(){ //freopen("D:\\output.txt", "w", stdout); int n; while(cin>>n) { int num1[50000]; int num2[50000]; int book=0; int i; for(i=n+1;i<=n*2;i++) { long long int a=i-n; long long int b=n*i; if(b%a==0) { book++; num1[book]=b/a; num2[book]=i; } } printf("%d\n",book); for(i=1;i<=book;i++) { printf("1/%d = 1/%d + 1/%d\n",n,num1[i],num2[i]); } } return 0;}
- 简单的枚举实例
- 枚举的简单应用
- 枚举的简单使用
- 枚举的简单使用
- 枚举的简单使用
- 简单的枚举
- 简单的使用枚举
- 模拟枚举和枚举的简单应用
- 枚举的简单应用(构思)
- 枚举类的简单介绍
- 枚举类型的简单使用
- C#枚举的简单使用
- 枚举法的简单心得
- 一个枚举的简单例子
- 枚举(enum)的简单用法
- enum 枚举的简单应用
- Java简单枚举的实现
- 枚举类的简单操作
- 推荐几个矢量图库网站
- 虚拟机字节码执行引擎
- Linux文件权限及用户管理
- C++11 智能指针
- 在xcode8中使用真机调试
- 简单的枚举
- Java面试题全集(中)
- android 打开闹铃的设置
- Maven仓库基础知识学习
- Deep Speech笔记
- TCP/IP:UDP(2) IP分片
- IDEA 常用插件
- 如何使用busybox编译和生成最简linux根文件系统(rootfs)
- Service Intent must be explicit的两种解决方法