Codeforces Round #122 (Div. 2) B. Square

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

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
               }
          }
    }     

    
    
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值