简单的枚举

补一下从前的做题记录。。。。某天一口气水了三道简单的枚举


这三道题的核心思想就是枚举,通过暴力的枚举所有情况来结局题目。
总体来说比较不费脑,只需要枚举所有情况就可以了,但是在枚举的
方法上,要注意方法的优化,不然可能会超时的。
第一题:
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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值