zoukankan      html  css  js  c++  java
  • [置顶] 小技巧总结

    1.识别空行。

    gets(string), if(strlen(string)==0)则是空行


    2.结构体的初始化函数可以放在结构体中,这样可以方便地根据需要对某一个节点进行初始化

     

    3.常见简写:ans(answer),cnt(count),idx(index,下标),init(initial),str(string),pos(position,位置)


    4.生成随机数简单的方法:

    #include <iostream>
    #include <cstdlib>
    using namespace std;
    int main()
    {
        for(int i=0;i<5;i++)
            cout<<rand()<<" ";
        return 0;
    }
    
    但rand()总以1为种子,种子固定,产生的随机数总为同一序列的随机数。上面的程序每次执行的结果都是一样的,在我的机子上都是:41 18467 6334 26500 19169。但是rand()还是可以满足某些需求的,比如测试排序STL 的priority_queue的top为最大项时可以通过rand()生成随数检测。

    功能更强大的是srand,网上资料很多,此处不表。


    5.判断一个数是否整数:有double型数据m,(int)(m+0.5) == m为真则m是整数;注意(int)m == m 可能会带来错误,因为计算机执行运算的时候可能会出现误差


    6.复制数组:使用memcpy,头文件string.h。

    例程:

    #include <iostream>
    #include <cstring>
    using namespace std;
    
    int main()
    {
        int a[5]={0, 1, 2, 3, 4},b[5]={0};
        memcpy(a, b, sizeof(int)*3);
        for(int i=0; i<5; i++) cout<<a[i]<<" ";
        memcpy(a, b, sizeof(b));
        for(int i=0; i<5; i++) cout<<a[i]<<" ";
        return 0;
    }
    

    结果:0 0 0 3 4 0 0 0 0 0


    7.模板

    #include <iostream>
    #include <string>
    #include <cstdio>
    #include <cmath>
    #include <cstring>
    #include <algorithm>
    #include <map>
    #define LL long long
    #define MAXI 2147483647
    #define MAXL 9223372036854775807
    #define eps (1e-8)
    #define dg(i) cout << "*" << i << endl;


  • 相关阅读:
    Java异常处理和设计
    一次qps测试实践
    Alternate Task UVA
    Just Another Problem UVA
    Lattice Point or Not UVA
    Play with Floor and Ceil UVA
    Exploring Pyramids UVALive
    Cheerleaders UVA
    Triangle Counting UVA
    Square Numbers UVA
  • 原文地址:https://www.cnblogs.com/cszlg/p/2910504.html
Copyright © 2011-2022 走看看