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
Algorithm 不了解翻转的技巧_Algorithm_Swap - Fatal编程技术网

Algorithm 不了解翻转的技巧

Algorithm 不了解翻转的技巧,algorithm,swap,Algorithm,Swap,我正在尝试解决一个名为的acm问题。但不幸的是,我没能理解这个问题。所以我试着用谷歌找到了。这个解释对flipk说。但我不知道怎么做。谁来给我解释一下。提前谢谢 以下是您最初的ACM问题中的相关内容: 翻转包括在堆栈中的两个煎饼之间插入抹刀,然后翻转抹刀上的煎饼,翻转子堆栈。翻转是通过指定要相对于整个堆栈翻转的子堆栈底部煎饼的位置来指定的。整个堆栈底部的煎饼位置为1,n个煎饼堆栈顶部的煎饼位置为n flipk的意思是将煎饼的顺序从1..k颠倒过来。问题的一部分是弄清楚怎么做 使用问题中的示例,假

我正在尝试解决一个名为的acm问题。但不幸的是,我没能理解这个问题。所以我试着用谷歌找到了。这个解释对flipk说。但我不知道怎么做。谁来给我解释一下。提前谢谢

以下是您最初的ACM问题中的相关内容:

翻转包括在堆栈中的两个煎饼之间插入抹刀,然后翻转抹刀上的煎饼,翻转子堆栈。翻转是通过指定要相对于整个堆栈翻转的子堆栈底部煎饼的位置来指定的。整个堆栈底部的煎饼位置为1,n个煎饼堆栈顶部的煎饼位置为n

flipk的意思是将煎饼的顺序从1..k颠倒过来。问题的一部分是弄清楚怎么做

使用问题中的示例,假设您实现了如下煎饼堆栈:

int[] pancakes = [8, 4, 6, 7, 5, 2];
int flip(int pos) {
    int spare;
    for (int i = 1; i < pos/2; i++) {
        // swap element [i] with element [pos-i+1]
        spare = pancakes[i];
        pancakes[i] = pancakes[pos-i+1];
        pancakes[pos-i+1] = spare;
    }
}
翻转操作可以这样编写:

int[] pancakes = [8, 4, 6, 7, 5, 2];
int flip(int pos) {
    int spare;
    for (int i = 1; i < pos/2; i++) {
        // swap element [i] with element [pos-i+1]
        spare = pancakes[i];
        pancakes[i] = pancakes[pos-i+1];
        pancakes[pos-i+1] = spare;
    }
}
flip3将只交换[1]和[3],而[2]保持不变 flip4将交换[1]和[4],然后交换[2]和[3]