暴力求解法——简单枚举
Posted 小乖乖的臭坏坏
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了暴力求解法——简单枚举相关的知识,希望对你有一定的参考价值。
例题1:
代码:
public class uva725
public static void main(String[] args)
int n = 62;
for (int a=12345;a<=98765;a++)
if(a%n!=0)
continue;
else
int b = a / n;
if(checkNum(a, b))
System.out.println(a + " / " + b + " = " + 62);
else
continue;
public static boolean checkNum(int a, int b)
int A[] = new int[10];
for (int i=0;i<5;i++)
A[a%10]++;
if(A[a%10]>1)
return false;
a = a/10;
for (int i=0;i<5;i++)
A[b%10]++;
if(A[b%10]>1)
return false;
b = b/10;
for (int i=0;i<10;i++)
if(A[i]!=1)
return false;
return true;
例题2:
public class uva11059
public static void main(String[] args)
// int n = 3;
// int A[] = 2,4,-1;
int n = 5;
int A[] = 2,5,-1,2,-1;
// int n = 2;
// int A[] = -1, 1;
//
int maxn = -1;
for (int p1 = 0;p1<=n-1;p1++)
for (int p2 = 1;p2<=n-p1;p2++)
int cnt = 1;
int j = n-p2;
if (p1==j)
cnt = A[p1];
else
for (int i=p1;i<=j;i++)
cnt *= A[i];
if(cnt>maxn)
maxn = cnt;
System.out.println(maxn>=0?maxn:0);
例题3:
代码:
public class uva10976
public static void main(String[] args)
int k = 2;
for (int y = k+1;y<=2*k;y++)
int x1 = k*y/(y-k);
float x2 = (float) (k*y/(y-k));
if(Math.abs(x1-x2)<1e-3)
System.out.println("1/"+k+" = 1/"+x1+" + 1/"+y);
以上是关于暴力求解法——简单枚举的主要内容,如果未能解决你的问题,请参考以下文章