zoukankan      html  css  js  c++  java
  • Square

    Square

    TimeLimit: 1 Second   MemoryLimit: 32 Megabyte

    Totalsubmit: 1638   Accepted: 440  

    Description

    Given a set of sticks of various lengths, is it possible to join them end-to-end to form a square?

    Input

    The first line of input contains N, the number of test cases. Each test case begins with an integer 4 <= M <= 20, the number of sticks. M integers follow; each gives the length of a stick - an integer between 1 and 10,000.

    Output

    For each case, output a line containing "yes" if is is possible to form a square; otherwise output "no".

    Sample Input

    3
    4 1 1 1 1
    5 10 20 30 40 50
    8 1 7 2 6 4 4 3 5

    Sample Output

    yes
    no
    yes

     

    思路:从第一个数开始搜索,将其和与边长比对,相等则计数+1,计数达到3的时候说明可以组成,因为剩下那条必与边长相等,搜索过程注意剪枝,若某个数已被加入边长则不能重复计算,应将其标记,另外应在每一层递归时进行判断,看是否满足结束条件,以此来优化时间

     

     

    #include<stdio.h>

    #include<string.h>

    int a[25],vis[25];

    int con,temp,side,sum,flag,k;

      //con用来记录边数,temp存放暂时的边长,用来与目标边长比对,index是每次查找的起始点(从上次结束的位置),非常重要,用此优化时间

    void dfs(int con,int temp,int index)

    {

    int i;
    if(3==con)
    {
    flag =1;
    return;
    }
    if(temp==side)
    {
    dfs(con+1,0,0);
    if(flag)
    return;
    }
    for(i = index ;i < k;i++)
    {
    if(!vis[i])    //判断此数是否已被用过
    {
    vis[i]=1;
    dfs(con,temp+a[i],i+1);
    if(flag)
    return;
    vis[i]=0;
    }
    }}int main(){
    int n,m,max;
    scanf("%d",&n);
    while(n--)
    {
    k = sum =0;
    flag = max =0;
    memset(vis,0,sizeof(vis));
    scanf("%d",&m);
    while(m--)
    {
    scanf("%d",&a[k++]);
    sum += a[k-1];
    if(max<a[k-1])
    max = a[k-1];
    }
    if(sum%4||max>sum/4)
    {
    printf("no ");
    continue;
    }
    side = sum/4;
    dfs(0,0,0);
    if(flag)
    {
    printf("yes ");
    continue;
    }
    printf("no ");
    }
    return0;}


  • 相关阅读:
    awk去重以某列重复的行
    awk 统计文件中按照某列统计某列的和(sum)
    使用jdk压缩war包
    histoty显示时间戳
    awk统计文件中某关键词出现次数
    Jbox帮助文档,默认的属性含义
    net之session漫谈及分布式session解决方案
    StackExchange.Redis 基本使用 (一) (转)
    Sql Server 表创建以及Ef浅谈
    数据验证(自定义特性)
  • 原文地址:https://www.cnblogs.com/anhuizhiye/p/3316123.html
Copyright © 2011-2022 走看看