URAL 1224. Spiral (规律)

1224. Spiral

Time limit: 1.0 second
Memory limit: 64 MB
Problem illustration
A brand new sapper robot is able to neutralize mines in a rectangular region having integer height and width ( N and M respectively). Before the robot begins its work it is placed near the top leftmost cell of the rectangle heading right. Then the robot starts moving and neutralizing mines making a clockwise spiral way (see picture). The spiral twists towards the inside of the region, covering all the cells. The region is considered safe when all the cells are visited and checked by the robot.
Your task is to determine the number of the turns the robot has to make during its work.

Input

The input contains two integers in the following order: N, M (1 ≤ N, M ≤ 2 31 − 1) .

Output

The output consists of a single integer value — the number of the turns.

Sample

inputoutput
3 5
4
Problem Source: 2002-2003 ACM Central Region of Russia Quarterfinal Programming Contest, Rybinsk, October 2002



解析:找规律。一定要注意n和m的大小关系,因为出发地点是固定的。



AC代码:

#include <bits/stdc++.h>
using namespace std;

int main(){
    long long n, m;
    while(~scanf("%lld%lld", &n, &m)){
        long long ans;
        if(n <= m) ans = 2 * (n - 1);
        else ans = 2 * m - 1;
        printf("%lld\n", ans);
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值