简体   繁体   English

我不明白我的程序出了什么问题

[英]i dont understand what is wrong in my program

this is a programming question and it doesn't give right answer to all cases 这是一个编程问题,它不能为所有情况提供正确的答案

you are playing a video game in which several stacks of boxes are lined up on the floor, with a crane on top to rearrange the boxes 您正在玩一个视频游戏,在游戏中,地板上排列了几叠箱子,并用起重机在上面重新排列了箱子

The crane supports the following commands: 起重机支持以下命令:

• Move one position left (does nothing if already at the leftmost position) •向左移动一个位置(如果已经在最左侧,则不执行任何操作)

• Move one position right (does nothing if already at the rightmost position) •向右移动一个位置(如果已经在最右侧,则不执行任何操作)

• Pick up a box from the current stack (does nothing if the crane already has a box) •从当前堆栈中拾取一个盒子(如果起重机已经有一个盒子,则不执行任何操作)

• Drop a box on the current stack (does nothing if the crane doesn't already have a box) •在当前堆栈上放一个盒子(如果起重机还没有盒子,则什么也不做)

Further, there is a limit H on the number of boxes on each stack. 此外,每个堆栈上的箱子数有限制H。 If a 'drop' command would result in a stack having more than H boxes, the crane ignores this drop command. 如果“放下”命令导致堆栈中的箱子超过H个,起重机将忽略此放下命令。 If the current stack has no boxes, a 'pick up' command is ignored. 如果当前堆栈中没有框,则将忽略“拾取”命令。

You are given the initial number of boxes in each stack and the sequence of operations performed by the crane. 您将获得每个堆栈中的初始箱数以及起重机执行的操作顺序。 You have to compute the final number of boxes in each stack. 您必须计算每个堆栈中盒子的最终数量。

Input format • Line 1 : The width of the game (the number of stacks of boxes), N, followed by the max height H of each stack. 输入格式•第1行:游戏的宽度(盒子的堆叠数)N,然后是每个堆叠的最大高度H。

• Line 2 : N integers, the initial number of boxes in each stack, from left to right. •第2行:N个整数,从左到右,每个堆栈中的初始盒子数。 Each number is ≤ H. 每个数字≤H。

• Line 3 : A sequence of integers, each encoding a command to the crane. •第3行:整数序列,每个整数编码对起重机的命令。

The commands are encoded as follows:

1 : Move left

2 : Move right

3 : Pick up box

4 : Drop box

0 : Quit

• The command Quit (0) appears exactly once, and is the last command. •退出(0)命令仅出现一次,并且是最后一个命令。

• The initial position of the crane is above the leftmost stack, with the crane not holding any box. •起重机的初始位置在最左边的纸堆上方,而起重机没有任何盒子。

#include<iostream>
#include<iomanip>
#include<algorithm>
#include<vector>
using namespace std;
int main()
{
    long long x, y, t;
    cin >> x >> y;
    cout << endl;
    vector<long long> stack(x);
    for (long long i = 0; i < x; i++)
    {
        cin >> stack[i];
    }
    cout << endl;
    vector<long long>::pointer ptr = &stack[0];
    while (cin >> t)
    {
        if (t == 1)
        {
            if (ptr != &stack[0])
                ptr--;

        }
        if (t == 2)
        {
            if (ptr != &stack[x - 1])
                ptr++;
        }
        if (t == 3)
        {
            if (*ptr != 0)
                *ptr = *ptr - 1;
        }
        if (t == 4)
        {
            if (*ptr < y)
                *ptr = *ptr + 1;
        }
        if (t == 0)
            break;
    }
    cout << endl;
    for (long long i = 0; i < x; i++)
    {
        cout << stack[i];
    }
    return 0;
}

There are multiple things wrong with the program: 该程序有很多问题:

  • The program does not check if the crane is holding a box when picking up or dropping. 程序在抬起或放下时不检查起重机是否握住箱子。 This is what is causing the unexpected output. 这就是导致意外输出的原因。

  • The program puts all logic in a single function ( main ), which makes it more difficult to follow the program logic. 该程序将所有逻辑放在一个函数( main )中,这使得遵循程序逻辑变得更加困难。

  • The program uses a pointer to the stack where the crane is over, instead of simply using an index to the position. 该程序使用指向起重机所在的堆栈的指针,而不是简单地使用位置索引。 This also makes the logic more difficult to read. 这也使逻辑更难以阅读。

  • The program uses a sequence of if statements instead of a single switch statement, which again obscures the logic. 该程序使用一系列if语句而不是单个switch语句,这再次模糊了逻辑。

  • The program uses variable names x and y , which are not descriptive of their function, again making the program more difficult to follow. 该程序使用变量名称xy ,它们不能描述其功能,这又使该程序更难以遵循。 Instead use numberOfStacks and maxHeight or something like that, or at least N and H to relate to the problem description. 而是使用numberOfStacksmaxHeight或类似的东西,或者至少使用NH来与问题描述相关。

All but the first point are maybe "small" things, it's a short program and not difficult to read, but it would be easier if you follow this advice, making it easier to spot bugs (and figure out the first point). 除了第一点以外,其他所有内容可能都是“小”东西,这是一个简短的程序,不难阅读,但是如果您遵循此建议,将会更容易,从而更容易发现错误(并找出第一点)。


An example using the position instead of a pointer, using a switch statement, and separating into its own function: 一个使用位置而不是指针,使用switch语句并将其分离为自己的函数的示例:

void processCommands(vector<std::size_t>& stack, std::size_t maxHeight) {
   std::size_t position = 0;
   bool holdingBox = false;
   while (cin >> t) {
      switch (t) {
      case 1:
         if (position != 0)
            --position;
         break;
      case 2:
         if (position != stack.size() - 1)
            ++position;
         break;
      case 3:
         if (!holdingBox && stack[position] != 0) {
            --stack[position];
            holdingBox = true;
         }
         break;
      case 4:
         if (holdingBox && stack[position] != maxHeight) {
            ++stack[position];
            holdingBox = false;
         }
         break;
      case 0:
         return; // Exits current function, return to main where stack is printed
      }
   }
}

(I have also replaced long long for std::size_t , which I think better represents the function of those values (sizes of arrays and sizes of stacks). std::size_t is an unsigned long long on 64-bit machines. Maybe unsigned int would be good enough. (我也取代long longstd::size_t ,我认为更好地表示这些值的函数堆的阵列的(大小和尺寸)。 std::size_t是一个无符号长长在64位机器上。也许unsigned int就足够了。

Main could look like this: Main可能看起来像这样:

int main() {
   vector<std::size_t> stack;
   std::size_t maxHeight = initializeStack(stack);
   processCommands(stack, maxHeight);
   printStack(stack);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM