zoukankan      html  css  js  c++  java
  • HDU-1015-Safecracker

    题目链接

    http://acm.hdu.edu.cn/showproblem.php?pid=1015

    题意

    给一个数n 和 一串字符 str  ,           ( A B  C D E F G .......  X Y Z   代表  1 2 3 ..... 26)

    从 字符串中选 5个字符  如果  v - w^2 + x^3 - y^4 + z^5 = n  输出 字符,有多个 输出 字典迅速最大的那组

    对于这题,可直接5 个for循环 暴力出来,不过当要选的字符比5多时就很麻烦,你要写更多的for循环,并且 当 字符数为n时 ,你就不知道用多少个for 了

    那么直接递归,也可以说是dfs ,  我做题我的初衷是用来练习dfs的

    5个for的代码:

    #include<stdio.h>
    #include<string.h>
    #include<iostream>
    #include<algorithm>
    using namespace std;

    bool cmp(const int &a,const int &b)
    {
    return a>b;
    }

    int main(void)
    {
    int k,n;
    int i1,i2,i3,i4,i5;
    int v,w,x,y,z,ok;
    char str[20];
    while(scanf("%d%s",&n,str)!=EOF)
    {
    ok=0;
    if(n==0&&strcmp(str,"END")==0)
    break;
    k=strlen(str);
    sort(str,str+k,cmp);
    for(i1=0;i1<k;i1++)
    {
    for(i2=0;i2<k;i2++)
    {
    if(i1==i2)
    continue;
    for(i3=0;i3<k;i3++)
    {
    if(i3==i1||i3==i2)
    continue;
    for(i4=0;i4<k;i4++)
    {
    if(i4==i1||i4==i2||i4==i3)
    continue;
    for(i5=0;i5<k;i5++)
    {
    if(i5==i1||i5==i2||i5==i3||i5==i4)
    continue;
    v=str[i1]-64; w=str[i2]-64; x=str[i3]-64; y=str[i4]-64; z=str[i5]-64;
    if(v-w*w+x*x*x-y*y*y*y+z*z*z*z*z==n)
    {
    printf("%c%c%c%c%c ",str[i1],str[i2],str[i3],str[i4],str[i5]);
    ok=1;
    break;
    }
    }
    if(ok)
    break;
    }
    if(ok)
    break;
    }
    if(ok)
    break;
    }
    if(ok)
    break;
    }
    if(ok==0)
    printf("no solution ");
    }
    return 0;
    }

    dfs 的代码

    #include<stdio.h>
    #include<iostream>
    #include<algorithm>
    #include<string.h>
    using namespace std;

    char a[6];
    char str[20];
    int hash[20];
    int n,k;
    int success;

    bool cmp(const int &a,const int &b)
    {
    return a>b;
    }

    void dfs(int ans)
    {
    int i,index;
    if(success)
    return ;
    if(ans==5)
    {
    int v,w,x,y,z;
    v=a[0]-64; w=a[1]-64; x=a[2]-64; y=a[3]-64; z=a[4]-64;
    if(v-w*w+x*x*x-y*y*y*y+z*z*z*z*z==n)
    {
    a[5]='';
    printf("%s ",a);
    //printf("%c%c%c%c%c ",a[0],a[1],a[2],a[3],a[4]);
    success=1;
    }
    return ;
    }
    for(i=0;i<k;i++)
    {
    if(hash[i]==0)
    {
    hash[i]=1;
    a[ans]=str[i];
    dfs(ans+1);
    hash[i]=0;
    }
    if(success)
    return ;
    }
    }

    int main(void)
    {
    int i,j;
    while(scanf("%d%s",&n,str)!=EOF)
    {
    memset(hash,0,sizeof(hash));
    success=0;
    if(n==0&&strcmp(str,"END")==0)
    return 0;
    k=strlen(str);
    sort(str,str+k,cmp);
    dfs(0);
    if(!success)
    printf("no solution ");
    }
    return 0;
    }

  • 相关阅读:
    eclipse中的Invalid text string (xxx).
    在jsp文件中出现Unknown tag (c:out)
    eclipse 界面复原
    ecilpse 纠错插件
    Multiple annotations found at this line:- The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path
    Port 8080 required by Tomcat v9.0 Server at localhost is already in use. The server may already be running in another process, or a system process may be using the port.
    调用第三方https接口
    调用第三方http接口
    创建带值枚举
    spring整合redis之Redis配置文件
  • 原文地址:https://www.cnblogs.com/liudehao/p/4125141.html
Copyright © 2011-2022 走看看