暴力枚举
程序员文章站
2022-06-02 22:48:11
...
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int main()
{
int n, x, y, i, j;
int flag, num[11], k=0;
while(~scanf("%d", &n) && n)
// while(scanf("%d", &n) != EOF && n)
{
if (k) printf("\n"); k++;
flag = 1;
for(i=1234; i<50000; i++)
{
memset(num,0,sizeof(num));
x = i*n;
y = i;
while(x||y)
{
if(!num[y % 10])
{
num[y % 10] = 1;
y /= 10;
}
else break;
if(!num[x % 10])
{
num[x % 10] = 1;
x /= 10;
}
else break;
}
for(j=0; j<10; j++)
if(!num[j])
break;
if(j == 10 && i*n <= 98765)
{
printf("%05d / %05d = %d\n", i*n, i, n);
flag = 0;
}
}
if(flag)
printf("There are no solutions for %d.\n", n);
}
return 0;
}
思路:利用数组下标来存取余数,若abcde与efghi有相同的数字则 数组的和不为10
#include <iostream>
#include <cstdio>
using namespace std;
struct number
{
int xx;
int yy;
} num[10010];
int main()
{
unsigned int k;
int x, y, m, n;
while(scanf("%d",&k) != EOF)
{
m = 0;
n = 0;
for(int y=k+1; y<=2*k; y++)
{
x = (int)(k*y/(y-k));
if(x>=y)
{
if((x*y == (k*y + k*x)))
{
num[n++].xx = x;
num[m++].yy = y;
}
}
}
cout << n <<endl;
for(int i=0; i<n; i++)
cout << "1/" << k <<" = " << "1/" << num[i].xx << " + "<< "1/" << num[i].yy << endl;
}
return 0;
}
思路:根据X >= Y ,得到 k < Y <= 2*k 以此来确定Y的枚举范围
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int main()
{
int n, s[20], m = 1;
long long maxnum;
while(scanf("%d", &n) != EOF)
{
maxnum = 0;
memset(s,0,sizeof(s));
for(int i=0; i<n; i++) //存储数组
cin >> s[i];
for ( int i = 0; i < n; ++i ) //枚举子数组的起点和终点
{
for ( int j = i; j < n ; ++j )
{
long long sum = 1;
for ( int k = i; k <= j; ++k )
{
sum *= s[k];
}
if(sum > maxnum)
maxnum = sum;
}
}
printf("Case #%d: The maximum product is %lld.\n\n",m++,maxnum);
}
return 0;
}
思路:枚举子数列的起点和终点
上一篇: 转 Java给word中的table赋值
下一篇: 柠檬姜茶功效和做法