Dreamoon and Stairs CodeForces - 476A

46 篇文章 0 订阅
17 篇文章 0 订阅

Dreamoon and Stairs CodeForces - 476A

题目描述
Dreamoon wants to climb up a stair of n steps. He can climb 1 or 2 steps at each move. Dreamoon wants the number of moves to be a multiple of an integer m.

What is the minimal number of moves making him climb to the top of the stairs that satisfies his condition?

Input
The single line contains two space separated integers n, m (0 < n ≤ 10000, 1 < m ≤ 10).

Output
Print a single integer — the minimal number of moves being a multiple of m. If there is no way he can climb satisfying condition print  - 1 instead.

这里写图片描述

Note
For the first sample, Dreamoon could climb in 6 moves with following sequence of steps: {2, 2, 2, 2, 1, 1}.

For the second sample, there are only three valid sequence of steps {2, 1}, {1, 2}, {1, 1, 1} with 2, 2, and 3 steps respectively. All these numbers are not multiples of 5.

大致题意
给你一个台阶数n和一个整数m,每次你可以走一个台阶或者两个。问你最少需要走几次恰好到达n台阶处,且这个数是m的倍数,如果不存在则输出-1

思路
一次走两步可以分成两次走一步,所以先尽量都用两步的走,看所需的次数是否是m的倍数,如果不是再慢慢的将两步拆分成一步,以此类推。

下面是代码

#include <iostream> 
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <math.h>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#include<sstream>
#include<ctime>
using namespace std;
int dix[4]={0,1,0,-1};
int diy[4]={1,0,-1,0};
int main()
{
    int n,m,k,flag=0,s;
    cin>>n>>m;
    if(n%2==0)
    {
        s=k=n/2;
        while(k>=0)
        {
            if(s%m==0&&s>=m)
            {
                cout<<s;
                flag=1;
                break;
            }
            s++;
            k--;
        }
    }
    else 
    {
        k=n/2;
        s=k+1;
        while(k>=0)
        {
            if(s%m==0&&s>=m)
            {
                cout<<s;
                flag=1;
                break;
            }
            s++;
            k--;
        }
    }
    if(flag==0)
    cout<<-1;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值