There is a square painted on a piece of paper, the square's side equals n meters. John Doe draws crosses on the square's perimeter. John paints the first cross in the lower left corner of the square. Then John moves along the square's perimeter in the clockwise direction (first upwards, then to the right, then downwards, then to the left and so on). Every time he walks (n + 1) meters, he draws a cross (see picture for clarifications).
John Doe stops only when the lower left corner of the square has two crosses. How many crosses will John draw?
题解:这就是找规律了。尝试把前10的推出来,可以看看偶数都是固定规律4*n+1,奇数分情况考虑,比如3,7和 1,5……是不同的。
#include<stdio.h>
#include<iostream>
using namespace std;
int main()
{
int n, i;
cin>>n;
while(n--)
{
long long int t,k;
cin>>t;
if(t % 2 == 0)
{
cout<<t*4+1<<endl;
}
else
{
if(t%4==1)
{
cout<<1+t*2<<endl;
}
else
{
k=(t+1)/4;
cout<<4*k<<endl;
}
}
}
}