ZOL 3977. Pointers

  太久没有做 zoj,对 oj 来说,由于它高度的”黑盒性“(输入数据和答案完全保密),保护自信心是非常重要的。所以我先选择一道非常简单的题目刷起。本题目是一个相当简单的题目,难度系数和求 A+B 相当。

  本题,已知一个指针,初始状态指向 N(北),现在对指针做一系列顺时针(C)或者逆时针(A)旋转 90 度的操作,问指针然后指向哪个方向。

  由于四个方向形成一个循环,所以很自然的提示出,把四个方向所在的”圆环“展开成一个数组,所有的旋转操作实际上是移动数组内的索引,对索引进行递增或者递减的操作。然后对索引进行对数组长度的 MOD (取余)操作,限制在合理范围内即可。

  设索引值为 x,初始值为 0,定义向逆时针方向旋转定义为正方向,则:

  逆时针(A)旋转 90 度:x = ( x + 1 )  % 4;

  顺时针(C)选择 90 度:x = ( x - 1 + 4 ) % 4 = ( x + 3 ) % 4;

  因此,我们需要把旋转方向(A 或 C),映射到对 x 的递增值(1 或 3 )上。因此我们发现这里有一个巧合:A 和 C 之间的差值(C - A = 2),恰好也是这个递增值之间的差值( 3 - 1 = 2)。所以这个映射关系,不需要使用条件判断,可以直接写出此映射关系:

  x = ( x + *p - ‘A‘ + 1 ) % 4; ( *p = ‘A‘ 或 ‘C‘ )

  如果我们进一步查阅一下 ASCII 码表,上面的代码也等效于:

  x = ( x + *p - ‘@‘ ) % 4; 或者  x = ( x + *p - 0x40 ) % 4;

  最终代码如下:

#include <stdio.h>
int main(int argc, char* argv[])
{
    int i, x, count = 0;
    char *p;
    char directions[8] = "NWSE";
    char line[128];
    scanf("%d\n", &count);
    for(i = 0; i < count; i++)
    {
        gets(line);
        p = line;
        x = 0;
        while(*p)
        {
            x = (x + (*p - ‘A‘ + 1)) & 3;
            ++p;
        }
        printf("%c\n", directions[x]);
    }
    return 0;
}

  【补充】由于旋转次数很少(不超过 100 次),因此如果我们一直对 x 进行累加,也不会溢出 int 的最大值。所以在 while 循环中的 MOD 操作可以去除,仅在最后输出结果时,对 x 进行一次 MOD 操作即可。

时间: 2024-10-12 00:21:22

ZOL 3977. Pointers的相关文章

LeetCode OJ:Populating Next Right Pointers in Each Node II(指出每一个节点的下一个右侧节点II)

Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tree could be any binary tree? Would your previous solution still work? Note: You may only use constant extra space. For example,Given the following binary tr

[leetcode] Populating Next Right Pointers in Each Node

Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; } Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL. Initially, al

leetcode--Populating Next Right Pointers in Each Node II

Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tree could be any binary tree? Would your previous solution still work? Note: You may only use constant extra space. For example,Given the following binary tr

leetcode 117 Populating Next Right Pointers in Each Node II ----- java

Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tree could be any binary tree? Would your previous solution still work? Note: You may only use constant extra space. For example,Given the following binary tr

【树】Populating Next Right Pointers in Each Node

题目: Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; } Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL. Initially

More Effective C++ 条款28 Smart Pointers(智能指针)

1. 智能指针(如标准库的auto_ptr,shared_ptr,weak_ptr,boost的scoped_ptr等)主要用于动态内存的管理,同时提供给用户与内置指针一样的使用方法,本条款主要涉及智能指针在构造与析构,复制和赋值,解引等方面的注意点,而非智能指针的实现细节. 2. 智能指针的构造,赋值,析构 智能指针的copy constructor,assignment operator,destructor对应于不同的观念而有不同的实现,主要有三种选择: 1).不允许对象的共享,在调用co

LeetCode——Populating Next Right Pointers in Each Node

Description: Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; } Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL.

More Effective C++ 条款1 仔细区别pointers和references

1. 初始化的区别:有空指针(NULL),但没有空引用,和const变量一样,引用一旦定义就必须和对象绑定.(当然char* pc=0;char& rc=*pc;也合法但无意义) 由此造成的影响: 1) dynamic_cast,对于指针的down_cast,如果失败就返回空指针,但由于没有"空引用"的说法,所以对于引用的down_cast如果失败会抛出一个bad_cast异常. 2) 由于对于引用的使用不需要测试其有效性,而对于指针的使用往往要测试其是否为空. 2. 赋值的区

Populating Next Right Pointers in Each Node -- leetcode

Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; } Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL. Initially, al