暴力求解-枚举法

/*****0-9全排列

输入正整数n,按从小到大的顺序输出所有形如abcde/fghij=n的表达式,其中a~j恰好为数字0~9的一个排列,n为2到79

样例输入:62

样例输出:79546/01283=62

       94736/01528=62

刚开始想用堆栈来实现0到9全排列,还有五个for循环也可以,
但是想到STL中有一个next_permutation的函数,可以实现全排列,于是将其采用过来*/

写法一:暴力剪枝

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

int main()
{
    int n;
    scanf("%d",&n);
    int s1,s2,flag[11];
    for(int i=1234;i<=5000;i++)//反过来想就是i*n=前面数,用数组标记十个位置,必须不能有相等的数
    {
        memset(flag,0,sizeof(flag));
        s1=i;
        s2=i*n;
        while(s1!=s2)
        {
            if(!flag[s1%10])
            {
                flag[s1%10]=1;
                s1/=10;
            }
            else
                break;
            if(!flag[s2%10])
            {
                flag[s2%10]=1;
                s2/=10;
            }
            else
                break;
        }
        int j;
        for(j=0;j<10;j++)
            if(!flag[j])
            break;
        if(j==10&&i*n<=98765)
        {
            if(i<10000)
                printf("%d/0%d=%d\n",i*n,i,n);
            else
                printf("%d/%d=%d\n",i*n,i,n);
        }
    }
    return 0;
}

写法2:STL

#include <stdio.h>
#include<stdlib.h>
#include <algorithm>
#include <string.h>


using namespace std;

int main()
{
    int length,a,b,n;
    char str[20]="1234567890";
    scanf("%d",&n);

    length = strlen(str);


    while (next_permutation(str, str + length))
    {
//        printf("%s\n",str);
          a=(str[0]-'0')*10000+(str[1]-'0')*1000+(str[2]-'0')*100+(str[3]-'0')*10+(str[4]-'0');
          b=(str[5]-'0')*10000+(str[6]-'0')*1000+(str[7]-'0')*100+(str[8]-'0')*10+(str[9]-'0');

          if(b*n==a)
          {
          printf("%c%c%c%c%c/",str[0],str[1],str[2],str[3],str[4]);
          printf("%c%c%c%c%c=%d\n",str[5],str[6],str[7],str[8],str[9],n);
          }

    }

    system("pause");
    return 0;
}

/*
最大乘积
输入n个元素组成的序列s,你需要找出一个乘积罪的的连续子序列。如果这个罪的的乘积不是正数,输出-1.1<=n<=18,-10<=Si<=10;
样例输入:
3
2 4 -3
5
2 5 -1 2 -1
样例输出:
8
20

分析:分析:连续子序列有两个要素:起点和终点,因此只需枚举起点和终点即可。
由于每个元素的绝对值不超过10,一共又不超过18个元素,最大可能的乘积不会超过10^18,可以用long long
*/

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
    long long n;
    long long  a[110],s[110];
    long long maxn;
    scanf("%lld",&n);
    memset(s,0,sizeof(s));
    for(int i=1; i<=n; i++)
    {
        scanf("%lld",&a[i]);
    }
    s[0]=1;
    maxn=-0x3f3f;
    for(int i=1; i<=n; i++)
    {
        s[i]=a[i]*s[i-1];
    }
    for(int i=1; i<=n; i++)
        for(int j=1; j<=n; j++)
        {
            if(s[i]/s[j-i]>maxn)
                maxn=s[i]/s[j-i];
        }
    if(maxn<0)
        cout<<-1<<endl;
    else
        printf("%lld\n",maxn);
    return 0;
}

/**分数拆分
输入正整数k,找到所有的正整数x>=y,使得1/k=1/x + 1/y;
样例输入:
2
12
样例输出:
2
1/2 = 1/6 + 1/3
1/2 = 1/4 + 1/4
*/

#include<stdio.h>
#include<string.h>
struct integer
{
    int X,Y;
} a[10000];
int main()
{
    int i,j,y,k,count;
    while(~scanf("%d",&k))
    {
        memset(a,0,sizeof(a));
        i=count=0;
        for(y=k+1; y<=k*2; y++)//知x>=y,有1/x<=1/y,因此1/k-1/y<=1/y,即y<=2*k,即在k到k之间枚举j
        {
            if(y*k%(y-k)==0)
            {
                count++;
                a[i].X=y*k/(y-k);
                a[i++].Y=y;
            }
        }
        printf("%d\n",count);
        for(j=0; j<i; j++)
            printf("1/%d = 1/%d + 1/%d\n",k,a[j].X,a[j].Y);
    }
    return 0;
}

学习于:http://www.2cto.com/kf/201307/228233.html
http://blog.csdn.net/qwerty_xk/article/details/7870075

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值