zoukankan      html  css  js  c++  java
  • 代码之美

    1. 快排三数取中

    int Mid_of_Three(int a[],int left,int right) 
    {
        int mid=left + (right-left)/2;
     
        if(a[right] < a[left])  Swap(a,left,right);
        if(a[right] < a[mid])   Swap(a,mid,right);
        if(a[left] < a[mid])    Swap(a,left,mid);
      
        return left;
    }

    2. 两次比较返回三数中值

    int Mid_of_Three(int a,int b,int c)
    {
        if( (a-b)*(a-c) < 0 )  return a;
        if( (b-a)*(b-c) < 0 )  return b;
        else  return c;
    }

    3. 二叉树高度

    int Cal_Height(point tree)    // 版本1
    {
         int n_lc=0, n_rc=0;
         if(!tree)   return 0;
         else{
             n_lc = cal_height(tree->lc);
             n_rc = cal_height(tree->rc);
          // return (n_lc > n_rc) ? (n_lc+1):(n_rc+1);    //两个均可  +1 是代表当前根节点
             return 1 + max(cal_height(tree->lc), cal_height(tree->rc));
         }
     }
    int Cal_Height(point tree)    // 版本2
    {
          if(!tree)   return 0;
          else  return  1 + max(cal_height(tree->lc), cal_height(tree->rc));
    }

    4. 高效输出64位长整型

    void PrintInt64(long long a)  
    {  
           if (a<=100000000)
               printf("%d/n", a);  
           else {  
               printf("%d", a/100000000);  
               printf("%08d/n", a%100000000);  
           }  
    } 

    5. a和b两个数 + - * /运算,和/差的平均

  • 相关阅读:
    模板模式变形
    理解volatitle带来的可见性
    数据库隔离级别
    Spring对POST内容进行处理的坑
    动态加载JS和CSS
    MySQL性能优化总结
    JS自执行匿名函数
    CDATA为何物?
    如何编写高效的jQuery代码
    war和war exploded区别
  • 原文地址:https://www.cnblogs.com/wjcx-sqh/p/6195986.html
Copyright © 2011-2022 走看看