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;
}
}
}
}
本文解答了一道算法题,题目描述了一个正方形及其周围画交叉线的过程,通过分析规律,计算出在特定条件下画出的交叉线条数。详细介绍了解题思路及代码实现。
418

被折叠的 条评论
为什么被折叠?



