SDUT 3297 DFS,sdut3297dfs

來源:互聯網
上載者:User

SDUT 3297 DFS,sdut3297dfs

奇妙的23點Time Limit: 1000ms   Memory limit: 65536K  有疑問?點這裡^_^題目描述

題意很簡單,給出5個數字,你可以使用 ‘ + ’ ,  ‘ - ’ , ‘ * ’運算子(運算子沒有優先順序關係),使得最後計算的結果等於23,其中給出的5個數字可以任意改變順序。

輸入 輸入5個數字,每個數位大小範圍為[1,50]。輸出 如果最後的運算結果可以等於23,輸出Yes,不能的話輸出No。

樣本輸入
1 1 1 1 11 2 3 4 52 3 5 7 11
樣本輸出
NoYesYes
提示 
提示

#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#include<map>using namespace std;int flag=0;int vis[55];int a[6];char c[]= {'+','-','*'};int DFS(int i ,int ans,int num){    if(flag||num>5)        return 0;    if(ans==23&&num==5)    {        flag=1;        return 1;    }    for(int j=0; j<5; j++)        for(int i=0; i<3; i++)        {            if(vis[j]==0)            {                vis[j]=1;                if(i==0)                    DFS(j,ans+a[j],num+1);                else if(i==1)                    DFS(j,ans-a[j],num+1);                else                    DFS(j,ans*a[j],num+1);                vis[j]=0;            }        }    return 0;}int main(){    while(~scanf("%d",&a[0]))    {        flag=0;        for(int i=1; i<5; i++)            scanf("%d",&a[i]);        for(int i=0; i<5; i++)        {            memset(vis,0,sizeof(vis));            vis[i]=1;            DFS(i,a[i],1);            if(flag)                break;        }        if(flag==0)            printf("No\n");        else            printf("Yes\n");    }} 


著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.