Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 到达约翰的顶端 问题陈述_C++_Algorithm - Fatal编程技术网

C++ 到达约翰的顶端 问题陈述

C++ 到达约翰的顶端 问题陈述,c++,algorithm,C++,Algorithm,约翰想爬上n级台阶。他每走一步都能爬上一两步。John希望移动的次数是整数m的倍数 让他爬到满足条件的楼梯顶部的最小步数是多少 输入 单行包含两个空格分隔的整数n,m(0  我个人根本不明白二者的力量有多大用处 让我们先用伪代码编写一个不同的算法: N = number of steps M = desired multiple # Excluding any idea of the multiple restraint, what is the maximum and minimum # n

约翰想爬上n级台阶。他每走一步都能爬上一两步。John希望移动的次数是整数m的倍数

让他爬到满足条件的楼梯顶部的最小步数是多少

输入
单行包含两个空格分隔的整数n,m(0  我个人根本不明白二者的力量有多大用处

让我们先用伪代码编写一个不同的算法:

N = number of steps
M = desired multiple

# Excluding any idea of the multiple restraint, what is the maximum and minimum
# number of steps that John could take?

If number of steps is even:
    minimum = N / 2
    maximum = N

If number of steps is odd:
    minimum = N / 2 + 1
    maximum = N

# Maybe the minimum number of steps is perfect?

If minimum is a multiple of M:
    Print minimum

# If it isn't, then we need to increase the number of steps up to a multiple of M.
# We then need to make sure that it didn't surpass the maximum number of steps.

Otherwise:
    goal = minimum - (minimum % M) + M

    if goal <= maximum:
        Print goal
    Otherwise:
        Print -1
N=步骤数
M=所需倍数
#排除任何关于多重约束的想法,最大值和最小值是多少
#约翰可以采取多少步骤?
如果步骤数为偶数:
最小值=N/2
最大值=N
如果步骤数为奇数:
最小值=N/2+1
最大值=N
#也许最小的步数是完美的?
如果最小值是M的倍数:
打印最小值
#如果不是,那么我们需要将步数增加到M的倍数。
#然后我们需要确保它不会超过最大步数。
否则:
目标=最小值-(最小%M)+M

如果目标请更新你问题的标题,它不能正确反映内容。@user111:我很确定你原始问题中的算法是错误的。我在这里展示的算法/代码是否给出了正确答案?如果没有,你能告诉我一个你得到错误答案的例子吗?
2
你的程序给出了
-1
但答案应该是2@sharth@user111:你说得对!在我的描述中,我说了“包含”,但我在翻译成代码时把它弄糟了。现在应该修复它。
#include<stdio.h>
#include<math.h>

using namespace std;

int main(){
  int n,m;
  scanf("%d %d",&n,&m);
  int x= pow (2,floor (log2(n)) );
  int rem = n-x;
  int ans = ((x/2)+rem);
  if ( ans % m == 0 )
    printf (" %d \n ",ans);
  else
    printf("-1\n");

  return 0;
}
N = number of steps
M = desired multiple

# Excluding any idea of the multiple restraint, what is the maximum and minimum
# number of steps that John could take?

If number of steps is even:
    minimum = N / 2
    maximum = N

If number of steps is odd:
    minimum = N / 2 + 1
    maximum = N

# Maybe the minimum number of steps is perfect?

If minimum is a multiple of M:
    Print minimum

# If it isn't, then we need to increase the number of steps up to a multiple of M.
# We then need to make sure that it didn't surpass the maximum number of steps.

Otherwise:
    goal = minimum - (minimum % M) + M

    if goal <= maximum:
        Print goal
    Otherwise:
        Print -1
#include <cstdio>

int main(){
    int n,m;
    scanf("%d %d", &n, &m);

    const int minimum = (n / 2) + (n % 2);
    const int maximum = n;

    if (minimum % m == 0) {
        printf("%d\n", minimum);
        return 0;
    }

    const int guess = minimum - (minimum % m) + m;
    if (guess <= maximum) {
        printf("%d\n", guess);
        return 0;
    }

    printf("%d\n", -1);
    return 0;
}