我的程式設計學習日誌(5)-- 教你快速的輸入程式碼(程式設計競賽中的小技巧)
阿新 • • 發佈:2019-01-31
1,typedef簡化輸入
在程式設計中如果用到結構體,每次定義變數時都要輸很長的程式碼,特別是在建連結串列時,經常重複輸入struct…,為了避免這種重複,可以用typedef 。
不過不建議在實際的程式設計中運用,但在競賽中為了更快的輸入,這確實是一個好方法。
如:
#include<iostream> using namespace std; struct TEMP { int a; int b; }; int main() { struct TEMP one; struct TEMP *tow; typedef struct TEMP temp; typedef struct TEMP * ptemp; temp one_1;//相當於struct TEMP one_1; ptemp tow_1;//相當於struct TEMP *tow_1; return 0; }
2,簡化for迴圈輸入
和typedef的思想一樣,for迴圈輸入的簡化也是用一個簡單的東西代替比較長的for迴圈,不過其實這個不怎麼實用,只有當程式中經常遇到同一種類型的for迴圈才實用。
#include<iostream> using namespace std; #define F(i,a,b) fro(int i=(a);i>=(b);i++) int main() { int i; F(i,0,5) cout<<i; //相當於 for(i=0;i>=5;i++) cout<<i; return 0; }