HDU 5339-Untitled(dfs)

16 篇文章 0 订阅

题目地址:HDU 5339
题意:给出n个数和一个数a,问a对其中的一些数取余(可以为n个数),是否a能等于0
思路:遍历枚举,每次找<=a的数,然后取余,一直到最后看是否为0
代码被人叉了,数据水才是真的水,又做了一发搜索。

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <set>
#include <queue>
#include <stack>
#include <map>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
typedef long long LL;
const int inf=0x3f3f3f3f;
const double pi= acos(-1.0);
const double esp=1e-6;
int s[30];
int cmp(int a,int b)
{
    return a>b;
}
int main()
{
    int T,n,a;
    int ans;
    int cnt;
    scanf("%d",&T);
    while(T--){
        scanf("%d %d",&n,&a);
        ans=inf;
        cnt=0;
        memset(s,0,sizeof(s));
        for(int i=0;i<n;i++)
            scanf("%d",&s[i]);
        sort(s,s+n,cmp);
        for(int i=0;i<n;i++){
                if(a>=s[i]){
                    a%=s[i];
                    cnt++;
                    if(a==0){
                        ans=min(ans,cnt);
                        break;
                    }
                }
        }
        //printf("cnt=%d\n",cnt);
        if(ans==inf)
            puts("-1");
        else
            printf("%d\n",ans);
    }
    return 0;
}
/*
2
2 9
2 7
2 9
6 7

2
-1
*/

正确无×

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <set>
#include <queue>
#include <stack>
#include <map>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
typedef long long LL;
const int inf=0x3f3f3f3f;
const double pi= acos(-1.0);
const double esp=1e-6;
int s[30];
int minn;
void dfs(int a,int t,int k)
{
    if(!a)
    {
        minn=min(minn,k);
        return ;
    }
    for(int i=t-1;i>=1;i--)
        if(a>=s[i])
            dfs(a%s[i],i,k+1);
}
int main()
{
    int T,n,a,i;
    scanf("%d",&T);
    while(T--){
        minn=inf;
        scanf("%d %d",&n,&a);
        for(i=1;i<=n;i++){
            scanf("%d",&s[i]);
        }
        sort(s+1,s+n+1);
        dfs(a,n+1,0);
        if(minn==inf)
            puts("-1");
        else
            printf("%d\n",minn);
    }
    return 0;
}
/*
1
3 10
9 8 2
*/
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Rocky0429

一块也是爱

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值