zoukankan      html  css  js  c++  java
  • wenbao与acm技巧(必备知识)

    推荐博客:http://blog.csdn.net/xueerfei008/article/details/19029797    http://blog.csdn.net/xueerfei008/article/details/11834011

    计算一个数中一的个数

     1 cout<<__builtin_popcount(7); 

    提高速度

     1 std::ios::sync_with_stdio(false); 

     cin.tie(0),cout.tie(0);

    程序运行时间

     1 #include <stdio.h>
     2 #include <time.h>
     3 int main() {
     4     clock_t start = clock();
     5     
     6     // Place your codes here...
     7     for(int i = 0; i < 10000000; ++i){
     8         for(int j = 0; j < 100; ++j);
     9     }
    10     
    11     clock_t ends = clock();
    12 
    13     printf("Running Time :%lf
    ", (ends - start) / CLOCKS_PER_SEC * 1000.0);
    14     
    15     return 0;
    16 }

    哎。。忘记是求什么的了

    (n*m-1)/(m-1)

    1*n+2*(n-1)+3*(n-2)+…+n*1=n(n+1)(n+2)/6

    1+2+3+…+n=n(n+1)/2
    1²+2²+3²…+n²=n(n+1)(2n+1)/6
    1³+2³+3³+…n³=n²(n+1)²/4=[n(n+1)/2]²
    1^4+2^4+3^4+...+n^4=n(n+1)(2n+1)(3n^2+3n-1)/30
    1*2+2*3+3*4+4*5+…+n(n+1)=n(n+1)(n+2)/3

    PI值

     const double PI = acos(-1.0); 

    @关于for:

    1 //处理字符串的时候可以这样写————
    2 for(int i = 0; str[i]; i++)
    3 
    4 //但是处理数组只能这样写(不然多组输入的时候会有问题出现,应为memse只是清除字符串的t)
    5 for(int i = 0; i < n; i++)

    输出到文本

    1 #include <iostream>
    2 using namespace std;
    3 int main(){
    4     freopen("in.txt", "r", stdin);
    5     freopen("out.txt", "w",stdout);
    6     int a,b;
    7     while(cin>>a>>b) cout<<a+b<<"
    ";
    8 }
    1 #include <fstream>
    2 using namespace std;
    3 ifstream fin("abc.txt");
    4 ofstream fout("abcd.txt");
    5 int main(){
    6     int a,b;
    7     while(fin>>a>>b) fout<<a+b<<"
    ";
    8     return 0;
    9 }

    @  关于memset

    大牛博客:http://www.cnblogs.com/LLGemini/p/4309660.html

     1 memset是用于字符串处理的,不再多说,那么处理int数组怎么办呢?要怎样处理呢?
     2 //    *******char 是一个字节而int是四个字节
     3 memset(a, 0, sizeof(a));    //    正确
     4 memset(a, 1, sizeof(1));    //    错误,字节问题初始化的值为:16843009
     5 int型的最大值为 0x7f,所以可以 memset(a, 0x7f, sizeof(a)); 初始化为最大值,但是非常容易爆int;
     6 最精巧的无穷大常量取值是0x3f3f3f3f,0x3f3f3f3f的十进制是1061109567,也就是10^9级别的,0x3f3f3f3f+0x3f3f3f3f=2122219134,没有爆int;
     7 
     8 所以说0x3f3f3f3f是最棒的选择
     9 0x3f3f3f3f的每个字节都是0x3f!所以要把一段内存全部置为无穷大,我们只需要
    10 memset(a,0x3f,sizeof(a));
    11 
    12 其他赋值:
    13 memset(arr,0x7F,sizeof(arr)); //它将arr中的值全部赋为2139062143,这是用memset对int赋值所能达到的最大值
    14 memset(arr,0x80,sizeof(arr)); //set int to -2139062144
    15 memset(arr,0x7F,sizeof(arr)); //set double to 1.38242e+306
    16 memset(arr,0xFE,sizeof(arr)); //set double to -5.31401e+303

    @  int   long long int 范围

     1 unsigned   int   04294967295        0 ~ 4*10^9;    
     2 int   -21474836482147483647    -1*2*10^9 ~ 2*10^9;
     3 unsigned long 04294967295        0 ~ 4*10^9;
     4 long   -21474836482147483647        -1*2*10^9 ~ 2*10^9;
     5 long long的最大值:9223372036854775807        9*10^18;
     6 long long的最小值:-9223372036854775808        -1*9*10^18;
     7 unsigned long long的最大值:18446744073709551615    1*10^19;
     8 
     9 __int64的最大值:9223372036854775807    9*10^18;
    10 __int64的最小值:-9223372036854775808    -1*9*10^18;
    11 unsigned __int64的最大值:18446744073709551615        1*10^19;

     @  用c++11编译

        g++ -std=c++11 y.cpp -o y

    @  强制转化

        1LL

    @  n的阶乘的位数

      @@  斯特林公式

          

          更加精确的近似公式为:

            

          其中

            

    利用斯特林(Stirling)公式求解n!的位数:

    易知整数n的位数为[lg10(n)]+1. 
    用Stirling公式计算n!结果的位数时,可以两边取对数,得: 
    log10(n!) = log10(2*PI*n)/2+n*log10(n/E); 
    故n!的位数为

    res=log10(2*PI*n)/2+n*log10(n/E)+1

    或者为:

    long result = (long)((log10(sqrt(4.0*acos(0.0)*n))+n*(log10(n)-log10(exp(1.0))))+1); (编程更方便)

    (注意:当n=1时,算得的结果为0)

    n!的估计:

    在高德纳的《计算机程序设计艺术》一书中,对n!的估计: 
    n! = sqrt(2*π*n) * ((n/e)^n) * (1 + 1/(12*n) + 1/(288*n*n) + O(1/n^3));

      x = log10N!

    long result = (long)((log10(sqrt(4.0*acos(0.0)*n))+n*(log10(n)-log10(exp(1.0))))+1);

     1 #include <iostream>
     2 #include <cmath>
     3 using namespace std;
     4 int main(){
     5     int n, t;
     6     cin >>t;
     7     while(t--){
     8         cin>>n;
     9         if(n==1) cout<<1<<endl;
    10         else{
    11             long result = (long)((log10(sqrt(4.0*acos(0.0)*n))+n*(log10(n)-log10(exp(1.0))))+1);
    12             cout<<result<<endl;
    13         }    
    14     }
    15     return 0;
    16 }

    @  memcpy函数

      memcpy(a, b, sizeof(a));

      将b赋值给a,b是源

    @  必备数学知识

    大牛博客:http://blog.sina.com.cn/u/2056445344

    求与 y=-x-1, y=x-1, y=0 三条直线相切的抛物线的方程

    三角形垂心坐标公式

    三角形重心坐标公式及欧拉线

    平面三角形外心坐标公式

    平面三角形内心坐标公式

    注:若在上述去绝对值时,适当地变动符号,可以推是其他三个旁心坐标公式。实质上内心与旁心是一样的!旁心是内心的延伸,内心是到三边所对应的线段,而旁心是三边所对应的直线。

    斐波那契额数列

    字符输入

    1 char read(){
    2     char ch;
    3     for(;;){
    4         ch = getchar();
    5         if(ch >= 'A' && ch <= 'Z') return ch;
    6     }
    7 }

    for循环

      1 #define REP(i, n) for(int i = 0; i < n; i++) 

    组合数学

     1 C(n+1, k+1) = C(n, k) + C(n, k+1)

    2 C(n, k+1) = C(n, k)*(n-k)/(k+1) 

    只有不断学习才能进步!

  • 相关阅读:
    经验谈 论前端架构的重要性
    论 Angular的混乱
    DTW 算法(转)
    软件提高发射功率原理
    (转)LSI SAS 1068E Raid CentOS 5.5 安装实例浪潮NF5220系列 分类: linux
    聚类算法总结
    信号相似性的描述
    python科学计算整理
    一个无线通信类投稿的期刊list
    2012年Elsevier旗下Computer Science期刊最新SCI影响因子排名
  • 原文地址:https://www.cnblogs.com/wenbao/p/5727729.html
Copyright © 2011-2022 走看看