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

思路: 定义一个数组用来储存柱子上的数字,如果数字满足条件则a[i] = num 否则 i++

AC代码:

 1 #include<iostream>
2 #include<string.h>
3 #include<math.h>
4 using namespace std;
5
6 int main()
7 {
8 int a[50], b[50];
9 int times, peg, temp;
10 int n = 1;
11 int num = 1;
12 int i = 0, j;
13 cin >> times;
14 while(times--)
15 {
16 cin >> peg;
17 memset(a, 0, sizeof(a));
18 while(1)
19 {
20 if(i == peg)
21 {
22 cout << num - 1 << endl;
23 num = 1, i = 0;
24 break;
25 }
26 if(a[i] == 0)
27 {
28 a[i] = num++;
29 i = 0;
30 continue;
31 }
32 else
33 {
34 j = (int)sqrt(a[i] + num);
35 if(j * j == (a[i] + num))
36 {
37 a[i] = num++;
38 i = 0;
39 continue;
40 }
41 else
42 {
43 i++;
44 }
45 }
46 }
47 }
48
49 return 0;
50 }

10276 - Hanoi Tower Troubles Again!(思维,模拟)的更多相关文章

  1. HDU 1329 Hanoi Tower Troubles Again!(乱搞)

    Hanoi Tower Troubles Again! Problem Description People stopped moving discs from peg to peg after th ...

  2. HDU1329 Hanoi Tower Troubles Again!——S.B.S.

    Hanoi Tower Troubles Again! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (J ...

  3. ZOJ-1239 Hanoi Tower Troubles Again!

    链接:ZOJ1239 Hanoi Tower Troubles Again! Description People stopped moving discs from peg to peg after ...

  4. 【HDOJ】1329 Hanoi Tower Troubles Again!

    水题,搞清楚hanoi的定义就好做了. /* 1329 */ #include <cstdio> #include <cstring> #include <cstdlib ...

  5. hdu 1329 Hanoi Tower Troubles Again!

    找规律的题目an=an-1+(i+i%2)/2*2; ;}

  6. zoj 2954 Hanoi Tower

    Hanoi Tower Time Limit: 2 Seconds Memory Limit: 65536 KB You all must know the puzzle named "Th ...

  7. Codeforces Gym 100114 A. Hanoi tower 找规律

    A. Hanoi tower Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Descript ...

  8. 汉诺塔 Hanoi Tower

    电影<猩球崛起>刚开始的时候,年轻的Caesar在玩一种很有意思的游戏,就是汉诺塔...... 汉诺塔源自一个古老的印度传说:在世界的中心贝拿勒斯的圣庙里,一块黄铜板上插着三支宝石针.印度 ...

  9. 3-6-汉诺塔(Hanoi Tower)问题-栈和队列-第3章-《数据结构》课本源码-严蔚敏吴伟民版

    课本源码部分 第3章  栈和队列 - 汉诺塔(Hanoi Tower)问题 ——<数据结构>-严蔚敏.吴伟民版        源码使用说明  链接☛☛☛ <数据结构-C语言版> ...

随机推荐

  1. Qt update刷新之源码分析(二)

    大家好,我是IT文艺男,来自一线大厂的一线程序员 上次视频给大家从源码层面剖析了Qt update刷新机制的异步事件投递过程,这次视频主要从源码层面剖析Qt刷新事件(QEvent::UpdateReq ...

  2. 简单的webRTC连接测试

    webRTC WebRTC (Web Real-Time Communications) 是一项实时通讯技术,它允许网络应用或者站点,在不借助中间媒介的情况下,建立浏览器之间点对点(Peer-to-P ...

  3. 【深入理解Java虚拟机】垃圾回收

    引用计数算法 给对象加一个计数器,引用一次+1,引用时效就-1,当计数器=0时对象就不能再被使用: 实现简单,判定效率高:Java虚拟接没有使用,主要原因是很难解决对象之间循环引用问题: GC算法: ...

  4. 通达OA任意文件上传+文件包含GetShell/包含日志文件Getshell

    0x01 简介 通达OA采用基于WEB的企业计算,主HTTP服务器采用了世界上最先进的Apache服务器,性能稳定可靠.数据存取集中控制,避免了数据泄漏的可能.提供数据备份工具,保护系统数据安全.多级 ...

  5. AtCoder Beginner Contest 186

    A Brick int n, m; int main() { scanf("%d%d", &n, &m); printf("%d\n", n / ...

  6. ListView解析

    ListView通过一个Adapter来完成数据和组件的绑定.以ListActivity为例,它集成自Activity,里面包含有一个ListAdapter和一个ListView.绑定的操作通过set ...

  7. Codeforces Round #538 D. Lunar New Year and a Wander

    题面: 传送门 题目描述: Bob想在公园散步.公园由n个点和m条无向边组成.当Bob到一个未经过的点时,他就会把这个点的编号记录在笔记本上.当且仅当Bob走完所有的点,他才会停下来.这时,Bob的笔 ...

  8. 欢迎参加3月活动:AWS 在线研讨会与阿里云 RISC-V 应用创新大赛

    3月份我们在帮合作云厂商 Amazon Web Services(AWS) 与阿里云推广2个活动,欢迎感兴趣的园友参加. 活动一:亚马逊云科技在线研讨会:借助 DGL 实现实时欺诈检测 博客园专属报名 ...

  9. P1008_三连击(JAVA语言)

    /*  * 题目描述 将1,2,⋯,9共9个数分成3组, 分别组成3个三位数,且使这3个三位数构成1:2:3的比例,试求出所有满足条件的3个三位数. 输入输出格式 输入格式: 木有输入 输出格式: 若 ...

  10. Java例题_31 逆序输出数组的值

    1 /*31 [程序 31 数组逆序] 2 题目:将一个数组逆序输出. 3 程序分析:用第一个与最后一个交换. 4 */ 5 6 /*分析 7 * 第一种方法:找到这个数组的中间下标,然后交换两端的数 ...