血泪教训 in PAT-A | 算法无关的PAT tricks

让CodeBlocks支持C++11语法

转自stackoverflow

  1. Go to Toolbar -> Settings -> Compiler
  2. In the Selected compiler drop-down menu, make sure GNU GCC Compiler is selected
  3. Below that, select the compiler settings tab and then the compiler flags tab underneath
  4. In the list below, make sure the box for "Have g++ follow the C++11 ISO C++ language standard [-std=c++11]" is checked
  5. Click OK to save
然后,auto大法好!!!!!

节约时间的tricks

  • 读题时,把输入、变量名列在纸上。注意标号的起始是0还是1,上限是多少。图论题尽量用邻接矩阵。
  • 能放在函数内部,就别放在全局,方便调试时观察!!!
  • 手动模拟一下样例,把大致流程图在纸上画好再敲代码!!!流程图上最好保留中间结果!!!方便定位错误!!
  • 每个函数末尾,输出调试一下!不要等写了七八十行一百来行了再测试!!!
  • 想清楚,传值还是传引用。
  • 注意初始化,用过之后再当新的用也要初始化(全局变量,在某函数中被修改了)。某些min/max的初始值要注意⚠️,用vector path的初始化也要注意.
    找最短的一条,一定保证path不为空,为空则直接path = temp_path,如2019浙大复试上机的最后一题。

输入处理(注意换行符)

  • string映射为int,两个map
  • #include getline(cin, str);
  • string类型,str.find(substr)返回的是起始下标,找不到则返回-1
  • sscanf与stringstream函数的用法总结
  • stringstream用法
    https://blog.csdn.net/Sophia1224/article/details/53054698
操作 string 字符数组
定义字符串 string s; char s[100];
取得第i个字符 s[i] s[i]
字符串长度 s.length()或 s.size() strlen(s)
读入一行 getline(cin, s); gets(s);
赋值 s = "you"; strcpy(s, "you");
字符串连接 s = s + "you"; s += "you"; strcat(s, "you");
字符串比较 s == "you" > < strcmp(s, "you");

string 函数用法

  1. 截取子串
    s.substr(pos, n)    截取s中从pos开始(包括0)的n个字符的子串,并返回
    s.substr(pos)        截取s中从从pos开始(包括0)到末尾的所有字符的子串,并返回
    
  2. 替换子串
    s.replace(pos, n, s1)    用s1替换s中从pos开始(包括0)的n个字符的子串
    
  3. 查找子串(找不到返回-1)
    s.find(s1)         查找s中第一次出现s1的位置,并返回(包括0)
    s.rfind(s1)        查找s中最后次出现s1的位置,并返回(包括0)
    s.find_first_of(s1)       查找在s1中任意一个字符在s中第一次出现的位置,并返回(包括0)
    s.find_last_of(s1)       查找在s1中任意一个字符在s中最后一次出现的位置,并返回(包括0)
    s.fin_first_not_of(s1)         查找s中第一个不属于s1中的字符的位置,并返回(包括0)
    s.fin_last_not_of(s1)         查找s中最后一个不属于s1中的字符的位置,并返回(包括0)
    

https://www.twblogs.net/a/5b7e8e9b2b717767c6aa6478
https://c1rew.github.io/2019/04/12/C-remove-erase-%E7%94%A8%E6%B3%95%E6%B5%85%E6%9E%90/

sscanf()用法

c字符串的函数用法

3月把心态搞崩了的输入处理(用sscanf好写)

    while (getline(cin, str)) {
        if (str.find("end") != -1) {
            cout << "-------- end --------" << endl;
            break;
        }
        int pos;
        bool res;
        if ((pos = str.find("root")) != -1) {
            int a;
            ss << str;
            ss >> a;
            cout << a << endl;
            res = isRoot(a);
        } else if ((pos = str.find("siblings")) != -1) {
            int a, b;
            pos = str.find("and");
            str.erase(pos, 4); // "8 and 2 are siblings" - "and "
            ss << str;
            ss >> a >> b;
            cout << a << " " << b << endl;
            res = areSiblings(a, b);
        } else if ((pos = str.find("is the parent of ")) != -1) {
            int a, b;
            str.erase(pos, 16); // "32 is the parent of 11" - "is the parent of "
            ss << str;
            ss >> a >> b;
            cout << a << " " << b << endl;
            res = isParent(a, b);
        } else if (str.find("child of") != -1) {
            int a, b, lr;
            if (str.find("left") != -1) {
                lr = 1;
                sscanf(str.data(), "%d is the left child of %d", &a, &b);
            } else {
                sscanf(str.c_str(), "%d is the right child of %d", &a, &b);
                lr = 2;
            }
            cout << a << " " << b << " " << lr << endl;
            res = isLRChild(a, b, lr);
        } else if ((pos = str.find("on the same level")) != -1) {
            int a, b;
            sscanf(str.data(), "%d and %d are on the same level", &a, &b);
            cout << a << " " << b << endl;
        } else if (str.find("full tree") != -1) {
            res = isFullTree();
        }
//        ss.clear();
        ss.str("");
    }

数组大小不应过大,否则段错误

1105 Spiral Matrix (25 分)

螺旋矩阵,关键在于 确定合理的数组大小
题中给出n(至多10000)个数,要求行数M * 列数N恰为n,且M不小于N。
那么N最大为100,M最大为10000(实际上是小于10000的最大素数)。

#include 
#include 
#include 
#include 

using namespace std;

int main() {
    int nn, matrix[10001][101], arr[10001];
    scanf("%d", &nn);
    int mmm, nnn;
    for (nnn = int(sqrt(nn)); nnn > 0; --nnn) {
        if (nn % nnn == 0) {
            mmm = nn / nnn;
            break;
        }
    }
    for (int i = 0; i < nn; ++i) {
        scanf("%d", &arr[i]);
    }
    sort(arr, arr + nn, greater<>());
    int index = 0, clock = nnn / 2;
    for (int j = 0; j < clock; ++j) {
        // left ---- right
        for (int i = j; i < nnn - j; ++i) {
            matrix[j][i] = arr[index++];
        }
        // top ---- bottom
        for (int i = j + 1; i < mmm - j - 1; ++i) {
            matrix[i][nnn - j - 1] = arr[index++];
        }
        // right ---- left
        for (int i = nnn - j - 1; i >= j; --i) {
            matrix[mmm - j - 1][i] = arr[index++];
        }
        // bottom ---- top
        for (int i = mmm - j - 2; i > j; --i) {
            matrix[i][j] = arr[index++];
        }
    }
    int ii = clock;
    while (index < nn) {
        matrix[ii++][clock] = arr[index++];
    }
    for (int i = 0; i < mmm; ++i) {
        for (int j = 0; j < nnn; ++j) {
            printf("%d", matrix[i][j]);
            printf(j < nnn - 1 ? " " : "\n");
        }
    }
    return 0;
}

你可能感兴趣的:(血泪教训 in PAT-A | 算法无关的PAT tricks)