• 有符号数、无符号树混合计算问题。


    今天在线刷题,其中一个问题总是结果跟期望的不一样,在一次次的检查程序逻辑、确认无误后,还是不能通过,不得已用VS开始调试!

    这里是我的程序代码:

     1 // maxDepth.cpp : 定义控制台应用程序的入口点。
     2 //
     3 
     4 #include "stdafx.h"
     5 #include<vector>
     6 #include<iostream>
     7 #include<limits.h>
     8 
     9 using namespace std;
    10 
    11 class TreeNode {
    12 public:
    13      int val;
    14      TreeNode *left, *right;
    15      TreeNode(int val) {
    16          this->val = val;
    17          this->left = this->right = NULL;
    18      }
    19  }; 
    20 
    21 class Solution {
    22 public:
    23     /**
    24      * @param root: The root of binary tree.
    25      * @return: An integer
    26      */
    27     void PreOrder(TreeNode *root, std::vector<TreeNode*> &path)
    28     {
    29         if (!root)
    30             return;
    31         path.push_back(root);
    32         if(!root->left && !root->right)
    33             if (path.size() > max)
    34                 max = path.size();
    35         if(root->left) PreOrder(root->left, path);
    36         if(root->right) PreOrder(root->right, path);
    37         path.pop_back();        
    38     }
    39     int maxDepth(TreeNode *root) {
    40         // write your code here
    41         if (!root)
    42             return 0;
    43         std::vector<TreeNode*> path;
    44         PreOrder(root, path); 
    45         return max;
    46     }
    47     Solution():max(INT_MIN){;}
    48 private:
    49     int max;
    50 };
    51 
    52 void Test()
    53 {
    54     int c = -1;
    55     if (c > (unsigned)1)std::cout << "c > -1" << std::endl;
    56     c = INT_MIN;
    57     //std::cout << c;
    58     //std::cout << 
    59     
    60     std::cout << -7 + (unsigned)5 << std::endl;
    61     std::cout << -7 + (unsigned)10 << std::endl;
    62 
    63     //bool b = c > 1;
    64     //std::cout << b ;
    65 
    66 }
    67 
    68 int _tmain(int argc, _TCHAR* argv[])
    69 {
    70     TreeNode *root = new TreeNode(0);
    71     Solution so;
    72     std::cout << so.maxDepth(root) << std::endl;
    73 
    74     Test();
    75 
    76 
    77     
    78     return 0;
    79 }

    调试时发现,33行处if (path.size() > max)怎么都不会执行。如此就明白了,这里涉及到有符号、无符号数的混合运算问题!

    1. int max定义的是有符号数,并且初始化为最小的负数INT_MIN,而size()函数返回的是无符号数,在混合运算中,会把有符号数转化为无符号数。负数的第一位为1,转化为无符号数之后大于所有的正数,所以这里怎么都不会执行!

    2. 转化过程后运算时,可能会发生溢出,C的简单做法就是截断!如我这里的test()函数的编译信息。

    3. 实际上,我在编译时,VS已经对33行发出警告“ warning C4018: “>”: 有符号/无符号不匹配”,可是我却忽视了这个做法。所以,绝不要忽视编译器的任何警告。

    4.实际上,在CSAPP第二章中,就对有符号、无符号数进行了深入的讨论。并且得出了“无符号数的带来的弊端远大于它所带来的作用,尽量少使用无符号数”的结论。可是C++标准库中,很多关于size(),capacity()函数返回的都是无符号数,所以处理这样的程序中,必须小心处理有符号无符号数混合运算的问题!

  • 相关阅读:
    国外pip源下载太慢,修改成国内pip源
    i++和i--
    CSS3之太极图源代码
    对 Vue 的理解(一)
    CSS 盒子模型及 float 和 position
    Notes about Vue Style Guide
    TypeScript 入门笔记
    flex 布局
    git rebase 和 git merge 总结
    理解JavaScript作用域
  • 原文地址:https://www.cnblogs.com/bitpeng/p/4783984.html
Copyright © 2020-2023  润新知