天天看點

刷題記錄

刷題記錄

常見題型

  • 模拟
  • STL
  • 字元串
  • 數字
  • 暴力
  • 搜尋(BFS/DFS)
  • 圖論
  • 貪心
  • DP

PIPIOJ周賽記錄

PAT記錄

常用技巧

  • 多組輸入
    // c++使用方法
    while(cin>>c && c != 0){
    	...
    }
    
    // c使用方法
    while(scanf("%d", &t) != EOF){
        ...
    }
               
  • 直接輸入,輸出16進制
    scanf("%llx", &a);
    printf("%llx", a);
               
  • 結構體使用位址盡量使用指針,不然可能會出現以下問題
    struct Node{
    	int id;
    };
    
    void test1(){
    	Node *t;
    	for(int i = 0; i < 2; i ++){
    		Node *node = (Node*)malloc(sizeof(Node));
    		node->id = i;
    		if(i == 0){
    			t = node;
    		}
    		cout<<"i:"<<i<<endl;
    		printf("%p\n", node);//新配置設定的位址
    	}
    	
    	cout<<t->id<<endl;//為0
    	return 0;
    }
    
    void test2(){
    	Node *t;
    	for(int i = 0; i < 2; i ++){
    		Node node;
    		node.id = i;
    		if(i == 0){
    			t = &node;
    		}
    		cout<<"i:"<<i<<endl;
    		printf("%p\n", &node);//每次都一樣,同一個位址
    	}
    	
    	cout<<t->id<<endl;//為1
    	return 0;
    }
               
  • 字元串輸入輸出
    //c++使用頭檔案<bits/stdc++.h>
    string s;
    printf("%s", s.c_str());//string不是c中的string,需要調用c_str()才能對應%s
    
    //輸入一行
    getline(cin, s);//輸入一行,包括空格,直到換行符為止
    gets(s);//輸入一行,包括空格,直到換行符為止
               
  • 字元數組定義
    char str[10] = {"0123456789"};
               
  • ctype頭檔案使用
    #include<ctype>
    bool isDigial(char c);//判斷是否是資料
    char tolower(char c);//轉換成小寫
               
  • 對于特定格式的輸入可以使用scanf();簡化
    //輸入 "11:12"
    scanf("%d:%d", &a, &b);//簡化了':'處理
    
    //對于單個字元前的無效空格和換行
    scanf(" %c", c);//自動跳過之前的無效空格與換行
               

常見錯誤

  • 判斷資料類型以及資料範圍
    • 何時使用int和double
    • 是否需要使用long long
    //long long的輸入與輸出
      long long a;
      printf("%lld", a);//或printf("%l64", a);
      
      //強轉long long
      long long c = 1ll * a * b;