zoukankan      html  css  js  c++  java
  • HDU 1329 Hanoi Tower Troubles Again!(乱搞)

    Hanoi Tower Troubles Again!

    Problem Description
    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
     
    Answer
    可以找规律解=_=,输入1时答案是1,后面的答案间隔是:2,4,4,6,6,8,8...(答案是3,7,11,17,23,31,39...)
    所以答案就出来了。当然也可以模拟。
     
    #include <cstdio>
    #include <iostream>
    #include <string>
    #include <cstring>
    #include <algorithm>
    #include <cmath>
    #include <vector>
    #define PI acos(-1.0)
    #define ms(a) memset(a,0,sizeof(a))
    #define msp memset(mp,0,sizeof(mp))
    #define msv memset(vis,0,sizeof(vis))
    #define msd memset(dp,0,sizeof(dp))
    using namespace std;
    //#define LOCAL
    int main()
    {
    #ifdef LOCAL
        freopen("in.txt", "r", stdin);
        //freopen("out.txt","w",stdout);
    #endif // LOCAL
        ios::sync_with_stdio(false);
        int N;
        cin>>N;
        while(N--)
        {
            int n;
            cin>>n;
            int ans=3,ca=2;
            if(n==1)printf("1
    ");
            else if(n==2)printf("3
    ");
            else
            {
                for(int i=3;i<=n;i++)
                {
                    if(i%2)ca+=2;
                    ans+=ca;
                }
                printf("%d
    ",ans);
            }
        }
        return 0;
    }
    View Code
  • 相关阅读:
    什么是.NET Core以及.NET Core能做什么 菜鸟飞不动
    SQL数据库连接字符串的几种写法整理
    高并发
    前端 防抖&节流,你学到未啊?
    Promise实现一个函数,通过fetch请求一个接口'/api/getdata'(可能成功,也可能失败),超过3秒钟请求未返回则认为超时
    手写实现deepClone方法
    手写Promise.retry方法;实现次数内重试请求
    element-ui的table表格通过子表数据,进行展示左侧展开箭头
    页面导出为PDF格式
    js自定义数字跳动效果computeNumber
  • 原文地址:https://www.cnblogs.com/gpsx/p/5212885.html
Copyright © 2011-2022 走看看