zoukankan      html  css  js  c++  java
  • 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

     1 #include<iostream>
     2 #include<string.h>
     3 #include<math.h>
     4 using namespace std;
     5 int main()
     6 {
     7     int a[50],b[50];
     8     int times,peg,temp;
     9     int n=1;
    10     int num=1;
    11     int i,j;
    12     cin>>times;
    13     while(times--)
    14     {    
    15         cin>>peg;
    16         memset(a,0,sizeof(a));
    17         while(1)
    18         {
    19             if(i==peg)
    20             {
    21                 cout<<num-1<<endl;
    22                 num=1,i=0;
    23                 break;
    24             }
    25             if(a[i]==0)
    26             {
    27                 a[i]=num++;
    28                 i=0;
    29                 continue;
    30             }
    31             else 
    32             {
    33                 j=sqrt(a[i]+num);
    34                 if(j*j==(a[i]+num))
    35                 {
    36                     a[i]=num++;
    37                     i=0;
    38                     continue;
    39                 }
    40                 else 
    41                     i++; 
    42             }
    43         }
    44     }
    45     return 0;
    46 }
  • 相关阅读:
    模块和包——Python
    异常——Python
    单例——Python
    类属性和类方法——Python
    继承和多态——Python
    私有属性和私有方法——Python
    面向对象封装案例——Python
    面相对象基础语法——Python
    类、接口作为成员变量类型——Java
    内部类的概念和分类——Java
  • 原文地址:https://www.cnblogs.com/xdbingo/p/4798543.html
Copyright © 2011-2022 走看看