• zoj 1239 Hanoi Tower Troubles Again!


    Hanoi Tower Troubles Again!

    Time Limit: 2 Seconds Memory Limit: 65536 KB

    People stopped moving discs from peg to peg after they know the number of steps needed to complete the entire task. But on the other hand, they didn't not stopped thinking about similar puzzles with the Hanoi Tower. Mr.S invented a little game on it. The game consists of N pegs and a LOT of balls. The balls are numbered 1,2,3... The balls look ordinary, but they are actually magic. If the sum of the numbers on two balls is NOT a square number, they will push each other with a great force when they're too closed, so they can NEVER be put together touching each other.

    The player should place one ball on the top of a peg at a time. He should first try ball 1, then ball 2, then ball 3... If he fails to do so, the game ends. Help the player to place as many balls as possible. You may take a look at the picture above, since it shows us a best result for 4 pegs.


    Input

    The first line of the input contains a single integer T, indicating the number of test cases. (1<=T<=50) Each test case contains a single integer N(1<=N<=50), indicating the number of pegs available.


    Output

    For each test case in the input print a line containing an integer indicating the maximal number of balls that can be placed. Print -1 if an infinite number of balls can be placed.


    Sample Input

    2
    4
    25


    Sample Output

    11
    337

    不是很难的题,看懂题意就好,只需要记录每根柱子的第一个球是多少就好。

    题意:给定柱子输N,按编号从小到大放球,要求,如果该球不在最底数,则该球和它下面一个球的编号之和必须是完全平方数。

    给定柱子数N,最多能放多少球上去。如果无限个,就输出-1(我没看出什么时候输出-1)

    附上代码:

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cmath>
     5 using namespace std;
     6 
     7 int a[55],n,m;
     8 
     9 bool xx(int x)  //x是当前要放的球的编号
    10 {
    11     int s,w;
    12     for(int i=1; i<=n; i++)   //一共有这么多的柱子
    13     {
    14         s=a[i]+x;
    15         w=(int)sqrt(s)*(int)sqrt(s);
    16         if(!a[i]||s==w)  //当柱子为空或者此时和为完全平方数时,放入这个球
    17         {
    18             a[i]=x;
    19             m++;
    20             xx(x+1);
    21             return true;
    22         }
    23     }
    24     return false;
    25 }
    26 
    27 int main()
    28 {
    29     int T,i,j;
    30     scanf("%d",&T);
    31     while(T--)
    32     {
    33         scanf("%d",&n);
    34         memset(a,0,sizeof(a)); //a数组存储最上面的球的编号
    35         m=0;
    36         if(xx(1)) //从编号1开始循环
    37             printf("%d
    ",m);
    38         else
    39             printf("-1
    ");
    40     }
    41     return 0;
    42 }
  • 相关阅读:
    VirtualBox 下USB 设备加载的步骤及无法加载的解决办法
    浅析:setsockopt()改善socket网络程序的健壮性
    减小Gcc编译程序的体积
    linux下查看系统进程占用的句柄数
    Linux下高并发socket最大连接数所受的各种限制
    spring-jpa通过自定义sql执行修改碰到的问题
    阿里巴巴Java开发手册中的DO、DTO、BO、AO、VO、POJO定义
    sql select时增加常量列
    CASE WHEN 及 SELECT CASE WHEN的用法
    MYSQL常见运算符和函数
  • 原文地址:https://www.cnblogs.com/pshw/p/5142162.html
Copyright © 2020-2023  润新知