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;
    }

  • 相关阅读:
    span里设置高度
    Tensorflow2.0语法
    Python-OpenCV中的filter2D()函数
    解决安装tensorflow等速度慢,超时
    keras绘图之pydot和graphviz的导入( pydot` failed to call GraphViz)
    天天向上
    time库
    PythonDraw及turtle库
    TempComvert及-eval的用法
    百分号字符串拼接
  • 原文地址:https://www.cnblogs.com/liudehao/p/4125141.html
Copyright © 2011-2022 走看看