【代碼規範】競賽 C/C++ 通用代碼規範

前言 

此規範爲通用規範,爲參加競賽隊伍統一代碼風格而設定。

整體規範

1. 空行:在長代碼的條件下,若代碼量較大,應給不同作用的代碼塊中間用適當的空行隔開。若代碼量過大,因適當的使用函數。

2. 括號:左括號不換行。

3. 縮進:推薦用兩個空格作爲縮進。

5. 變量名/函數名:(1) 應使用和作用有關的英文,可由多個單字組成。

                              (2) 簡單變量名(一兩個字母)應作用明顯,不宜過多。

                              (3) 推薦使用大駝峯命名法(帕斯卡命名法):多個單詞組成的名字應每個單詞首字母大寫。

                               例如 a, b, c, d 此類命名不宜過多。

                               首字母大寫:PrintEmployeePaychecks()   

                               下劃線:print_employee_paychecks()

                               錯誤:Print_Employee_Paychecks()(下劃線 + 首字母大寫)、printemployeepaychecks() (全小寫)

5. 成對書寫:輸入'()','[]','{}' 時應成對輸入,避免疏漏。

6. 註釋:註釋時,'//' 應與代碼行相隔兩個空格,'//'後再添加一個空格。若在一行上,不宜過長。若有條件,推薦使用英文註釋。

int left = 1, right = n;  // left is the leftmost index of Array;

7. 對齊:應嚴格按照代碼格式進行縮進。'{' 不換行,且每個 '{}' 直接的代碼塊應增加一個縮進格式!

8. 空格:每個運算符的兩旁應嚴格增加一個空格,由逗號分隔的多個表達式前,嚴格用一個空格!(第一個表達式除外)

例如:if (a == b)、sum += a;、a && b、power(a, b, c, d, c + 1)  紅色下劃線處均爲一個空格。

細節規範

1. 頭文件:引入頭文件,"#include" 與 "<...>" 之間應有一個空格。

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>

2.  定義常量:若題目已經給定常量,應用 "const" 類型定義,並給上界適當留出空間。

const int maxn = 1e5 + 10;
const int mod = 1e9 + 7;

3. 重命名:若題目需要使用到 long long ,應使用 "LL" 進行重命名。

typedef long long LL;

4. 定義函數:函數名字應使用英文,並以作用相關。並遵循下圖規則。

     

5. 主函數:規則如下。

  

6. 判斷語句:規則如下。

  

7. 循環語句:while。規則如下。

  

8. 循環語句:for。規則如下。

  

9. 輸入語句:規則如下

  

10. 輸出語句:規則如下。

  

參考代碼片段

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long LL;
const int maxn = 1e5 + 10;
int num[maxn], dp[maxn][3];

int dfs(int len, bool last6, bool limit) {
  if (len == 0) return 1;
  if (limit == false && dp[len][last6] != 0) return dp[len][last6];
  int cnt = 0, maxx = limit ? num[len] : 9;
  for (int i = 0; i <= maxx; i++) {
    if ((i == 2 && last6 == true) || i == 4) continue;
    cnt += dfs(len - 1, i == 6, limit && i == maxx);
  }
  return limit ? cnt : (dp[len][last6] = cnt);
}

int solve(int x) {
  int k = 0;
  memset(num, 0, sizeof num);
  memset(dp, 0, sizeof dp);
  while (x) {
    num[++k] = x%10;
    x /= 10;
  }
  return dfs(k, false, true);
}

int main() {
  int a, b;
  while (scanf("%d %d", &a, &b) != EOF && (a||b)) {
    printf("%d\n", solve(b) - solve(a - 1));
  }

  return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章